diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..e647ba0b8 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -3,4 +3,4 @@ let count = 0; count = count + 1; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 -// Describe what line 3 is doing, in particular focus on what = is doing +// Describe what line 3 is doing, in particular focus on what = is doing --> = means a assignment on itself +1 diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..611678dc0 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -7,5 +7,8 @@ let lastName = "Johnson"; let initials = ``; +initials = `${firstName[0]}, ${middleName[0]}, ${lastName[0]}`; + // https://www.google.com/search?q=get+first+character+of+string+mdn +//console.log(initials); diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..852b0a8a9 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,18 @@ console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = ; -const ext = ; +const dir = filePath.slice(0, filePath.lastIndexOf("/")); +const ext = filePath.slice(filePath.lastIndexOf(".") + 1); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +console.log(dir); +console.log(ext); + +/* +Explanation: + +filePath.lastIndexOf(".") finds the position of the last dot in the path. + +slice() extracts the substring starting one position after the dot (hence +1), giving just the extension without the dot. +*/ + +// https://www.google.com/search?q=slice+mdn diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..6d68fa228 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,7 +3,10 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; -// In this exercise, you will need to work out what num represents? -// Try breaking down the expression and using documentation to explain what it means -// It will help to think about the order in which expressions are evaluated +// In this exercise, you will need to work out what num represents? --> num gives a random number between 1 and 100 +// Try breaking down the expression and using documentation to explain what it means --> Math.floor() give the rounded number below +// --> Math.random give a number between 0 and 1 but never 1 +// It will help to think about the order in which expressions are evaluated --> first what is in parenthesis // Try logging the value of num and running the program several times to build an idea of what the program is doing + +console.log(num); diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..d68d870e5 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,2 @@ This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +We don't want the computer to run these 2 lines - how can we solve this problem? --> with /* */ \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..22570713c 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,4 @@ const age = 33; age = age + 1; +// --> let age =33; instead of using const diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..fe9df0797 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,4 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +//--> cityOfBirth must be declared before the console.log of cityOfBirth diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..d42223e4d 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -7,3 +7,5 @@ const last4Digits = cardNumber.slice(-4); // Then run the code and see what error it gives. // Consider: Why does it give this error? Is this what I predicted? If not, what's different? // Then try updating the expression last4Digits is assigned to, in order to get the correct value +console.log(last4Digits); +//--> .slice() works only with a string --> add '4533' diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..161b4bd9b 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,4 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; + +//--> we mustn't begin a name's variable with integer \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..5558fcb15 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -1,5 +1,5 @@ -let carPrice = "10,000"; -let priceAfterOneYear = "8,543"; +let carPrice = 10000; +let priceAfterOneYear = 8543; carPrice = Number(carPrice.replaceAll(",", "")); priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); @@ -11,12 +11,13 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below -// a) How many function calls are there in this file? Write down all the lines where a function call is made +// a) How many function calls are there in this file? Write down all the lines where a function call is made --> Number() line4 and line5 -// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? +// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? --> the 2 variables are strings and Number returns a integer -// c) Identify all the lines that are variable reassignment statements +// c) Identify all the lines that are variable reassignment statements -->4,5,7,8 -// d) Identify all the lines that are variable declarations +// d) Identify all the lines that are variable declarations -->1,2,7,8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// --> Number gives the integer or real number and .replace() works with a string and replace all "," by nothing "" \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..c69877bb0 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 65; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -11,15 +11,16 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions -// a) How many variable declarations are there in this program? +// a) How many variable declarations are there in this program? -> 6 because in the same time, there are assignments -// b) How many function calls are there? +// b) How many function calls are there? --> 0 because console.log() is a method -// c) Using documentation, explain what the expression movieLength % 60 represents +// c) Using documentation, explain what the expression movieLength % 60 represents --> % is the modulo which gives the remaining // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators -// d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// d) Interpret line 4, what does the expression assigned to totalMinutes mean? -->We remove the seconds and divide with 60 to have the remaining time without the seconds -// e) What do you think the variable result represents? Can you think of a better name for this variable? +// e) What do you think the variable result represents? Can you think of a better name for this variable? -->lengthOfMovieHours // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +//--> not with a negative number diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..166001448 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -2,19 +2,24 @@ const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( 0, - penceString.length - 1 + penceString.length - 1 // take only 399 ); +console.log(penceStringWithoutTrailingP); + +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); //The padStart() adds characters to the beginning of a string until the string reaches a specified total length +console.log(paddedPenceNumberString); -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( 0, - paddedPenceNumberString.length - 2 + paddedPenceNumberString.length - 2 // gives only the pound ); +console.log(pounds); const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); +console.log(pence); console.log(`£${pounds}.${pence}`); // This program takes a string representing a price in pence diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..204ec32f5 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -14,3 +14,4 @@ Answer the following questions: What does `console` store? What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +//--> console is a object. the . call the property or a method diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..f68dccb78 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -2,12 +2,15 @@ // =============> write your prediction here // call the function capitalise with a string input -// interpret the error message and figure out why an error is occurring +// interpret the error message and figure out why an error is occurring --> str is already declared when the function is declared function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + let str2 = `${str[0].toUpperCase()}${str.slice(1)}`; + return str2; } // =============> write your explanation here // =============> write your new code here + +let test = "tamTamyngo"; +console.log(capitalise(test)); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..7442fef6a 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,18 +1,18 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here --> decimalNumber is a variable which is declarer already // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; + // const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } - -console.log(decimalNumber); +const test = 0.25; +console.log(convertToPercentage(test)); // =============> write your explanation here diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..8aef95639 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,23 @@ - // Predict and explain first BEFORE you run any code... // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here -function square(3) { +/* function square(3) { return num * num; -} +} */ // =============> write the error message here // =============> explain this error message here -// Finally, correct the code to fix the problem +// Finally, correct the code to fix the problem --> need a parameter num // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(3)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..7d980addd 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,20 @@ -// Predict and explain first... +// Predict and explain first... --> the function must give the multiplication of 2 numbers as argument -// =============> write your prediction here +// =============> write your prediction here don't use a return but a console.log which is not reusable later -function multiply(a, b) { +/* function multiply(a, b) { console.log(a * b); } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); - + */ // =============> write your explanation here // 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..57d7b68b0 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,17 @@ -// Predict and explain first... +// Predict and explain first... --> sum() must return a sum of a and b but error because of ; after return // =============> write your prediction here -function sum(a, b) { +/* function sum(a, b) { return; a + b; -} +} */ console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here // Finally, correct the code to fix the problem // =============> write your new code here + +function sum(a, b) { + return a + b; +} diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..848c17ff4 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,30 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here --> a integer is transformed to a string and .slice() take only the last character -const num = 103; +/* const num = 103; function getLastDigit() { return num.toString().slice(-1); -} +} */ -console.log(`The last digit of 42 is ${getLastDigit(42)}`); +/* 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 806 is ${getLastDigit(806)}`); */ // Now run the code and compare the output to your prediction // =============> write the output here -// Explain why the output is the way it is +// Explain why the output is the way it is --> because we use num // =============> write your explanation here // Finally, correct the code to fix the problem // =============> write your new code here +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)}`); // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..9e0fa49f6 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,9 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + // return the BMI of someone based off their weight and height + let BMI = weight / (height * height); + return Math.round(BMI * 10) / 10; // Return rounded BMI with one decimal place as a number (not string) +} + +console.log(calculateBMI(70, 1.73)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..62be2bc19 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 + +let toUp = function (str) { + let newStr = str.toUpperCase().replace(/ /g, "_"); + return newStr; +}; +console.log(toUp("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..e0c613abe 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,25 @@ // 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 + +let toPounds = function (str) { + const penceStringWithoutTrailingP = str.substring( + 0, + penceString.length - 1 // take only 399 + ); + + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); //The padStart() adds characters to the beginning of a string until the string reaches a specified total length + + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 // gives only the pound + ); + + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + return `£${pounds}.${pence}`; +}; + +const penceString = "399p"; +console.log(toPounds(penceString)); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..487484f18 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -7,7 +7,8 @@ function formatTimeDisplay(seconds) { const totalMinutes = (seconds - remainingSeconds) / 60; const remainingMinutes = totalMinutes % 60; const totalHours = (totalMinutes - remainingMinutes) / 60; - + // console.log(totalHours); + console.log(remainingSeconds); return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } @@ -17,18 +18,19 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> write your answer here ---> 3 times -// Call formatTimeDisplay with an input of 61, now answer the following: +// Call formatTimeDisplay with an input of 61, now answer the following: ---> 00:01:01 // b) What is the value assigned to num when pad is called for the first time? -// =============> write your answer here +// =============> write your answer here ---> 61 // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here ---> 0 // 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 the function pad is called last time at the last place of return for remainingSeconds // 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 --> 1 because num is the parameter and pad has 1 as argument +console.log(formatTimeDisplay(61)); diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index ca1dfe7f2..338645d71 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -11,8 +11,8 @@ function getAngleType(angle) { if (angle === 90) { return "Right angle"; } - // Run the tests, work out what Case 2 is testing, and implement the required code here. - // Then keep going for the other cases, one at a time. + // Run the tests, work out what Case 2 is testing, and implement the required code here. + // Then keep going for the other cases, one at a time. } // The line below allows us to load the getAngleType function into tests in other files. @@ -27,7 +27,7 @@ function assertEquals(actualOutput, targetOutput) { `Expected ${actualOutput} to equal ${targetOutput}` ); } - +console.log(90, 180); // Acceptance criteria: // Given an angle in degrees, @@ -60,4 +60,4 @@ const obtuse = getAngleType(120); // 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 diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..1a1a7256c 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,15 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let num = 0; + if (findCharacter) { + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + num++; + } + } + return `${findCharacter} appears ${num} time(s).`; + } else return 0; } +// console.log(countChar("Hello", "l")); + module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..246b08972 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/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 return 0 when a character that does not exist", () => { + const str = "aaaaa"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..7ca87fb73 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,11 @@ function getOrdinalNumber(num) { + if (num > 1 || num < 1) { + return "No 1st"; + } + return "1st"; } +console.log(getOrdinalNumber(1)); + module.exports = getOrdinalNumber; diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..d283e2e16 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,20 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + let repeatedStr = ""; + if (count < 0) { + return "impossible with a negative number"; + } + + if (count === 0) { + return ""; + } + for (i = 1; i <= count; i++) { + repeatedStr += str; + } + return repeatedStr; } +/* let test = "hello"; +let c = 3; */ +// console.log(repeat(test, c)); + module.exports = repeat; diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..e86000617 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -20,13 +20,31 @@ test("should repeat the string count times", () => { // Given a target string str and a count equal to 1, // When the repeat function is called with these inputs, // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. +test("should return the original string", () => { + 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 return an empty string when count equal to 0", () => { + 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 return a message when count a negative number", () => { + const str = "hello"; + const count = -1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("impossible with a negative number"); +}); diff --git a/Sprint-3/3-stretch/cardValidator.js b/Sprint-3/3-stretch/cardValidator.js new file mode 100644 index 000000000..4101d811b --- /dev/null +++ b/Sprint-3/3-stretch/cardValidator.js @@ -0,0 +1,28 @@ +let cardValidator = function (creditNumber) { + let onlyNum = "0123456789"; + let lastChar = creditNumber[creditNumber.length - 1]; + let sum = 0; + let same = creditNumber[0]; + if (Number(lastChar) % 2 === 0) { + return "error even number at the end"; + } else { + for (let i = 0; i < creditNumber.length; i++) { + if (!onlyNum.includes(creditNumber[i])) { + return "error on characters"; + } + } + } + for (let j = 0; j < creditNumber.length; j++) { + sum += Number(creditNumber[j]); + } + if (sum < 16) { + return "too short"; + } + for (let g = 1; g < creditNumber.length; g++) { + if (same !== creditNumber[g]) { + return "It's OK!"; + } + } + return "same numbers"; +}; +console.log(cardValidator("7725463")); diff --git a/Sprint-3/3-stretch/password-validator.js b/Sprint-3/3-stretch/password-validator.js index b55d527db..d6d42d96d 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,6 +1,38 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true -} +let passwordValidator = function (password) { + let digits = password; + const integer = "0123456789"; + let sum = 0; + for (let i = 0; i < digits.length; i++) { + if (!integer.includes(digits[i])) { + return "Invalide numbers"; + } + } -module.exports = passwordValidator; \ No newline at end of file + // Check all digits are NOT the same + if (digits.split("").every((d) => d === digits[0])) { + return "Must not have the same digits"; + } + + if (digits[digits.length - 1] % 2 !== 0) { + return "The last digit must be even"; + } + + for (let i = 0; i < digits.length; i++) { + sum += Number(digits[i]); + } + + if (sum <= 16) { + return "The sum of all digits must greater than 16"; + } + if (digits.length !== 16) { + return "Must have 16 digits"; + } + return "Good one!!"; +}; + +const test = "4444444444444446"; +console.log(passwordValidator(test)); +// 100% correct without mistakes made by Tam --> to check if all digits are the same, I have to use AI. + +module.exports = passwordValidator;