diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..2d3ee02f1 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -10,4 +10,6 @@ function capitalise(str) { } // =============> write your explanation here -// =============> write your new code here + //Variable str is passed as parametr in line 7. There is no need to create variable str again in line +// =============> write your new code here + // line 8: str = `${str[0].toUpperCase()}${str.slice(1)}`; diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..dbacf10d3 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,8 +1,8 @@ // Predict and explain first... - + // the code won't work // Why will an error occur when this program runs? // =============> write your prediction here - + // because of variable decimaNumber is already declared. And out of range of conlole function // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -18,3 +18,14 @@ console.log(decimalNumber); // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + console.log(decimalNumber); + return percentage; +} + +convertToPercentage(54); + + diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..ee2aa50de 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,21 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here - + //function expect num variable and gets 3 of number. function square(3) { return num * num; } // =============> write the error message here + //SyntaxError: Unexpected number // =============> explain this error message here - + //function expect num variable and gets 3 of number. // Finally, correct the code to fix the problem - + // change 3 to num // =============> write your new code here + function square(num) { + return num * num; +} diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..5a8ea8443 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -12,3 +12,11 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // Finally, correct the code to fix the problem // =============> write your new code here + +function multiply(a, b) { + return a * b; +} +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + + + diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..90ec7a90d 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -8,6 +8,13 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here: incorrect return statment // Finally, correct the code to fix the problem // =============> write your new code here + +function sum(a, b) { + return a + b; + +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..44e2cae0c 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,40 @@ // Predict and explain first... - + //it won't work // Predict the output of the following code: // =============> Write your prediction here + // I've just checked +//const num = 103; -const num = 103; +//function getLastDigit() { + //return num.toString().slice(-1); +//} -function getLastDigit() { - return num.toString().slice(-1); -} - -console.log(`The last digit of 42 is ${getLastDigit(42)}`); -console.log(`The last digit of 105 is ${getLastDigit(105)}`); -console.log(`The last digit of 806 is ${getLastDigit(806)}`); +//console.log(`The last digit of 42 is ${getLastDigit(42)}`); +//console.log(`The last digit of 105 is ${getLastDigit(105)}`); +//console.log(`The last digit of 806 is ${getLastDigit(806)}`); // Now run the code and compare the output to your prediction -// =============> write the output here +// =============> write the output here +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 // Explain why the output is the way it is // =============> write your explanation here + // because of const num=103 // Finally, correct the code to fix the problem // =============> write your new code here // This program should tell the user the last digit of each number. + + + +function getLastDigit(num) { + return num.toString().slice(-1); +} + +console.log(`The last digit of 42 is ${getLastDigit(42)}`); +console.log(`The last digit of 105 is ${getLastDigit(105)}`); +console.log(`The last digit of 806 is ${getLastDigit(806)}`); + // Explain why getLastDigit is not working properly - correct the problem + //function getLastDigit(num) - should be like that diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..51ab5f44b 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,10 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + let BMI = weight/(height*height); + return parseFloat(BMI.toFixed(2)); + // return the BMI of someone based off their weight and height -} \ No newline at end of file + // yes it better to be number in case we want to use it in some other calculations +} +console.log(calculateBMI(62.99, 1.64)); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..260d48459 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,9 @@ // You will need to come up with an appropriate name for the function // Use the MDN string documentation to help you find a solution // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase + function to_upper_snacke_case(str){ + str = str.replaceAll(" ", "_"); + str = str.toUpperCase(); + return str; + } + console.log(to_upper_snacke_case("lord of the rings")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..6feb11757 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,34 @@ // You will need to declare a function called toPounds with an appropriately named parameter. // You should call this function a number of times to check it works for different inputs + +//const penceString = "399p"; + +function toPounds(penceString) { + const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 + ); + + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + console.log(`£${pounds}.${pence}`); + + + +} +toPounds("4896p"); + + + + + + diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..d46f123fe 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -7,28 +7,28 @@ function formatTimeDisplay(seconds) { const totalMinutes = (seconds - remainingSeconds) / 60; const remainingMinutes = totalMinutes % 60; const totalHours = (totalMinutes - remainingMinutes) / 60; - return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(61)); // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit // to help you answer these questions // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> write your answer here: tree times. // Call formatTimeDisplay with an input of 61, now answer the following: // b) What is the value assigned to num when pad is called for the first time? -// =============> write your answer here +// =============> write your answer here: 0 // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here: '00' - string of full hours // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> write your answer here +// =============> write your answer here: 1 - Number of seconds remainder // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> write your answer here +// =============> write your answer here: '01' - String of seconds remainder diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..18247e6e5 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,8 +4,13 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(-2); if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}:${minutes} pm`; + } else if ( hours == 0 ){ + return `12:${minutes} am`; + } else if ( hours == 12 ){ + return `12:${minutes} pm`; } return `${time} am`; } @@ -23,3 +28,36 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +////////////// +// hours > 12 and minutes NOT 00 +const currentOutput3 = formatAs12HourClock("23:45"); +const targetOutput3 = "11:45 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +// hours < 12 and minutes NOT 00 +const currentOutput4 = formatAs12HourClock("09:13"); +const targetOutput4 = "09:13 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +// hours = 00 and minutes = 00 +const currentOutput5 = formatAs12HourClock("00:00"); +const targetOutput5 = "12:00 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + +// hours = 12 and minutes = 00 +const currentOutput6 = formatAs12HourClock("12:00"); +const targetOutput6 = "12:00 pm"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); 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..c2fa33231 100644 --- a/Sprint-3/1-key-implement/1-get-angle-type.js +++ b/Sprint-3/1-key-implement/1-get-angle-type.js @@ -9,7 +9,10 @@ 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 "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 @@ -43,14 +46,16 @@ 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); -// ====> 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(200); +assertEquals(reflex, "Reflex angle"); \ No newline at end of file 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..7570dd41e 100644 --- a/Sprint-3/1-key-implement/2-is-proper-fraction.js +++ b/Sprint-3/1-key-implement/2-is-proper-fraction.js @@ -8,8 +8,12 @@ // 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; + } else if ( numerator >= denominator) { + return false; + } +} // here's our helper again function assertEquals(actualOutput, targetOutput) { @@ -40,6 +44,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(negativeFraction,true ); // ====> complete with your assertion // Equal Numerator and Denominator check: @@ -47,6 +52,7 @@ const negativeFraction = isProperFraction(-4, 7); // 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); +assertEquals(equalFraction, false); // ====> complete with your assertion // Stretch: 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..79957c77d 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; + let rank; + if (card === "10"){ + rank = card; + } else { + rank = card.charAt(0); + } + + if (rank === "A") { + return 11; + } else if (Number(rank) >= 2 && Number(rank) <= 9) { + return Number(rank); + } else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") { + return 10; + } else { + throw new Error("Invalid card rank"); + } } // You need to write assertions for your function to check it works in different cases @@ -33,19 +48,30 @@ 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 faceCard = getCardValue("10"); +assertEquals(faceCard, 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 handleAce = getCardValue("A"); +assertEquals(handleAce, 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." +try { + const invalidCard = getCardValue("g"); + assertEquals(invalidCard, "Invalid card rank."); +} catch(error){ + console.log(error.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..830f5a476 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,11 @@ 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"; } - - - - - - - // 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..17b156e38 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 @@ -7,18 +7,18 @@ test("should identify right angle (90°)", () => { // 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 angle (< 90°)", () => { + expect(getAngleType(40)).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° and < 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 reflex angle (> 180° and < 360°)", () => { + expect(getAngleType(200)).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..a11d8a038 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,9 @@ function isProperFraction(numerator, denominator) { - if (numerator < denominator) return true; - // add your completed function from key-implement here + if (numerator < denominator) { + return true; + } else if ( numerator >= denominator) { + 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..04df4f3b9 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 true for a improper fraction", () => { + expect(isProperFraction(5, 3)).toEqual(false); +}); // Case 3: Identify Negative Fractions: +test("should return true for a negative fraction", () => { + expect(isProperFraction(-2, 3)).toEqual(true); +}); // Case 4: Identify Equal Numerator and Denominator: +test("should return true for a equal fraction", () => { + 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..1f1a44265 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,18 @@ function getCardValue(card) { - // replace with your code from key-implement + let rank; + if (card === "10"){ + rank = card; + } else { + rank = card.charAt(0); + } + if (rank === "A") { return 11; + } else if (Number(rank) >= 2 && Number(rank) <= 9) { + return Number(rank); + } else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") { + return 10; + } else { + 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 03a8e2f34..810f292fe 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 @@ -3,9 +3,28 @@ 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): +test("should return rank for Number Cards (2-10)", () => { + const numberCards = getCardValue("7♠"); + expect(numberCards).toEqual(7); +}); + // Case 3: Handle Face Cards (J, Q, K): +test("should return 10 Face Cards (J, Q, K)", () => { + const faceCards = getCardValue("J"); + expect(faceCards).toEqual(10); +}); + // Case 4: Handle Ace (A): +test("should return 11 for Ace (A)", () => { + const aceCard = getCardValue("A"); + expect(aceCard).toEqual(11); +}); + // Case 5: Handle Invalid Cards: +test("should return error for invalid Cards", () => { + const numberCards = getCardValue("Z"); + expect(numberCards).toEqual("Invalid card rank."); +}); \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/count.js b/Sprint-3/3-mandatory-practice/implement/count.js index fce249650..1011fdb31 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.js +++ b/Sprint-3/3-mandatory-practice/implement/count.js @@ -1,5 +1,9 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (char in 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/count.test.js b/Sprint-3/3-mandatory-practice/implement/count.test.js index 42baf4b4b..556ff9d05 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.test.js +++ b/Sprint-3/3-mandatory-practice/implement/count.test.js @@ -22,3 +22,9 @@ 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 count multiple occurrences of a character", () => { + const str = "aaaaa"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); 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..e8ff4654f 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,5 @@ function getOrdinalNumber(num) { - return "1st"; + return "${num}st"; } module.exports = getOrdinalNumber; \ No newline at end of file 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..380adce6d 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 @@ -10,4 +10,4 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); - }); +}); diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.js b/Sprint-3/3-mandatory-practice/implement/repeat.js index 621f9bd35..224bb1a62 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.js @@ -1,5 +1,9 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, num) { + if (num == 0 ) { + return ''; + } else { + return str.repeat(num); + } } module.exports = repeat; \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.test.js b/Sprint-3/3-mandatory-practice/implement/repeat.test.js index 8a4ab42ef..d2eb687c3 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.test.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.test.js @@ -14,19 +14,37 @@ test("should repeat the string count times", () => { 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. +test("should repeat the string count times", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).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 repeat the string count times", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); // case: Negative Count: // Given a target string str and a negative integer count, // When the repeat function is called with these inputs, // Then it should throw an error or return an appropriate error message, as negative counts are not valid. +test("should repeat the string count times", () => { + const str = "hello"; + const count = -1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); \ 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 b55d527db..964cbfa0e 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.js +++ b/Sprint-3/4-stretch-investigate/password-validator.js @@ -1,6 +1,17 @@ function passwordValidator(password) { - return password.length < 5 ? false : true + if ( password.length < 5 ) { + return false; + } else if (password.search[/A-Z/i] < 0){ + return false; + } else if (password.search[/a-z/i] < 0){ + return false; + } else if (password.search[/0-9/] < 0 ){ + return false; + } else if (password.search[/!@#$%&*_?-/] < 0 ){ + return false; + } else { + return true; + } } - 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..5a712c4be 100644 --- a/Sprint-3/4-stretch-investigate/password-validator.test.js +++ b/Sprint-3/4-stretch-investigate/password-validator.test.js @@ -14,13 +14,30 @@ 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"); +//const isValidPassword = require("./password-validator"); + + +function passwordValidator(password) { + if ( password.length < 5 ) { + return false; + } else if (password.search[/A-Z/i] < 0){ + return false; + } else if (password.search[/a-z/i] < 0){ + return false; + } else if (password.search[/0-9/] < 0 ){ + return false; + } else if (password.search[/!@#$%&*_?-/] < 0){ + return false; + } else { + return true; + } +} + 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 + expect(result).toEqual(false); +}); \ No newline at end of file