diff --git a/Sprint-3/1-key-implement/1-get-angle-type.js b/Sprint-3/1-key-implement/1-get-angle-type.js index 08d1f0cba..1447f1e30 100644 --- a/Sprint-3/1-key-implement/1-get-angle-type.js +++ b/Sprint-3/1-key-implement/1-get-angle-type.js @@ -8,8 +8,11 @@ // Then, write the next test! :) Go through this process until all the cases are implemented function getAngleType(angle) { - if (angle === 90) return "Right angle"; - // read to the end, complete line 36, then pass your test here + if (angle === 90) return "Right angle"; + if (angle < 90) return "Acute angle"; + if (angle > 90 && angle < 180) return "Obtuse angle"; + if (angle === 180) return "Straight angle"; + if (angle > 180 && angle < 360) return "Reflex angle"; } // we're going to use this helper function to make our assertions easier to read @@ -39,18 +42,23 @@ assertEquals(right, "Right 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); -// ====> write your test here, and then add a line to pass the test in the function above +assertEquals(obtuse, "Obtuse angle"); + // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // 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 +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); diff --git a/Sprint-3/1-key-implement/2-is-proper-fraction.js b/Sprint-3/1-key-implement/2-is-proper-fraction.js index 91583e941..82c0520d2 100644 --- a/Sprint-3/1-key-implement/2-is-proper-fraction.js +++ b/Sprint-3/1-key-implement/2-is-proper-fraction.js @@ -8,7 +8,9 @@ // write one test at a time, and make it pass, build your solution up methodically function isProperFraction(numerator, denominator) { + // Use absolute value of numerator to handle negatives if (numerator < denominator) return true; + return false; // for all other cases } // here's our helper again @@ -40,14 +42,28 @@ assertEquals(improperFraction, false); // 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 +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 +assertEquals(equalFraction, false); // Stretch: // What other scenarios could you test for? + +// Other scenarios to test could include: + +// Cases where the numerator or denominator is zero (e.g., 0/5, 5/0) — +// especially to check for division by zero or invalid fractions. + +// Fractions with negative denominators (e.g., 3/-4) +// to see if the function handles sign correctly. + +// Very large numbers +// to check if the function works with large integers. + +// Non-integer inputs (e.g., decimals or strings) +// to test input validation (if applicable). \ No newline at end of file diff --git a/Sprint-3/1-key-implement/3-get-card-value.js b/Sprint-3/1-key-implement/3-get-card-value.js index aa1cc9f90..43002d485 100644 --- a/Sprint-3/1-key-implement/3-get-card-value.js +++ b/Sprint-3/1-key-implement/3-get-card-value.js @@ -8,7 +8,12 @@ // 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; + const rank = card.slice(0, -1); // remove the emoji + if (rank === "A") return 11; + if (["K", "Q", "J", "10"].includes(rank)) return 10; + const number = Number(rank); + if (number >= 2 && number <= 9) return number; + throw new Error("Invalid card rank"); } // You need to write assertions for your function to check it works in different cases @@ -33,19 +38,36 @@ assertEquals(aceofSpades, 11); // 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 +assertEquals(fiveofHearts, 5); + // Handle Face Cards (J, Q, K): // Given a card with a rank of "10," "J," "Q," or "K", // When the function is called with such a card, // Then it should return the value 10, as these cards are worth 10 points each in blackjack. +const jackofClubs = getCardValue("J♣"); +assertEquals(jackofClubs, 10); + +const queenofDiamonds = getCardValue("Q♦"); +assertEquals(queenofDiamonds, 10); + +const kingofHearts = getCardValue("K♥"); +assertEquals(kingofHearts, 10); // Handle Ace (A): // Given a card with a rank of "A", // When the function is called with an Ace, // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. +const aceofSpades = getCardValue("A♠"); +assertEquals(aceofSpades, 11); + // Handle Invalid Cards: // 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." + + +// getCardValue("Z♠"); // should throw "Invalid card rank" + + diff --git a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js index d61254bd7..e7a3e28ee 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js +++ b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js @@ -1,7 +1,10 @@ function getAngleType(angle) { if (angle === 90) return "Right angle"; - // replace with your completed function from key-implement - + if (angle < 90) return "Acute angle"; + if (angle > 90 && angle < 180) return "Obtuse angle"; + if (angle === 180) return "Straight angle"; + if (angle > 180 && angle < 360) return "Reflex angle"; + throw new Error("Invalid angle"); } @@ -11,8 +14,10 @@ function getAngleType(angle) { + // Don't get bogged down in this detail // Jest uses CommonJS module syntax by default as it's quite old // We will upgrade our approach to ES6 modules in the next course module, so for now // we have just written the CommonJS module.exports syntax for you + module.exports = getAngleType; \ No newline at end of file diff --git a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js index b62827b7c..c3a35db8e 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js +++ b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js @@ -4,21 +4,19 @@ test("should identify right angle (90°)", () => { expect(getAngleType(90)).toEqual("Right angle"); }); -// REPLACE the comments with the tests -// make your test descriptions as clear and readable as possible +test("should identify acute angle (< 90°)", () => { + expect(getAngleType(45)).toEqual("Acute angle"); +}); -// Case 2: Identify Acute Angles: -// When the angle is less than 90 degrees, -// Then the function should return "Acute angle" +test("should identify obtuse angle (> 90° and < 180°)", () => { + expect(getAngleType(120)).toEqual("Obtuse 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 straight angle (180°)", () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); -// Case 4: Identify Straight Angles: -// When the angle is exactly 180 degrees, -// Then the function should return "Straight angle" +test("should identify reflex angle (> 180° and < 360°)", () => { + expect(getAngleType(270)).toEqual("Reflex 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" diff --git a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js index 9836fe398..9305566fa 100644 --- a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js +++ b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js @@ -1,6 +1,15 @@ +// Explanation: +// It checks if the absolute value of the numerator +// is less than the absolute value of the denominator. + + function isProperFraction(numerator, denominator) { - if (numerator < denominator) return true; - // add your completed function from key-implement here + if (denominator === 0) { + throw new Error("Denominator cannot be zero"); + } + + return Math.abs(numerator) < Math.abs(denominator); } -module.exports = isProperFraction; \ No newline at end of file +module.exports = isProperFraction; + diff --git a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js index ff1cc8173..7032715dc 100644 --- a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js +++ b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js @@ -5,7 +5,16 @@ test("should return true for a proper fraction", () => { }); // Case 2: Identify Improper Fractions: +test("should return false for an improper fraction", () => { + expect(isProperFraction(5, 2)).toEqual(false); +}); // Case 3: Identify Negative Fractions: +test("should return true for a negative proper fraction", () => { + 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); +}); diff --git a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js index 0d95d3736..e870ce006 100644 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js +++ b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js @@ -1,5 +1,15 @@ function getCardValue(card) { - // replace with your code from key-implement - return 11; + const rank = card.slice(0, -1); // All characters except the last one (suit) + + if (!rank) throw new Error("Invalid card rank"); + + if (rank === "A") return 11; + if (["K", "Q", "J", "10"].includes(rank)) return 10; + + const number = Number(rank); + if (number >= 2 && number <= 9) return number; + + throw new Error("Invalid card rank"); } -module.exports = getCardValue; \ No newline at end of file + +module.exports = getCardValue; diff --git a/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js b/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js index 03a8e2f34..463380642 100644 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js +++ b/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js @@ -1,11 +1,33 @@ const getCardValue = require("./3-get-card-value"); test("should return 11 for Ace of Spades", () => { - const aceofSpades = getCardValue("A♠"); - expect(aceofSpades).toEqual(11); - }); - -// Case 2: Handle Number Cards (2-10): -// Case 3: Handle Face Cards (J, Q, K): -// Case 4: Handle Ace (A): -// Case 5: Handle Invalid Cards: + expect(getCardValue("A♠")).toEqual(11); +}); + +// Case 2: Handle Number Cards (2-10) +test("should return 5 for Five of Hearts", () => { + expect(getCardValue("5♥")).toEqual(5); +}); + +test("should return 10 for Ten of Diamonds", () => { + expect(getCardValue("10♦")).toEqual(10); +}); + +// Case 3: Handle Face Cards (J, Q, K) +test("should return 10 for Jack of Clubs", () => { + expect(getCardValue("J♣")).toEqual(10); +}); + +test("should return 10 for Queen of Spades", () => { + expect(getCardValue("Q♠")).toEqual(10); +}); + +test("should return 10 for King of Hearts", () => { + expect(getCardValue("K♥")).toEqual(10); +}); + +// Case 5: Handle Invalid Cards +test("should throw an error for invalid card rank", () => { + expect(() => getCardValue("Z♣")).toThrow("Invalid card rank"); +}); + diff --git a/Sprint-3/3-mandatory-practice/implement/count.js b/Sprint-3/3-mandatory-practice/implement/count.js index fce249650..189cdd7c6 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.js +++ b/Sprint-3/3-mandatory-practice/implement/count.js @@ -1,5 +1,11 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (let char of stringOfCharacters) { + if (char === findCharacter) { + count++; + } + } + return count; } module.exports = countChar; \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js index 24f528b0d..a9af31a01 100644 --- a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js +++ b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js @@ -1,5 +1,12 @@ function getOrdinalNumber(num) { - return "1st"; + const suffixes = ["th", "st", "nd", "rd"]; + const value = num % 100; + if (value >= 11 && value <= 13) { + return num + "th"; + } + const lastDigit = num % 10; + const suffix = suffixes[lastDigit] || "th"; + return num + suffix; } -module.exports = getOrdinalNumber; \ No newline at end of file +module.exports = getOrdinalNumber; diff --git a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js index 6d55dfbb4..f35c237bb 100644 --- a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js +++ b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js @@ -11,3 +11,20 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); +}); + +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); + diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.js b/Sprint-3/3-mandatory-practice/implement/repeat.js index 621f9bd35..82c4a1edf 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.js @@ -1,5 +1,8 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (count < 0) { + throw new Error("Count must be non-negative"); + } + return str.repeat(count); } -module.exports = repeat; \ No newline at end of file +module.exports = repeat; diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.test.js b/Sprint-3/3-mandatory-practice/implement/repeat.test.js index 8a4ab42ef..facc0f327 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.test.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.test.js @@ -16,17 +16,46 @@ test("should repeat the string count times", () => { expect(repeatedStr).toEqual("hellohellohello"); }); + const repeat = require("./repeat"); + +// test("should repeat the string count times", () => { +// expect(repeat("hello", 3)).toEqual("hellohellohello"); +// }); + // case: handle Count of 1: // 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 when count is 1", () => { + expect(repeat("hello", 1)).toEqual("hello"); +}); + + // 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 when count is 0", () => { + expect(repeat("hello", 0)).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 for negative count", () => { + expect(() => repeat("hello", -1)).toThrow("Count must be non-negative"); +}); + + + + + + + + + + diff --git a/Sprint-3/4-stretch-investigate/find.js b/Sprint-3/4-stretch-investigate/find.js index c7e79a2f2..aebe1ca90 100644 --- a/Sprint-3/4-stretch-investigate/find.js +++ b/Sprint-3/4-stretch-investigate/find.js @@ -1,25 +1,35 @@ function find(str, char) { + // Start searching from the first character (index 0) let index = 0; + // Continue looping while index is less than the length of the string + // This prevents us from going beyond the last character of the string while (index < str.length) { + // Check if the current character matches the one we are looking for if (str[index] === char) { + // If found, return the current index position return index; } - index++; + // Move to the next character in the string + index++; // This increments index by 1, so the loop moves through each character } + + // If we finish the loop without finding the character, return -1 + // This means the character does not exist in the string return -1; } -console.log(find("code your future", "u")); -console.log(find("code your future", "z")); - -// The while loop statement allows us to do iteration - the repetition of a certain number of tasks according to some condition -// See the docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while +// Test examples: +console.log(find("code your future", "u")); // Should output the first index of 'u' +console.log(find("code your future", "z")); // Should output -1 since 'z' is not in the string -// Use the Python Visualiser to help you play computer with this example and observe how this code is executed -// Pay particular attention to the following: +/* +Explanation: +a) The 'index' variable starts at 0 and increments by 1 each loop (index++), allowing the function to +check each character in the string. +b) The 'if' statement checks whether the current character (str[index]) matches the character we're searching for. +c) 'index++' is used to move forward to the next character in the string on each loop iteration. +d) The condition 'index < str.length' ensures the loop only runs while 'index' is a valid position inside the string, +preventing errors. +*/ -// a) How the index variable updates during the call to find -// b) What is the if statement used to check -// c) Why is index++ being used? -// d) What is the condition index < str.length used for? diff --git a/Sprint-3/4-stretch-investigate/password-validator.js b/Sprint-3/4-stretch-investigate/password-validator.js index b55d527db..7c3bcecb2 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.js +++ b/Sprint-3/4-stretch-investigate/password-validator.js @@ -1,6 +1,48 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true +function isValidCreditCard(cardNumber) { + // Check if cardNumber is exactly 16 characters and all are digits + if (!/^\d{16}$/.test(cardNumber)) { + return false; + } + + // Check if card has at least two different digits + if (/^(\d)\1{15}$/.test(cardNumber)) { + return false; + } + + // Check if last digit is even + const lastDigit = Number(cardNumber[15]); + if (lastDigit % 2 !== 0) { + return false; + } + + // Calculate the sum of all digits and check if greater than 16 + const sum = cardNumber.split('').reduce((acc, digit) => acc + Number(digit), 0); + if (sum <= 16) { + return false; + } + + // All conditions passed, card is valid + return true; } +// Example usage: +// console.log(isValidCreditCard("9999777788880000")); // true +// console.log(isValidCreditCard("4444444444444444")); // false + +module.exports = isValidCreditCard; + +/* + +Explanation: + +The first if uses a regular expression to ensure the card number is exactly 16 digits long and contains only digits. + +The second if uses a regex to check if all digits are the same (e.g., all '4's). + +Then, we check if the last digit is even using modulo %. + +Then, we calculate the sum of all digits with reduce and ensure it is greater than 16. + +Finally, we return true if all these checks pass, else false. -module.exports = passwordValidator; \ No newline at end of file +*/ \ No newline at end of file