From 3b13a90330ddf2a6fe1120e56700a147e99b32d2 Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Sun, 29 Jun 2025 13:16:58 +0100 Subject: [PATCH 01/11] completed the code to pass the remaining angles --- Sprint-3/1-key-implement/1-get-angle-type.js | 26 ++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) 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..9d8411b25 100644 --- a/Sprint-3/1-key-implement/1-get-angle-type.js +++ b/Sprint-3/1-key-implement/1-get-angle-type.js @@ -34,23 +34,35 @@ const right = getAngleType(90); assertEquals(right, "Right angle"); // Case 2: Identify Acute Angles: -// When the angle is less than 90 degrees, +// When the angle is less than 90 degrees,Add commentMore actions // Then the function should return "Acute angle" -const acute = getAngleType(45); -assertEquals(acute, "Acute angle"); + +test("should identify acute angles (e.g., 45°)", () => { + expect(getAngleType(45)).toEqual("Acute angle"); + expect(getAngleType(89.9)).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" -const obtuse = getAngleType(120); -// ====> write your test here, and then add a line to pass the test in the function above + +test("should identify obtuse angles (e.g., 90.1° and 179.9°)", () => { + expect(getAngleType (90.1)).toEqual("Obtuse angle"); + expect(getAngleType (179.9)).toEqual("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 +test("should identify straight angle (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" -// ====> write your test here, and then add a line to pass the test in the function above \ No newline at end of file + +test("should identify reflex angles (180.1° and 359.9°)", () => { + expect(getAngleType(180.1)).toEqual("Reflex angle"); + expect(getAngleType (359.9)).toEqual("Reflex angle"); + }); From cd2d9f126cf6481335c050cefc80fec4e3f86c05 Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Sun, 29 Jun 2025 13:47:57 +0100 Subject: [PATCH 02/11] fraction code testdone --- Sprint-3/1-key-implement/2-is-proper-fraction.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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..3a8597095 100644 --- a/Sprint-3/1-key-implement/2-is-proper-fraction.js +++ b/Sprint-3/1-key-implement/2-is-proper-fraction.js @@ -40,6 +40,7 @@ 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); +assertEquals(properFraction, true); // ====> complete with your assertion // Equal Numerator and Denominator check: @@ -51,3 +52,17 @@ const equalFraction = isProperFraction(3, 3); // Stretch: // What other scenarios could you test for? +// fraction is true because the absolute value of the numerator (0) is less than the denominator (5). +assertEquals(isProperFraction (0, 5), true) + +// fraction is fa;se because the absolute value of the numerator (3) is greater than the denominator (0). +assertEquals(isProperFraction(3, 0), false); + +// fraction is true because the absolute value of the numerator (2) is less than the denominator (-5). +assertEquals(isProperFraction(2, -5), true); + +// fraction is false because the absolute value of the numerator (-3) is greater than the denominator (-4). +assertEquals(isProperFraction(-3, -4), false); + +// fraction is true because the absolute value of the numerator (-9) is less than the denominator (5). +assertEquals(isProperFraction(-9, 5), false); \ No newline at end of file From b3bfd4b0301dd99bb496942e858c2779f876afb3 Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Mon, 30 Jun 2025 23:58:07 +0100 Subject: [PATCH 03/11] made changes to the card values --- Sprint-3/1-key-implement/3-get-card-value.js | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) 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..15af943f5 100644 --- a/Sprint-3/1-key-implement/3-get-card-value.js +++ b/Sprint-3/1-key-implement/3-get-card-value.js @@ -7,6 +7,7 @@ // 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 +// if (rank === "A") return 11; function getCardValue(card) { if (rank === "A") return 11; } @@ -33,19 +34,44 @@ 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♥"); +assertEquals(fiveofHearts, 5); +const sevenofspades = getCardValue("7♠"); +assertEquals(sevenofspades , 7); +const eightofHearts = getCardValue("8♥"); +assertEquals(eightofHearts, 8); +const nineofdiamonds = getCardValue("9♦"); +assertEquals(nineofdiamonds, 9); + // ====> write your test here, and then add a line to pass the test in the function above // 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 tenofSpades = getCardValue("10♠"); +assertEquals(tenofSpades("10♠"), 10); +const jackofspades = getCardValue("J♠"); +assertEquals(jackofspades("J♠"), 10); +const Queenofhearts = getCardValue("Q♥"); +assertEquals(Queenofhearts("Q♥"), 10); +const Kingofhearts = getCardValue("K♥"); +assertEquals(Kingofhearts ("K♥"), 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 AceCard = getCardValue("A"); +assertEquals(AceCard ("A"), 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." + +const invalidCards = ["Z♠", "11", "Y♠", "1♣"]; +for (const card of invalidCards) { + assertEquals(error.message, "Invalid card rank"); +} \ No newline at end of file From eb25c346e0596693884277202ac40251bd2a30a9 Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Tue, 1 Jul 2025 23:30:27 +0100 Subject: [PATCH 04/11] using the template, i made some corrections to the mandatory-rewrite --- .../2-mandatory-rewrite/1-get-angle-type.js | 19 +++++---- .../1-get-angle-type.test.js | 18 +++++++++ .../2-is-proper-fraction.js | 14 +++++-- .../2-is-proper-fraction.test.js | 9 +++++ .../2-mandatory-rewrite/3-get-card-value.js | 14 ++++++- .../3-get-card-value.test.js | 39 +++++++++++++++++-- 6 files changed, 97 insertions(+), 16 deletions(-) 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..8faa4ab09 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js +++ b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js @@ -1,16 +1,19 @@ function getAngleType(angle) { - if (angle === 90) return "Right angle"; + if (angle === 90) { + return "Right angle"; // replace with your completed function from key-implement + } 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"; + } } - - - - - - - // 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 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..dfd482882 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 @@ -11,14 +11,32 @@ test("should identify right angle (90°)", () => { // When the angle is less than 90 degrees, // Then the function should return "Acute angle" +test("should identify acute angles (e.g., 45°)", () => { + expect(getAngleType(45)).toEqual("Acute angle"); + expect(getAngleType(89.9)).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 (e.g., 90.1° and 179.9°)", () => { + expect(getAngleType (90.1)).toEqual("Obtuse angle"); + expect(getAngleType (179.9)).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 angle (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.1° and 359.9°)", () => { + expect(getAngleType(180.1)).toEqual("Reflex angle"); + expect(getAngleType (359.9)).toEqual("Reflex angle"); +}); \ No newline at end of file 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..c60140111 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,14 @@ function isProperFraction(numerator, denominator) { - if (numerator < denominator) return true; - // add your completed function from key-implement here -} + if (numerator < denominator) return true;} + if (Math.abs(numerator) < Math.abs(denominator)) { + return true; + } { return false; } + if (numerator < denominator) { + return true; + } if (numerator === denominator) { + return false; + } if (denominator === 0) { + return false; + } module.exports = isProperFraction; \ No newline at end of file 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..08d5b7781 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 a Improper fraction", () => { + expect(isProperFraction(5, 3)).toEqual(false); +}); // Case 3: Identify Negative Fractions: +test("should return true for negative fraction", () => { + expect(isProperFraction(-2, 3)).toEqual(true); +}); // Case 4: Identify Equal Numerator and Denominator: +test("should return false for equal numerator and 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..883f9ee7e 100644 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js +++ b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js @@ -2,4 +2,16 @@ function getCardValue(card) { // replace with your code from key-implement return 11; } -module.exports = getCardValue; \ No newline at end of file +function getCardValue(card) { + const rank = card.slice(0, -1); // strip off the suit emoji + + if (rank === "A") return 11; + if (["K", "Q", "J", "10"].includes(rank)) return 10; + + const number = parseInt(rank); + if (!isNaN(number) && number >= 2 && number <= 9) { + return number; + } + + throw new Error("Invalid card rank"); +} 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..d457a5212 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 @@ -4,8 +4,39 @@ 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 5 for 5 of Hearts", () => { + const five = getCardValue("5♥"); + expect(five).toEqual(5); +}); + +test("should return 10 for 10 of Diamonds", () => { + const ten = getCardValue("10♦"); + expect(ten).toEqual(10); +}); + +// Case 3: Handle Face Cards (J, Q, K) +test("should return 10 for Jack of Spades", () => { + const jack = getCardValue("J♠"); + expect(jack).toEqual(10); +}); + +test("should return 10 for Queen of Clubs", () => { + const queen = getCardValue("Q♣"); + expect(queen).toEqual(10); +}); + +test("should return 10 for King of Hearts", () => { + const king = getCardValue("K♥"); + expect(king).toEqual(10); +}); + +// Case 4: Handle Ace + const AceCard = getCardValue("A"); +assertEquals(AceCard ("A"), 11); + +// Case 5: Handle Invalid Cards +test("should throw error for invalid card Z♠", () => { + expect(() => getCardValue("Z♠")).toThrow("Invalid card rank"); +}); -// 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: From 52cc4a38bda65c0067ed9274f4c62b3a04926a89 Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Wed, 2 Jul 2025 00:15:07 +0100 Subject: [PATCH 05/11] mandatory practice done --- .../3-mandatory-practice/implement/count.js | 14 +++++++- .../implement/count.test.js | 7 ++++ .../implement/get-ordinal-number.js | 21 +++++++++++- .../implement/get-ordinal-number.test.js | 32 +++++++++++++++++-- .../3-mandatory-practice/implement/repeat.js | 8 ++++- .../implement/repeat.test.js | 32 +++++++++++++++++++ 6 files changed, 108 insertions(+), 6 deletions(-) diff --git a/Sprint-3/3-mandatory-practice/implement/count.js b/Sprint-3/3-mandatory-practice/implement/count.js index fce249650..bd26dda9c 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.js +++ b/Sprint-3/3-mandatory-practice/implement/count.js @@ -2,4 +2,16 @@ function countChar(stringOfCharacters, findCharacter) { return 5 } -module.exports = countChar; \ No newline at end of file +module.exports = countChar; + +function countChar(stringOfCharacters, findCharacter) { + let count = 0; + for (let char of stringOfCharacters) { + if (char === findCharacter) { + count++; + } + } + return count; +} + +module.exports = countChar; diff --git a/Sprint-3/3-mandatory-practice/implement/count.test.js b/Sprint-3/3-mandatory-practice/implement/count.test.js index 42baf4b4b..8d5d152ee 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.test.js +++ b/Sprint-3/3-mandatory-practice/implement/count.test.js @@ -22,3 +22,10 @@ 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 if the character does not exist in the string", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); \ 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..2c851454a 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,24 @@ function getOrdinalNumber(num) { return "1st"; } +function getOrdinalNumber(num) { + const lastDigit = num % 10; + const lastTwoDigits = num % 100; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return num + "th"; + } + + switch (lastDigit) { + case 1: + return num + "st"; + case 2: + return num + "nd"; + case 3: + return num + "rd"; + default: + return num + "th"; + } +} -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..3a585daa7 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 @@ -8,6 +8,32 @@ 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"); - }); +const getOrdinalNumber = require("./getOrdinalNumber"); + +test("should return 1st", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); +}); + +test("should return 2nd", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +test("should return 3rd", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +test("should return 8th", () => { + expect(getOrdinalNumber(8)).toEqual("8th"); +}); + +test("should return 19th", () => { + expect(getOrdinalNumber(19)).toEqual("19th"); +}); + +test("should return 81st", () => { + expect(getOrdinalNumber(81)).toEqual("81st"); +}); + +test("should return 125th", () => { + expect(getOrdinalNumber(125)).toEqual("125th"); +}); diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.js b/Sprint-3/3-mandatory-practice/implement/repeat.js index 621f9bd35..d2b8f0c23 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.js @@ -2,4 +2,10 @@ function repeat() { return "hellohellohello"; } -module.exports = repeat; \ No newline at end of file +module.exports = repeat; + +function repeat(word = "hello", times = 3) { + return word.repeat(times); +} + +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..ed6ca3157 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.test.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.test.js @@ -30,3 +30,35 @@ test("should repeat the string count times", () => { // 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. +const repeat = require("./repeat"); + +// Case: Repeat string multiple times +test("should repeat the string count times", () => { + const str = "hello"; + const count = 3; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hellohellohello"); +}); + +// Case: Handle count of 1 +test("should return the original string if count is 1", () => { + const str = "test"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("test"); +}); + +// Case: Handle count of 0 +test("should return an empty string if count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); + +// Case: Negative count +test("should throw an error if count is negative", () => { + const str = "hello"; + const count = -2; + expect(() => repeat(str, count)).toThrow("Invalid "); +}); From 339c2ba2e94cb6f9e2532a1dbf6876345ab9b404 Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Wed, 2 Jul 2025 23:00:56 +0100 Subject: [PATCH 06/11] made changes to validate the card --- .../4-stretch-investigate/card-validator.md | 34 +++++++++++++++++++ Sprint-3/4-stretch-investigate/find.js | 19 ++++++++--- .../password-validator.js | 33 ++++++++++++++++++ .../password-validator.test.js | 33 +++++++++++++++++- 4 files changed, 114 insertions(+), 5 deletions(-) diff --git a/Sprint-3/4-stretch-investigate/card-validator.md b/Sprint-3/4-stretch-investigate/card-validator.md index e39c6ace6..fe5cdbde2 100644 --- a/Sprint-3/4-stretch-investigate/card-validator.md +++ b/Sprint-3/4-stretch-investigate/card-validator.md @@ -33,3 +33,37 @@ These are the requirements your project needs to fulfill: - Return a boolean from the function to indicate whether the credit card number is valid. Good luck! + +function validateCreditCard(cardNumber) { + // cardNumber = cardNumber.replace(/\D/g, ""); + if (cardNumber.length !== 16 || !/^\d{16}$/.test(cardNumber)) { + return false; + } + + // Check: Must contain at least 2 different digits + const sameDigits = new Set(cardNumber); + if (sameDigits.size < 2) { + return false; + } + + // Check: Last digit must be even + const lastDigit = Number(cardNumber[cardNumber.length - 1]); + if (lastDigit % 2 !== 0) { + return false; + } + + // let digitSum = 0; + for (let digit of strNum) { + digitSum += Number(digit); + } // Convert each character to a number and add it to the sum + if (digitSum <= 16) { + return false; + } + + return true; // All validation rules passed + } + + // All checks passed; valid card + return true; + +module.exports = validateCreditCard; diff --git a/Sprint-3/4-stretch-investigate/find.js b/Sprint-3/4-stretch-investigate/find.js index c7e79a2f2..b34382ead 100644 --- a/Sprint-3/4-stretch-investigate/find.js +++ b/Sprint-3/4-stretch-investigate/find.js @@ -18,8 +18,19 @@ console.log(find("code your future", "z")); // 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: +/* a) how does the index variable update during the call to find? +index starts at 0, and it increases by 1 on each loop iteration using index++. +It keeps track of the current position in the string as you check each character. -// 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? +b) What is the if statement used to check +This checks whether the character at the current index matches the target character. +If it does, the function returns the index and stops running. + +c) Why is index++ being used? +index++ increments the index by 1 after each check, so the loop continues moving forward through the string. +Without this, the loop would get stuck on the same character instead of running through the rest of the string. + +d) What is the condition index < str.length used for? +This ensures the loop runs only while index is within the bounds of the string. +It prevents accessing characters beyond the end of the string. +*/ diff --git a/Sprint-3/4-stretch-investigate/password-validator.js b/Sprint-3/4-stretch-investigate/password-validator.js index b55d527db..69b6ef37c 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.js +++ b/Sprint-3/4-stretch-investigate/password-validator.js @@ -1,6 +1,39 @@ function passwordValidator(password) { return password.length < 5 ? false : true } +function passwordValidator(password) { + return password.length >= 5; +} +// Checks whether the password variable is a string + if (typeof password !== 'string') return false; + + // Check length is at least a maximum of 5 characters long + if (password.length < 5) return true; + + // Check for at least one uppercase letter A-Z + if (!/[A-Z]/.test(password)) return false; + + // Check for at least one lowercase letter a-z + if (!/[a-z]/.test(password)) return false; + + // Check for at least one number 0-9 + if (!/[0-9]/.test(password)) return false; + + // Check for at least one allowed special character: ! # $ % . * & + if (!/[!#$%.*&]/.test(password)) return false; + + // Check that password is not in previousPasswords array + if (previousPasswords.includes(password)) return false; + + // If all checks pass, return true + return true; + + const hasUppercase = /[A-Z]/.test(password); + const hasLowercase = /[a-z]/.test(password); + const hasDigit = /[0-9]/.test(password); + const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password); + const hasNoSpaces = !/\s/.test(password); + module.exports = passwordValidator; \ No newline at end of file diff --git a/Sprint-3/4-stretch-investigate/password-validator.test.js b/Sprint-3/4-stretch-investigate/password-validator.test.js index 8fa3089d6..0d7a7dcae 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.test.js +++ b/Sprint-3/4-stretch-investigate/password-validator.test.js @@ -23,4 +23,35 @@ test("password has at least 5 characters", () => { // Assert expect(result).toEqual(true); } -); \ No newline at end of file +); const previousPasswords = ["Password123!", "Welcome1$", "Admin#2023"]; + +function isValidPassword(password) { + if (password.length < 5) { + return { valid: false, message: "Password must be at least 5 characters long." }; + } + + if (!/[A-Z]/.test(password)) { + return { valid: false, message: "Password must include at least one uppercase letter." }; + } + + if (!/[a-z]/.test(password)) { + return { valid: false, message: "Password must include at least one lowercase letter." }; + } + + if (!/[0-9]/.test(password)) { + return { valid: false, message: "Password must include at least one number." }; + } + + if (!/[!#$%.*&]/.test(password)) { + return { valid: false, message: "Password must include at least one special symbol (!#$%.*&)." }; + } + + if (previousPasswords.includes(password)) { + return { valid: false, message: "This password has already been used. Please choose a new one." }; + } + + return { valid: true }; +} + +module.exports = isValidPassword; + From 14c036b09e2a4fc01ab4d381292dbae337b520ff Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Sat, 5 Jul 2025 16:09:00 +0100 Subject: [PATCH 07/11] created a new file for card validator and ran the code --- .../1-percentage-change.js | 2 +- .../2-mandatory-rewrite/1-get-angle-type.js | 23 ++++--- .../2-is-proper-fraction.js | 21 +++--- .../3-get-card-value.test.js | 29 ++++----- .../implement/get-ordinal-number.test.js | 56 ++++++++-------- .../4-stretch-investigate/card-validator.js | 64 +++++++++++++++++++ .../4-stretch-investigate/card-validator.md | 31 --------- .../password-validator.js | 50 ++++++--------- .../password-validator.test.js | 17 ++--- 9 files changed, 155 insertions(+), 138 deletions(-) create mode 100644 Sprint-3/4-stretch-investigate/card-validator.js diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..69932ca6d 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,"")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; 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 8faa4ab09..93e872999 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js +++ b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js @@ -1,19 +1,26 @@ function getAngleType(angle) { + // Ensure angle is a number and within valid bounds + if (typeof angle !== "number" || angle <= 0 || angle >= 360) { + return "Invalid angle"; + } + if (angle < 90) { + return "Acute angle"; + } if (angle === 90) { return "Right angle"; - // replace with your completed function from key-implement - - } else if (angle < 90) { - return "Acute angle"; - } else if (angle > 90 && angle < 180) { + } + if (angle < 180) { return "Obtuse angle"; - } else if (angle === 180) { + } + if (angle === 180) { return "Straight angle"; - } else if (angle > 180 && angle < 360) { - return "Reflex angle"; } + // Any remaining angle must be between 180 and 360 (exclusive) + return "Reflex 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 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 c60140111..63c51b36f 100644 --- a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js +++ b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js @@ -1,14 +1,11 @@ + function isProperFraction(numerator, denominator) { - if (numerator < denominator) return true;} - if (Math.abs(numerator) < Math.abs(denominator)) { - return true; - } { return false; } - if (numerator < denominator) { - return true; - } if (numerator === denominator) { - return false; - } if (denominator === 0) { - return false; - } + // Check for invalid fraction (denominator cannot be zero) + if (denominator === 0) { + return false; + } + // Check if the absolute value of the numerator is less than the absolute value of the denominator + 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/3-get-card-value.test.js b/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js index d457a5212..1a78c4c00 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 @@ -5,9 +5,15 @@ test("should return 11 for Ace of Spades", () => { expect(aceofSpades).toEqual(11); }); // Case 2: Handle Number Cards (2-10) -test("should return 5 for 5 of Hearts", () => { - const five = getCardValue("5♥"); - expect(five).toEqual(5); +test("should return the value of number cards (2-10)", () => { + const testCases = [ + ["2♣", 2], + ["5♠", 5], + ["10♦", 10] + ]; + testCases.forEach(([card, expected]) => { + expect(getCardValue(card)).toEqual(expected); + }); }); test("should return 10 for 10 of Diamonds", () => { @@ -16,19 +22,10 @@ test("should return 10 for 10 of Diamonds", () => { }); // Case 3: Handle Face Cards (J, Q, K) -test("should return 10 for Jack of Spades", () => { - const jack = getCardValue("J♠"); - expect(jack).toEqual(10); -}); - -test("should return 10 for Queen of Clubs", () => { - const queen = getCardValue("Q♣"); - expect(queen).toEqual(10); -}); - -test("should return 10 for King of Hearts", () => { - const king = getCardValue("K♥"); - expect(king).toEqual(10); +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 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 3a585daa7..421cfd6e3 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 @@ -8,32 +8,32 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -const getOrdinalNumber = require("./getOrdinalNumber"); +function getOrdinalNumber(num) { + // Handle invalid inputs gracefully (optional) + if (typeof num !== "number" || !Number.isInteger(num)) { + return "Invalid input"; + } + + // Handle the special case for teens (11, 12, 13) + const teenRemainder = num % 100; + if (teenRemainder >= 11 && teenRemainder <= 13) { + return num + "th"; + } + + // Get the last digit to determine suffix + const lastDigit = num % 10; + + switch (lastDigit) { + case 1: + return num + "st"; + case 2: + return num + "nd"; + case 3: + return num + "rd"; + default: + return num + "th"; + } +} + +module.exports = getOrdinalNumber; -test("should return 1st", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); -}); - -test("should return 2nd", () => { - expect(getOrdinalNumber(2)).toEqual("2nd"); -}); - -test("should return 3rd", () => { - expect(getOrdinalNumber(3)).toEqual("3rd"); -}); - -test("should return 8th", () => { - expect(getOrdinalNumber(8)).toEqual("8th"); -}); - -test("should return 19th", () => { - expect(getOrdinalNumber(19)).toEqual("19th"); -}); - -test("should return 81st", () => { - expect(getOrdinalNumber(81)).toEqual("81st"); -}); - -test("should return 125th", () => { - expect(getOrdinalNumber(125)).toEqual("125th"); -}); diff --git a/Sprint-3/4-stretch-investigate/card-validator.js b/Sprint-3/4-stretch-investigate/card-validator.js new file mode 100644 index 000000000..c69e7ad0d --- /dev/null +++ b/Sprint-3/4-stretch-investigate/card-validator.js @@ -0,0 +1,64 @@ +function validateCreditCard(cardNumber) { + // Check length and numeric characters + if (cardNumber.length !== 16 || !/^\d{16}$/.test(cardNumber)) { + return false; + } + + // Check at least two different digits + const uniqueDigits = new Set(cardNumber); + if (uniqueDigits.size < 2) { + return false; + } + + // Check last digit is even + const lastDigit = Number(cardNumber[cardNumber.length - 1]); + if (lastDigit % 2 !== 0) { + return false; + } + + // Check sum of digits + let digitSum = 0; + for (let digit of cardNumber) { +function validateCreditCard(cardNumber) { + // Check length and numeric characters + if (cardNumber.length !== 16 || !/^\d{16}$/.test(cardNumber)) { + return false; + } + + // Check at least two different digits + const uniqueDigits = new Set(cardNumber); + if (uniqueDigits.size < 2) { + return false; + } + + // Check last digit is even + const lastDigit = Number(cardNumber[cardNumber.length - 1]); + if (lastDigit % 2 !== 0) { + return false; + } + + // Check sum of digits + let digitSum = 0; + for (let digit of cardNumber) { + digitSum += Number(digit); + } + if (digitSum <= 16) { + return false; + } + + // If all checks passed, return true + return true; +} + +module.exports = validateCreditCard; + digitSum += Number(digit); + } + if (digitSum <= 16) { + return false; + } + + // If all checks passed, return true + return true; +} + +module.exports = validateCreditCard; \ No newline at end of file diff --git a/Sprint-3/4-stretch-investigate/card-validator.md b/Sprint-3/4-stretch-investigate/card-validator.md index fe5cdbde2..da0aa654d 100644 --- a/Sprint-3/4-stretch-investigate/card-validator.md +++ b/Sprint-3/4-stretch-investigate/card-validator.md @@ -34,36 +34,5 @@ These are the requirements your project needs to fulfill: Good luck! -function validateCreditCard(cardNumber) { - // cardNumber = cardNumber.replace(/\D/g, ""); - if (cardNumber.length !== 16 || !/^\d{16}$/.test(cardNumber)) { - return false; - } - // Check: Must contain at least 2 different digits - const sameDigits = new Set(cardNumber); - if (sameDigits.size < 2) { - return false; - } - // Check: Last digit must be even - const lastDigit = Number(cardNumber[cardNumber.length - 1]); - if (lastDigit % 2 !== 0) { - return false; - } - - // let digitSum = 0; - for (let digit of strNum) { - digitSum += Number(digit); - } // Convert each character to a number and add it to the sum - if (digitSum <= 16) { - return false; - } - - return true; // All validation rules passed - } - - // All checks passed; valid card - return true; - -module.exports = validateCreditCard; diff --git a/Sprint-3/4-stretch-investigate/password-validator.js b/Sprint-3/4-stretch-investigate/password-validator.js index 69b6ef37c..af26b2e92 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.js +++ b/Sprint-3/4-stretch-investigate/password-validator.js @@ -1,39 +1,29 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true -} -function passwordValidator(password) { - return password.length >= 5; -} -// Checks whether the password variable is a string - if (typeof password !== 'string') return false; +const previousPasswords = ["Password123!", "Welcome1$", "Admin#2023"]; // example previous passwords list - // Check length is at least a maximum of 5 characters long - if (password.length < 5) return true; - - // Check for at least one uppercase letter A-Z - if (!/[A-Z]/.test(password)) return false; +function passwordValidator(password) { + // Check that password is a string + if (typeof password !== "string") return false; - // Check for at least one lowercase letter a-z - if (!/[a-z]/.test(password)) return false; + // Check minimum length + if (password.length < 5) return false; - // Check for at least one number 0-9 - if (!/[0-9]/.test(password)) return false; + // Check for at least one uppercase letter + if (!/[A-Z]/.test(password)) return false; - // Check for at least one allowed special character: ! # $ % . * & - if (!/[!#$%.*&]/.test(password)) return false; + // Check for at least one lowercase letter + if (!/[a-z]/.test(password)) return false; - // Check that password is not in previousPasswords array - if (previousPasswords.includes(password)) return false; + // Check for at least one digit + if (!/[0-9]/.test(password)) return false; - // If all checks pass, return true - return true; + // Check for at least one special character from allowed set + if (!/[!#$%.*&]/.test(password)) return false; + // Check password not previously used + if (previousPasswords.includes(password)) return false; + // If all checks pass, password is valid + return true; +} - const hasUppercase = /[A-Z]/.test(password); - const hasLowercase = /[a-z]/.test(password); - const hasDigit = /[0-9]/.test(password); - const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password); - const hasNoSpaces = !/\s/.test(password); - -module.exports = passwordValidator; \ No newline at end of file +module.exports = passwordValidator; diff --git a/Sprint-3/4-stretch-investigate/password-validator.test.js b/Sprint-3/4-stretch-investigate/password-validator.test.js index 0d7a7dcae..db34f6954 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.test.js +++ b/Sprint-3/4-stretch-investigate/password-validator.test.js @@ -14,16 +14,10 @@ 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", () => { - // Arrange - const password = "12345"; - // Act - const result = isValidPassword(password); - // Assert - expect(result).toEqual(true); -} -); const previousPasswords = ["Password123!", "Welcome1$", "Admin#2023"]; + // Assert + + + const previousPasswords = ("Password123!", "Welcome1$", "Admin#2023"); function isValidPassword(password) { if (password.length < 5) { @@ -53,5 +47,4 @@ function isValidPassword(password) { return { valid: true }; } -module.exports = isValidPassword; - +module.exports = isValidPassword; \ No newline at end of file From c7ccd980908d69e6c604c5c8cdebba5960a11d85 Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Sat, 5 Jul 2025 16:37:12 +0100 Subject: [PATCH 08/11] card validator cheked and passed using npx --- Sprint-3/2-mandatory-rewrite/3-get-card-value.js | 6 ++---- Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) 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 883f9ee7e..e68038926 100644 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js +++ b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js @@ -1,7 +1,3 @@ -function getCardValue(card) { - // replace with your code from key-implement - return 11; -} function getCardValue(card) { const rank = card.slice(0, -1); // strip off the suit emoji @@ -15,3 +11,5 @@ function getCardValue(card) { throw new Error("Invalid card rank"); } + +module.exports = getCardValue; \ No newline at end of file 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 1a78c4c00..b8c060e97 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 @@ -4,6 +4,8 @@ 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 value of number cards (2-10)", () => { const testCases = [ @@ -28,10 +30,6 @@ test("should return 10 for face cards J, Q, K", () => { expect(getCardValue("K♥")).toEqual(10); }); -// Case 4: Handle Ace - const AceCard = getCardValue("A"); -assertEquals(AceCard ("A"), 11); - // Case 5: Handle Invalid Cards test("should throw error for invalid card Z♠", () => { expect(() => getCardValue("Z♠")).toThrow("Invalid card rank"); From 5981bd28506633ee86e4223a51fb04a51c7cb105 Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Tue, 8 Jul 2025 00:21:14 +0100 Subject: [PATCH 09/11] run, tested and passed npx test --- .../password-validator.test.js | 58 +++++++++---------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/Sprint-3/4-stretch-investigate/password-validator.test.js b/Sprint-3/4-stretch-investigate/password-validator.test.js index db34f6954..861a3cac3 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.test.js +++ b/Sprint-3/4-stretch-investigate/password-validator.test.js @@ -16,35 +16,29 @@ You must breakdown this problem in order to solve it. Find one test case first a */ // Assert - - const previousPasswords = ("Password123!", "Welcome1$", "Admin#2023"); - -function isValidPassword(password) { - if (password.length < 5) { - return { valid: false, message: "Password must be at least 5 characters long." }; - } - - if (!/[A-Z]/.test(password)) { - return { valid: false, message: "Password must include at least one uppercase letter." }; - } - - if (!/[a-z]/.test(password)) { - return { valid: false, message: "Password must include at least one lowercase letter." }; - } - - if (!/[0-9]/.test(password)) { - return { valid: false, message: "Password must include at least one number." }; - } - - if (!/[!#$%.*&]/.test(password)) { - return { valid: false, message: "Password must include at least one special symbol (!#$%.*&)." }; - } - - if (previousPasswords.includes(password)) { - return { valid: false, message: "This password has already been used. Please choose a new one." }; - } - - return { valid: true }; -} - -module.exports = isValidPassword; \ No newline at end of file +const passwordValidator= require("./password-validator"); + + test("Password must be at least 5 characters long.", () => { + expect(passwordValidator("Strong1!")).toBe(true); +}); + + test("Password must include at least one uppercase letter.", () =>{ + expect(passwordValidator("strong1")).toBe(false); + } ) + + test("Password must include at least one lowercase letter.", () =>{ + expect(passwordValidator("STRONG1")).toBe(false); + } ) + + test("Password must include at least one number.", () =>{ + expect(passwordValidator("Strong")).toBe(false); + } ) + + test("Password must include at least one special symbol (!#$%.*&).", () =>{ + expect(passwordValidator("Strong")).toBe(false); + } ) + + test("This password has already been used. Please choose a new one.", () =>{ + expect(passwordValidator("Password123")).toBe(false); + + }) From 989265d6b58b007edb1c43afc3d166662c249fb9 Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Tue, 8 Jul 2025 01:48:37 +0100 Subject: [PATCH 10/11] all tested and passed the jest test --- .../3-mandatory-practice/implement/count.js | 6 -- .../implement/count.test.js | 3 +- .../implement/get-ordinal-number.test.js | 62 +++++++++---------- ...-ordinal-number.js => getOrdinalNumber.js} | 4 -- .../3-mandatory-practice/implement/repeat.js | 12 ++-- .../implement/repeat.test.js | 3 +- .../4-stretch-investigate/card-validator.js | 32 ---------- 7 files changed, 37 insertions(+), 85 deletions(-) rename Sprint-3/3-mandatory-practice/implement/{get-ordinal-number.js => getOrdinalNumber.js} (88%) diff --git a/Sprint-3/3-mandatory-practice/implement/count.js b/Sprint-3/3-mandatory-practice/implement/count.js index bd26dda9c..5777801e9 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.js +++ b/Sprint-3/3-mandatory-practice/implement/count.js @@ -1,9 +1,3 @@ -function countChar(stringOfCharacters, findCharacter) { - return 5 -} - -module.exports = countChar; - function countChar(stringOfCharacters, findCharacter) { let count = 0; for (let char of stringOfCharacters) { diff --git a/Sprint-3/3-mandatory-practice/implement/count.test.js b/Sprint-3/3-mandatory-practice/implement/count.test.js index 8d5d152ee..12808648f 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.test.js +++ b/Sprint-3/3-mandatory-practice/implement/count.test.js @@ -1,5 +1,5 @@ // implement a function countChar that counts the number of times a character occurs in a string -const countChar = require("./count"); + // Given a string str and a single character char to search for, // When the countChar function is called with these inputs, // Then it should: @@ -9,6 +9,7 @@ const countChar = require("./count"); // And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'), // 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'). +const countChar = require("./count"); test("should count multiple occurrences of a character", () => { const str = "aaaaa"; 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 421cfd6e3..b53341af9 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 @@ -1,39 +1,35 @@ -const getOrdinalNumber = require("./get-ordinal-number"); // In this week's prep, we started implementing getOrdinalNumber // continue testing and implementing getOrdinalNumber for additional cases // Write your tests using Jest - remember to run your tests often for continual feedback -// Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" - -function getOrdinalNumber(num) { - // Handle invalid inputs gracefully (optional) - if (typeof num !== "number" || !Number.isInteger(num)) { - return "Invalid input"; - } - - // Handle the special case for teens (11, 12, 13) - const teenRemainder = num % 100; - if (teenRemainder >= 11 && teenRemainder <= 13) { - return num + "th"; - } - - // Get the last digit to determine suffix - const lastDigit = num % 10; - - switch (lastDigit) { - case 1: - return num + "st"; - case 2: - return num + "nd"; - case 3: - return num + "rd"; - default: - return num + "th"; - } -} - -module.exports = getOrdinalNumber; +const getOrdinalNumber = require("./getOrdinalNumber"); +test("returns 1st for 1", () => { + expect(getOrdinalNumber(1)).toBe("1st"); +}); + + test("returns 2nd for 2", () => { + expect(getOrdinalNumber(2)).toBe("2nd"); +}); + +test("returns 3rd for 3", () => { + expect(getOrdinalNumber(3)).toBe("3rd"); +}); + +test("returns 4th for 4", () => { + expect(getOrdinalNumber(4)).toBe("4th"); +}); + +test("Handle the special case for teens (11, 12, 13)", () => { + expect(getOrdinalNumber(11)).toBe("11th"); + expect(getOrdinalNumber(12)).toBe("12th"); + expect(getOrdinalNumber(13)).toBe("13th"); +}); + +test("Get the last digit to determine suffix", () => { + expect(getOrdinalNumber(21)).toBe("21st"); + expect(getOrdinalNumber(22)).toBe("22nd"); + expect(getOrdinalNumber(23)).toBe("23rd"); + expect(getOrdinalNumber(101)).toBe("101st"); +}); diff --git a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js b/Sprint-3/3-mandatory-practice/implement/getOrdinalNumber.js similarity index 88% rename from Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js rename to Sprint-3/3-mandatory-practice/implement/getOrdinalNumber.js index 2c851454a..9ce5a7cf3 100644 --- a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js +++ b/Sprint-3/3-mandatory-practice/implement/getOrdinalNumber.js @@ -1,6 +1,3 @@ -function getOrdinalNumber(num) { - return "1st"; -} function getOrdinalNumber(num) { const lastDigit = num % 10; const lastTwoDigits = num % 100; @@ -20,5 +17,4 @@ function getOrdinalNumber(num) { return num + "th"; } } - module.exports = getOrdinalNumber; diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.js b/Sprint-3/3-mandatory-practice/implement/repeat.js index d2b8f0c23..3b7a40ffe 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.js @@ -1,11 +1,9 @@ -function repeat() { - return "hellohellohello"; -} - -module.exports = repeat; +function repeat(str = "", count = 1) { + if (count < 0) { + throw new Error("Invalid count"); + } -function repeat(word = "hello", times = 3) { - return word.repeat(times); + return str.repeat(count); } 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 ed6ca3157..d2d675533 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.test.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.test.js @@ -30,7 +30,6 @@ test("should repeat the string count times", () => { // 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. -const repeat = require("./repeat"); // Case: Repeat string multiple times test("should repeat the string count times", () => { @@ -60,5 +59,5 @@ test("should return an empty string if count is 0", () => { test("should throw an error if count is negative", () => { const str = "hello"; const count = -2; - expect(() => repeat(str, count)).toThrow("Invalid "); + expect(() => repeat(str, count)).toThrow("Invalid count"); }); diff --git a/Sprint-3/4-stretch-investigate/card-validator.js b/Sprint-3/4-stretch-investigate/card-validator.js index c69e7ad0d..ee1d5ea3e 100644 --- a/Sprint-3/4-stretch-investigate/card-validator.js +++ b/Sprint-3/4-stretch-investigate/card-validator.js @@ -16,27 +16,6 @@ function validateCreditCard(cardNumber) { return false; } - // Check sum of digits - let digitSum = 0; - for (let digit of cardNumber) { -function validateCreditCard(cardNumber) { - // Check length and numeric characters - if (cardNumber.length !== 16 || !/^\d{16}$/.test(cardNumber)) { - return false; - } - - // Check at least two different digits - const uniqueDigits = new Set(cardNumber); - if (uniqueDigits.size < 2) { - return false; - } - - // Check last digit is even - const lastDigit = Number(cardNumber[cardNumber.length - 1]); - if (lastDigit % 2 !== 0) { - return false; - } - // Check sum of digits let digitSum = 0; for (let digit of cardNumber) { @@ -51,14 +30,3 @@ function validateCreditCard(cardNumber) { } module.exports = validateCreditCard; - digitSum += Number(digit); - } - if (digitSum <= 16) { - return false; - } - - // If all checks passed, return true - return true; -} - -module.exports = validateCreditCard; \ No newline at end of file From d465d6a9ef808a62de889b4864d289f142d4b0e0 Mon Sep 17 00:00:00 2001 From: mayowa0-7 Date: Thu, 10 Jul 2025 22:23:08 +0100 Subject: [PATCH 11/11] card value function fixed --- Sprint-3/2-mandatory-rewrite/3-get-card-value.js | 11 +++++------ .../4-stretch-investigate/password-validator.js | 2 +- .../password-validator.test.js | 15 +++++++++------ 3 files changed, 15 insertions(+), 13 deletions(-) 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 e68038926..22d4c5998 100644 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js +++ b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js @@ -4,12 +4,11 @@ function getCardValue(card) { if (rank === "A") return 11; if (["K", "Q", "J", "10"].includes(rank)) return 10; - const number = parseInt(rank); - if (!isNaN(number) && number >= 2 && number <= 9) { - return number; + // Accept only exact digits 2 through 9 + if (/^[2-9]$/.test(rank)) { + return Number(rank); } - throw new Error("Invalid card rank"); } - -module.exports = getCardValue; \ No newline at end of file +module.exports = getCardValue; + \ No newline at end of file diff --git a/Sprint-3/4-stretch-investigate/password-validator.js b/Sprint-3/4-stretch-investigate/password-validator.js index af26b2e92..9ebd413b8 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.js +++ b/Sprint-3/4-stretch-investigate/password-validator.js @@ -1,9 +1,9 @@ const previousPasswords = ["Password123!", "Welcome1$", "Admin#2023"]; // example previous passwords list function passwordValidator(password) { + // Check that password is a string if (typeof password !== "string") return false; - // Check minimum length if (password.length < 5) return false; diff --git a/Sprint-3/4-stretch-investigate/password-validator.test.js b/Sprint-3/4-stretch-investigate/password-validator.test.js index 861a3cac3..7e91b3c36 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.test.js +++ b/Sprint-3/4-stretch-investigate/password-validator.test.js @@ -19,11 +19,14 @@ You must breakdown this problem in order to solve it. Find one test case first a const passwordValidator= require("./password-validator"); test("Password must be at least 5 characters long.", () => { - expect(passwordValidator("Strong1!")).toBe(true); + expect(passwordValidator("St1!")).toBe(false); +}); + + test("password with exactly 5 valid characters long", () => { + expect(isValidPassword("Stg1!")).toBe(true); }); - test("Password must include at least one uppercase letter.", () =>{ - expect(passwordValidator("strong1")).toBe(false); + expect(passwordValidator("strong1!")).toBe(false); } ) test("Password must include at least one lowercase letter.", () =>{ @@ -31,14 +34,14 @@ const passwordValidator= require("./password-validator"); } ) test("Password must include at least one number.", () =>{ - expect(passwordValidator("Strong")).toBe(false); + expect(passwordValidator("Strong*")).toBe(false); } ) test("Password must include at least one special symbol (!#$%.*&).", () =>{ - expect(passwordValidator("Strong")).toBe(false); + expect(passwordValidator("Strong2")).toBe(false); } ) test("This password has already been used. Please choose a new one.", () =>{ - expect(passwordValidator("Password123")).toBe(false); + expect(passwordValidator("Password123!")).toBe(false); })