diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..de40c5929 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,4 @@ count = count + 1; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing +//Line 3 taking the current value to count, then make the count to new value diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index ca1dfe7f2..b0105b28f 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -8,11 +8,29 @@ // Then, write the next test! :) Go through this process until all the cases are implemented function getAngleType(angle) { - if (angle === 90) { + + if (angle ===90){ + return "Right angle"; + + } else if (angle <90){ + + return "Acute angle"; + + } else if (angle >90 && angle <180){ + + return "Obtuse angle"; + + } else if (angle ===180){ + + return "Straight angle"; + + } else if (angle >180 && angle <360){ + + return "Reflex angle"; } - // Run the tests, work out what Case 2 is testing, and implement the required code here. - // Then keep going for the other cases, one at a time. + // Run the tests, work out what Case 2 is testing, and implement the required code here. + // Then keep going for the other cases, one at a time. } // The line below allows us to load the getAngleType function into tests in other files. @@ -21,9 +39,11 @@ module.exports = getAngleType; // we're going to use this helper function to make our assertions easier to read // if the actual output matches the target output, the test will pass + function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, + `Expected ${actualOutput} to equal ${targetOutput}` ); } @@ -37,19 +57,26 @@ function assertEquals(actualOutput, targetOutput) { // Case 1: Identify Right Angles: // When the angle is exactly 90 degrees, // Then the function should return "Right angle" + const right = getAngleType(90); + assertEquals(right, "Right angle"); // Case 2: Identify Acute Angles: // When the angle is less than 90 degrees, // Then the function should return "Acute angle" + const acute = getAngleType(45); + assertEquals(acute, "Acute angle"); // Case 3: Identify Obtuse Angles: // When the angle is greater than 90 degrees and less than 180 degrees, // Then the function should return "Obtuse angle" + const obtuse = getAngleType(120); + +assertEquals(obtuse, "Obtuse angle"); // ====> write your test here, and then add a line to pass the test in the function above // Case 4: Identify Straight Angles: @@ -57,7 +84,15 @@ const obtuse = getAngleType(120); // Then the function should return "Straight angle" // ====> write your test here, and then add a line to pass the test in the function above +const straight = getAngleType(180); + +assertEquals(straight, "Straight angle"); + // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" -// ====> write your test here, and then add a line to pass the test in the function above \ No newline at end of file +// ====> write your test here, and then add a line to pass the test in the function above + +const reflex = getAngleType(270); + +assertEquals(reflex, "Reflex angle"); diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index a4739af77..914b39e6a 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -7,21 +7,36 @@ // complete the rest of the tests and cases // write one test at a time, and make it pass, build your solution up methodically -function isProperFraction(numerator, denominator) { - if (numerator < denominator) { +function isProperFraction(numerator,denominator) { + + if (denominator=== 0) { + + return false; + + } + if (Math.abs(numerator)< Math.abs(denominator)){ + return true; + } + + else { + return false; } } // The line below allows us to load the isProperFraction function into tests in other files. // This will be useful in the "rewrite tests with jest" step. -module.exports = isProperFraction; +module.exports =isProperFraction; // here's our helper again -function assertEquals(actualOutput, targetOutput) { + +function assertEquals(actualOutput,targetOutput){ + console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` + + actualOutput ===targetOutput, + + `Expected ${actualOutput} to equal${targetOutput}` ); } @@ -31,29 +46,43 @@ function assertEquals(actualOutput, targetOutput) { // Input: numerator = 2, denominator = 3 // target output: true // Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true. -const properFraction = isProperFraction(2, 3); -assertEquals(properFraction, true); + +const properFraction =isProperFraction(2,3); + +assertEquals(properFraction,true); + // Improper Fraction check: // Input: numerator = 5, denominator = 2 // target output: false // Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false. -const improperFraction = isProperFraction(5, 2); -assertEquals(improperFraction, false); + +const improperFraction =isProperFraction(5,2); + +assertEquals(improperFraction,false); + // Negative Fraction check: // Input: numerator = -4, denominator = 7 // target output: true // Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true. -const negativeFraction = isProperFraction(-4, 7); // ====> complete with your assertion +const negativeFraction =isProperFraction(-4,7); + +assertEquals(negativeFraction,true); + + // Equal Numerator and Denominator check: // Input: numerator = 3, denominator = 3 // target output: false // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. -const equalFraction = isProperFraction(3, 3); // ====> complete with your assertion +const equalFraction =isProperFraction(3,3); + +assertEquals(equalFraction,false); + + // Stretch: // What other scenarios could you test for? diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index 266525d1b..7d1cf709c 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -7,38 +7,46 @@ // complete the rest of the tests and cases // write one test at a time, and make it pass, build your solution up methodically // just make one change at a time -- don't rush -- programmers are deep and careful thinkers -function getCardValue(card) { - if (rank === "A") { - return 11; - } + +function getCardValue(card){ + const rank = card.slice(0, -1); + + if (rank === "A") return11; + + if (["10", "J", "Q", "K"].includes(rank)) return 10; + + + if (["2","3","4","5","6","7","8","9"].includes(rank)) return Number(rank); + + throw new Error("Invalid card rank"); } +module.exports = getCardValue; + // The line below allows us to load the getCardValue function into tests in other files. // This will be useful in the "rewrite tests with jest" step. -module.exports = getCardValue; // You need to write assertions for your function to check it works in different cases // we're going to use this helper function to make our assertions easier to read // if the actual output matches the target output, the test will pass -function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); + +function assertEquals(actual, expected){ + + console.assert(actual === expected, `Expected ${actual} to equal ${expected}`); + } + // Acceptance criteria: // Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A), // When the function getCardValue is called with this card string as input, // Then it should return the numerical card value -const aceofSpades = getCardValue("A♠"); -assertEquals(aceofSpades, 11); // Handle Number Cards (2-10): // Given a card with a rank between "2" and "9", // When the function is called with such a card, // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). -const fiveofHearts = getCardValue("5♥"); + // ====> write your test here, and then add a line to pass the test in the function above // Handle Face Cards (J, Q, K): @@ -55,3 +63,4 @@ const fiveofHearts = getCardValue("5♥"); // Given a card with an invalid rank (neither a number nor a recognized face card), // When the function is called with such a card, // Then it should throw an error indicating "Invalid card rank." + diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index 4a92a3e82..c82fdcc30 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -12,15 +12,31 @@ test("should identify right angle (90°)", () => { // Case 2: Identify Acute Angles: // When the angle is less than 90 degrees, // Then the function should return "Acute angle" +test("should identify acute angles (<90°)",() =>{ + expect(getAngleType(45)).toEqual("Acute angle"); +}); // Case 3: Identify Obtuse Angles: // When the angle is greater than 90 degrees and less than 180 degrees, // Then the function should return "Obtuse angle" +test("should identify obtuse angles (90°{ + + expect(getAngleType(120)).toEqual("Obtuse angle"); +}); // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" +test("should identify straight angles (180°)",() =>{ + + expect(getAngleType(180)).toEqual("Straight angle"); +}); // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" + +test("should identify reflex angles (180°{ + + expect(getAngleType(270)).toEqual("Reflex angle"); +}); \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index caf08d15b..2755daa9c 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -2,12 +2,24 @@ // We will use the same function, but write tests for it using Jest in this file. const isProperFraction = require("../implement/2-is-proper-fraction"); -test("should return true for a proper fraction", () => { +test("should return true for a proper fraction",() => { expect(isProperFraction(2, 3)).toEqual(true); }); // Case 2: Identify Improper Fractions: +test("should return false for an improper fraction (numerator > denominator)",()=>{ + + expect(isProperFraction(5, 2)).toEqual(false); +}); // Case 3: Identify Negative Fractions: +test("should return true for a negative proper fraction (|numerator| < denominator)",()=>{ + + expect(isProperFraction(-4, 7)).toEqual(true); +}); // Case 4: Identify Equal Numerator and Denominator: +test("should return false when numerator equals denominator",()=>{ + + expect(isProperFraction(3, 3)).toEqual(false); +}); \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js index 04418ff72..4cad10146 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -2,12 +2,45 @@ // We will use the same function, but write tests for it using Jest in this file. const getCardValue = require("../implement/3-get-card-value"); -test("should return 11 for Ace of Spades", () => { +test("should return 11 for Ace of Spades",() =>{ const aceofSpades = getCardValue("A♠"); expect(aceofSpades).toEqual(11); }); // Case 2: Handle Number Cards (2-10): +test("should return the number value for number cards",() =>{ + + expect(getCardValue("5♥")).toEqual(5); + + expect(getCardValue("10♦")).toEqual(10); + +}); + // Case 3: Handle Face Cards (J, Q, K): +test("should return 10 for face cards J, Q, K",() =>{ + + expect(getCardValue("J♣")).toEqual(10); + + expect(getCardValue("Q♠")).toEqual(10); + + expect(getCardValue("K♦")).toEqual(10); + +}); + // Case 4: Handle Ace (A): +test("should return 11 for any Ace",() =>{ + + expect(getCardValue("A♦")).toEqual(11); + + expect(getCardValue("A♥")).toEqual(11); + +}); + // Case 5: Handle Invalid Cards: +test("Invalid cards throw error",() =>{ + + expect(() => getCardValue("Z♠")).toThrow("Invalid card rank"); + + expect(() => getCardValue("1♣")).toThrow("Invalid card rank"); + +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..444bb258b 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,15 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (let i = 0; i < stringOfCharacters.length; i++) { + + if (stringOfCharacters[i] === findCharacter) { + + count = count + 1; + } + } + + return count; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..59e7d6deb 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -10,11 +10,12 @@ const countChar = require("./count"); // When the function is called with these inputs, // Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa'). -test("should count multiple occurrences of a character", () => { - const str = "aaaaa"; - const char = "a"; - const count = countChar(str, char); - expect(count).toEqual(5); +test("should return 5 when counting 'a' in 'aaaaa'", () => { + expect(countChar("aaaaa", "a")).toEqual(5); +}); + +test("should return 1 when counting 'b' in 'abc'", () => { + expect(countChar("abc", "b")).toEqual(1); }); // Scenario: No Occurrences @@ -22,3 +23,11 @@ test("should count multiple occurrences of a character", () => { // And a character char that does not exist within the case-sensitive str, // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. + +test("should return 0 when counting 'z' in 'hello'", () => { + expect(countChar("hello", "z")).toEqual(0); +}); + +test("should return 0 when counting any character in an empty string", () => { + expect(countChar("", "a")).toEqual(0); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..c3af8730a 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,27 @@ -function getOrdinalNumber(num) { - return "1st"; +function getOrdinalNumber(num){ + + let last =num % 10; + + let lastTwo =num % 100; + + if(lastTwo >= 11 && lastTwo<= 13){ + + return num +"th"; + } + + if (last ===1)return num +"st"; + + if (last ===2)return num +"nd"; + + if (last ===3)return num +"rd"; + + return num +"th"; } +console.log(getOrdinalNumber(1)); +console.log(getOrdinalNumber(2)); +console.log(getOrdinalNumber(3)); +console.log(getOrdinalNumber(11)); +console.log(getOrdinalNumber(13)); + module.exports = getOrdinalNumber; diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index dfe4b6091..b53c6a428 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,6 +8,28 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); -}); +describe("getOrdinalNumber (category-based tests)", () => { + test("uses 'th' for 11, 12, 13 (teen exceptions)", () => { + expect(getOrdinalNumber(11)).toBe("11th"); + expect(getOrdinalNumber(12)).toBe("12th"); + expect(getOrdinalNumber(13)).toBe("13th"); + }); + test("adds 'st' for numbers ending in 1 (except 11)", () => { + expect(getOrdinalNumber(1)).toBe("1st"); + expect(getOrdinalNumber(21)).toBe("21st"); + }); + test("adds 'nd' for numbers ending in 2 (except 12)", () => { + expect(getOrdinalNumber(2)).toBe("2nd"); + expect(getOrdinalNumber(22)).toBe("22nd"); + }); + test("adds 'rd' for numbers ending in 3 (except 13)", () => { + expect(getOrdinalNumber(3)).toBe("3rd"); + expect(getOrdinalNumber(23)).toBe("23rd"); + }); + test("uses 'th' for other endings (0, 4-9)", () => { + expect(getOrdinalNumber(4)).toBe("4th"); + expect(getOrdinalNumber(10)).toBe("10th"); + expect(getOrdinalNumber(19)).toBe("19th"); + }); + +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..90def747d 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,24 @@ -function repeat() { - return "hellohellohello"; +function repeat(word,time){ + + if (time <0){ + + throw new Error("Invalid count"); + + } + + let result =""; + + for (let i = 0;i { // Given a target string str and a count equal to 1, // When the repeat function is called with these inputs, // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. +test("should return original string if count is 1",() =>{ + const str = "hi"; + + const count =1; + + const repeatedStr =repeat(str,count); + + expect(repeatedStr).toEqual("hi"); + +}); // case: Handle Count of 0: // Given a target string str and a count equal to 0, // When the repeat function is called with these inputs, // Then it should return an empty string, ensuring that a count of 0 results in an empty output. +test("should return empty string if count is 0",() =>{ + + const str ="test"; + + const count =0; + const repeatedStr =repeat(str,count); + + expect(repeatedStr).toEqual(""); +}); // case: Negative Count: // Given a target string str and a negative integer count, // When the repeat function is called with these inputs, // Then it should throw an error or return an appropriate error message, as negative counts are not valid. +test("should throw error if count is negative",() =>{ + + const str ="error"; + + const count =-2; + + expect(() => repeat(str,count)).toThrow("Invalid count"); +}); \ No newline at end of file diff --git a/Sprint-3/3-stretch/find.js b/Sprint-3/3-stretch/find.js index c7e79a2f2..5786dc8b5 100644 --- a/Sprint-3/3-stretch/find.js +++ b/Sprint-3/3-stretch/find.js @@ -20,6 +20,14 @@ console.log(find("code your future", "z")); // Pay particular attention to the following: // a) How the index variable updates during the call to find +It starts at 0 and goes up by 1 everytime the loop runs. + // b) What is the if statement used to check +It checks if the character in the string in the current index is the same +as the one we want to find, or we are looking for. + // c) Why is index++ being used? +To move to the next character in the string. + // d) What is the condition index < str.length used for? +Makes sure we dont go past the end of string. diff --git a/Sprint-3/3-stretch/password-validator.js b/Sprint-3/3-stretch/password-validator.js index b55d527db..afde52cd1 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,6 +1,22 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true +function passwordValidator(password){ + + if (password.length < 5) return false; + + if (!/[A-Z]/.test(password)) return false; + + if (!/[a-z]/.test(password)) return false; + + if (!/[0-9]/.test(password)) return false; + + if (!/[!#\$%\.\*&]/.test(password)) return false; + + return true; } +console.log(passwordValidator("Abc1")); + +console.log(passwordValidator("12345")); + +console.log(passwordValidator("abcde1")); module.exports = passwordValidator; \ No newline at end of file diff --git a/Sprint-3/3-stretch/password-validator.test.js b/Sprint-3/3-stretch/password-validator.test.js index 8fa3089d6..20cde4622 100644 --- a/Sprint-3/3-stretch/password-validator.test.js +++ b/Sprint-3/3-stretch/password-validator.test.js @@ -15,12 +15,28 @@ To be valid, a password must: You must breakdown this problem in order to solve it. Find one test case first and get that working */ const isValidPassword = require("./password-validator"); -test("password has at least 5 characters", () => { +test("password has at least 5 characters",() =>{ // Arrange - const password = "12345"; + const password = "Abc1"; // Act const result = isValidPassword(password); // Assert expect(result).toEqual(true); } -); \ No newline at end of file +); + +test("valid password passes all rules",() =>{ + + expect(isValidPassword("Abc1")).toBe(false); +}); + + +test("password has at least 5 characters",() =>{ + + expect(isValidPassword("12345")).toBe(false); +}); + +test("password missing number fails",() =>{ + + expect(isValidPassword("abcde")).toBe(false); +}); \ No newline at end of file