diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..433c72934 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -20,4 +20,4 @@ console.log(`The base part of ${filePath} is ${base}`); const dir = ; const ext = ; -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +// https://www.google.com/search?q=slice+mdn 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..d7867ba4b 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,16 @@ // 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"; + // read to the end, complete line 36, then pass your test here + // if the angle is less than 90, return "Acute angle" + if (angle < 90) return "Acute angle"; + // if the angle is greater than 90 and less than 180, return "Obtuse angle" + if (angle > 90 && angle < 180) return "Obtuse angle"; + // if the angle is exactly 180, return "Straight angle" + if (angle === 180) return "Straight angle"; + // if the angle is greater than 180 and less than 360, return "Reflex angle" + if (angle > 180 && angle < 360) return "Reflex angle"; } // we're going to use this helper function to make our assertions easier to read @@ -20,7 +28,7 @@ function assertEquals(actualOutput, targetOutput) { `Expected ${actualOutput} to equal ${targetOutput}` ); } - +console.log("hello"); // Acceptance criteria: // Given an angle in degrees, @@ -43,14 +51,19 @@ assertEquals(acute, "Acute angle"); // 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: // 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 straightAngle = getAngleType(180); +assertEquals(straightAngle, "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-key-implement/2-is-proper-fraction.js b/Sprint-3/1-key-implement/2-is-proper-fraction.js index 91583e941..25112cb4a 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,8 @@ // write one test at a time, and make it pass, build your solution up methodically function isProperFraction(numerator, denominator) { - if (numerator < denominator) return true; + // if (numerator < denominator) return true; it will not work for negative numbers + return Math.abs(numerator) < Math.abs(denominator); } // here's our helper again @@ -41,6 +42,7 @@ assertEquals(improperFraction, false); // 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 @@ -48,6 +50,20 @@ const negativeFraction = isProperFraction(-4, 7); // 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? + +// Zero Numerator check: +// Input: numerator = 0, denominator = 5 +// target output: true +// Explanation: The fraction 0/5 is a proper fraction because the numerator (0) is less than the denominator (5). The function should return true. +const zeroNumerator = isProperFraction(0, 5); +assertEquals(zeroNumerator, true); + +// Zero Denominator check: +// Input: numerator = 4, denominator = 0 +// target output: false +// Explanation: A fraction with a zero denominator is undefined. The function should return false to indicate it's not a valid proper fraction. +const zeroDenominator = isProperFraction(4, 0); +assertEquals(zeroDenominator, false); 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..7bebb6e1a 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,22 @@ // 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); // Extract the rank from the card string + + if (rank === "A") return 11; + + // Face cards and 10 are worth 10 points + if (["10", "J", "Q", "K"].includes(rank)) { + return 10; + } + // Convert rank to number and check if it's a valid number card + const numericRank = Number(rank); + if (!isNaN(numericRank)) { + return numericRank; + } + // If rank is not valid, throw an error + throw new Error("Invalid card rank."); + // Note: The function assumes the input is always a valid card string } // You need to write assertions for your function to check it works in different cases @@ -34,18 +49,40 @@ assertEquals(aceofSpades, 11); // 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); // 5 of Hearts should return 5 but still we need to implement this in the function // 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 tenOfDiamonds = getCardValue("10♦"); +assertEquals(tenOfDiamonds, 10); // 10 of Diamonds should return 10 +const jackOfClubs = getCardValue("J♣"); +assertEquals(jackOfClubs, 10); // Jack of Clubs should return 10 +const queenOfSpades = getCardValue("Q♠"); +assertEquals(queenOfSpades, 10); // Queen of Spades should return 10 +const kingOfHearts = getCardValue("K♥"); +assertEquals(kingOfHearts, 10); // King of Hearts should return 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 aceOfDiamonds = getCardValue("A♦"); +assertEquals(aceOfDiamonds, 11); // Ace of Diamonds should return 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." +// Invalid card test (should throw error) +try { + getCardValue("Z♠"); + console.assert(false, "Expected error for invalid rank"); +} catch (e) { + console.assert( + e.message === "Invalid card rank.", + `Unexpected error message: ${e.message}` + ); +} 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..d2e6b06e2 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js +++ b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js @@ -1,18 +1,33 @@ function getAngleType(angle) { - if (angle === 90) return "Right angle"; // replace with your completed function from key-implement + // Step 1: Check for a right angle (exactly 90 degrees) + if (angle === 90) return "Right angle"; -} - - - - + // Step 2: Check for a straight angle (exactly 180 degrees) + if (angle === 180) return "Straight angle"; + // Step 3: Check for an acute angle (between 0 and 90) + if (angle > 0 && angle < 90) return "Acute Angle"; + // Step 4: Check for an obtuse angle (between 90 and 180) + if (angle > 90 && angle < 180) return "Obtuse angle"; + // Step 5: Check for a reflex angle (between 180 and 360) + if (angle > 180 && angle < 360) return "Reflex angle"; + // Step 6: Handle invalid or unsupported input + return "Invalid 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 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 +module.exports = getAngleType; + +// console.log(getAngleType(90)); // "Right angle" +// console.log(getAngleType(180)); // "Straight angle" +// console.log(getAngleType(45)); // "Acute angle" +// console.log(getAngleType(120)); // "Obtuse angle" +// console.log(getAngleType(0)); // "Invalid angle" +// console.log(getAngleType(200)); // "Invalid angle" +// console.log(getAngleType(90.5)); // "Invalid angle" 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..2d41f61ea 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 @@ -1,24 +1,31 @@ -const getAngleType = require("./1-get-angle-type"); +const getAngleType = require("./1-get-angle-type"); // Import the function to be tested 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 - // 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 angle (>90 && <180)", () => { + 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 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 Straight Angle (> 180 && < 360)", () => { + expect(getAngleType(278)).toEqual("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..90315e03c 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,19 @@ function isProperFraction(numerator, denominator) { - if (numerator < denominator) return true; - // add your completed function from key-implement here + if (denominator === 0) return false; // Avoid division by zero + if (numerator < 0 || denominator < 0) return false; // Proper fractions are positive + if (numerator < denominator) return true; // Proper if numerator < denominator + return false; // Otherwise, it's improper } -module.exports = isProperFraction; \ No newline at end of file +module.exports = isProperFraction; +// Test cases for the isProperFraction function +console.log(isProperFraction(1, 2)); // true +console.log(isProperFraction(3, 4)); // true +console.log(isProperFraction(5, 5)); // false +console.log(isProperFraction(7, 6)); // false +console.log(isProperFraction(0, 1)); // true +console.log(isProperFraction(1, 0)); // false (denominator is zero) +console.log(isProperFraction(-1, 2)); // false (negative numerator) +console.log(isProperFraction(1, -2)); // false (negative denominator) +console.log(isProperFraction(-1, -2)); // false (both negative) +console.log(isProperFraction(2, 3)); // true 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..022627492 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 @@ -1,11 +1,27 @@ -const isProperFraction = require("./2-is-proper-fraction"); +function isProperFraction(numerator, denominator) { + // Case 1: Check for zero denominator (undefined) + if (denominator === 0) return false; -test("should return true for a proper fraction", () => { - expect(isProperFraction(2, 3)).toEqual(true); -}); + // Case 2: Check for negative values (numerator or denominator) + if (numerator < 0 || denominator < 0) return false; -// Case 2: Identify Improper Fractions: + // Case 3: Check for equal numerator and denominator + if (numerator === denominator) return false; -// Case 3: Identify Negative Fractions: + // Case 4: Check for proper fraction (numerator < denominator) + return numerator < denominator; +} +module.exports = isProperFraction; +// usage examples +console.log(isProperFraction(2, 3)); // true +console.log(isProperFraction(5, 3)); // false +console.log(isProperFraction(3, 3)); // false +console.log(isProperFraction(-2, 3)); // false +console.log(isProperFraction(2, -3)); // false +console.log(isProperFraction(0, 5)); // true (0 < 5) +console.log(isProperFraction(2, 2)); // false -// Case 4: Identify Equal Numerator and Denominator: +// Test cases for the isProperFraction function +// test("should return true for a proper fraction", () => { +// expect(isProperFraction(2, 3)).toEqual(true); +// }); 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..41c123a75 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,20 @@ function getCardValue(card) { - // replace with your code from key-implement - return 11; + // replace with your code from key-implement + // return 11; // always return 11 for testing purposes that is wrong + const rank = card.slice(0, -1); // Extract rank (everything except the last character) + if (rank === "A") return 11; // handle Ace as 11 + if (["K", "Q", "J"].includes(rank)) return 10; // handle face cards as 10 + const numerincRank = parseInt(rank); //Handle number cards 2–9 + if (numerincRank >= 2 && numerincRank <= 9) { + return numerincRank; // Return the numeric value for cards 2-9 + } + // Invalid card rank + return 0; } -module.exports = getCardValue; \ No newline at end of file +module.exports = getCardValue; + +// // Test cases +console.log(getCardValue("A♠")); // 11 +console.log(getCardValue("7♥")); // 7 +console.log(getCardValue("K♦")); // 10 +console.log(getCardValue("A♣")); // 11 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..a82316add 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,27 @@ const getCardValue = require("./3-get-card-value"); test("should return 11 for Ace of Spades", () => { - const aceofSpades = getCardValue("A♠"); - expect(aceofSpades).toEqual(11); - }); + const aceofSpades = getCardValue("A♠"); + expect(aceofSpades).toEqual(11); +}); // Case 2: Handle Number Cards (2-10): +test("should return 7 for 7 of Hearts", () => { + const sevenOfHearts = getCardValue("7♥"); + expect(sevenOfHearts).toEqual(7); +}); // Case 3: Handle Face Cards (J, Q, K): +test("should return 10 for King of Diamonds", () => { + const kingOfDiamonds = getCardValue("K♦"); + expect(kingOfDiamonds).toEqual(10); +}); // Case 4: Handle Ace (A): +test("should return 11 for Ace of Clubs", () => { + const aceOfClubs = getCardValue("A♣"); + expect(aceOfClubs).toEqual(11); +}); // Case 5: Handle Invalid Cards: +test("should return 0 for invalid card 'X♠'", () => { + const invalidCard = getCardValue("X♠"); + expect(invalidCard).toEqual(0); +}); diff --git a/Sprint-3/3-mandatory-practice/implement/count.js b/Sprint-3/3-mandatory-practice/implement/count.js index fce249650..2b056aa25 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 + // return 5; //This will always return 5, regardless of the inputs. So it’s just a placeholder. + return stringOfCharacters.split(findCharacter).length - 1; } -module.exports = countChar; \ No newline at end of file +module.exports = countChar; + +// console.log(countChar("hello", "l")); // 2 +// console.log(countChar("hello world", "o")); // 2 +// console.log(countChar("banana", "a")); // 3 +// console.log(countChar("mississippi", "i")); // 5 diff --git a/Sprint-3/3-mandatory-practice/implement/count.test.js b/Sprint-3/3-mandatory-practice/implement/count.test.js index 42baf4b4b..165467d59 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.test.js +++ b/Sprint-3/3-mandatory-practice/implement/count.test.js @@ -22,3 +22,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 the character is not present", () => { + const str = "aaaaa"; + const char = "z"; // 'z' does not exist in "aaaaa" + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +module.exports = countChar; 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..2c5d0e882 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,28 @@ function getOrdinalNumber(num) { - return "1st"; + // return "1st"; //always returns "1st" no matter what input is provided + const lastTwo = num % 100; + const lastDigit = num % 10; + if (lastTwo >= 11 && lastTwo <= 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; -module.exports = getOrdinalNumber; \ No newline at end of file +// console.log(getOrdinalNumber(1)); // "1st" +// console.log(getOrdinalNumber(2)); // "2nd" +// console.log(getOrdinalNumber(3)); // "3rd" +// console.log(getOrdinalNumber(4)); // "4th" +// console.log(getOrdinalNumber(51)); // "51st" +// console.log(getOrdinalNumber(102)); // "102nd" +// console.log(getOrdinalNumber(1114)); // "1114th" 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..dfa8e44b6 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 @@ -9,5 +9,6 @@ const getOrdinalNumber = require("./get-ordinal-number"); // Then the function should return "1st" test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); - }); + expect(getOrdinalNumber(1)).toEqual("1st"); +}); +console.log(getOrdinalNumber(1)); diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.js b/Sprint-3/3-mandatory-practice/implement/repeat.js index 621f9bd35..ace324344 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.js @@ -1,5 +1,19 @@ -function repeat() { - return "hellohellohello"; +// function repeat() { +// return "hellohellohello"; +// } +// The function always returns "hellohellohello" regardless of any input. +// There are no parameters, so it’s not flexible or reusable. +function repeat(str, count) { + if (!Number.isInteger(count) || count < 0) { + return "Error: Count must be a non-negative integer"; + } + return str.repeat(count); } -module.exports = repeat; \ No newline at end of file +module.exports = repeat; +// Example usage: +// console.log(repeat("hello", 3)); // Expected: "hellohellohello" +// console.log(repeat("at", 5)); // Expected: "atatatatat" +// console.log(repeat("test0", 0)); // Expected: "" +// console.log(repeat("", 5)); // Expected: "" +// console.log(repeat("WE", 1)); // Expected: "hi" diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.test.js b/Sprint-3/3-mandatory-practice/implement/repeat.test.js index 8a4ab42ef..2df825f68 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.test.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.test.js @@ -9,24 +9,25 @@ const repeat = require("./repeat"); // When the repeat function is called with these inputs, // Then it should repeat the str count times and return a new string containing the repeated str values. -test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("hellohellohello"); - }); +// 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: // 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. - +console.log(repeat("str", 1)); // Expected: "str" // 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. - +console.log(repeat("hello", 0)); // Expected: " " // 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. +console.log(repeat("hello", -3)); // Expected: "Error: Count must be a non-negative integer" diff --git a/Sprint-3/4-stretch-investigate/find.js b/Sprint-3/4-stretch-investigate/find.js index c7e79a2f2..4e16197dc 100644 --- a/Sprint-3/4-stretch-investigate/find.js +++ b/Sprint-3/4-stretch-investigate/find.js @@ -20,6 +20,11 @@ console.log(find("code your future", "z")); // Pay particular attention to the following: // a) How the index variable updates during the call to find +// increments by 1 in every iteration using index++. // b) What is the if statement used to check +// checks if the character at the current index matches the specified character. // c) Why is index++ being used? +// It moves the pointer forward through the string. Without it, the loop would run infinitely. + // d) What is the condition index < str.length used for? +// It ensures we don’t read beyond the string boundary — this prevents errors or undefined behavior. diff --git a/Sprint-3/4-stretch-investigate/password-validator.js b/Sprint-3/4-stretch-investigate/password-validator.js index b55d527db..381851b87 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.js +++ b/Sprint-3/4-stretch-investigate/password-validator.js @@ -1,6 +1,32 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true -} +// function passwordValidator(password) { +// return password.length < 5 ? false : true; +// } + +// module.exports = passwordValidator; +const previousPasswords = ["Password123!", "Welcome2023$", "Admin*1"]; +function isValidPassword(password) { + const hasMinLength = password.length >= 5; + const hasUppercase = /[A-Z]/.test(password); + const hasLowercase = /[a-z]/.test(password); + const hasDigit = /[0-9]/.test(password); + const hasSymbol = /[!#$%.*&]/.test(password); + const isNotPrevious = !previousPasswords.includes(password); + + return ( + hasMinLength && + hasUppercase && + hasLowercase && + hasDigit && + hasSymbol && + isNotPrevious + ); +} -module.exports = passwordValidator; \ No newline at end of file +module.exports = isValidPassword; +// Example usage: +// console.log(isValidPassword("Abdi10!")); // true +// console.log(isValidPassword("12345")); // false +// console.log(isValidPassword("123")); // false +// console.log(isValidPassword("abcde")); // false +// console.log(isValidPassword("")); // false diff --git a/Sprint-3/4-stretch-investigate/password-validator.test.js b/Sprint-3/4-stretch-investigate/password-validator.test.js index 8fa3089d6..d67f22d2d 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.test.js +++ b/Sprint-3/4-stretch-investigate/password-validator.test.js @@ -16,11 +16,16 @@ You must breakdown this problem in order to solve it. Find one test case first a */ 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); -} -); \ No newline at end of file + // Arrange + const password = "12345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +}); +expect(result).toEqual(true); +const isValidPassword = require("./password-validator"); +// // usage example +// console.log(isValidPassword("Test123!")); // true +// console.log(isValidPassword("pass")); // false (too short) +// console.log(isValidPassword("password")); // false (no number or special character)