diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..7422c5208a 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,8 +1,18 @@ // Predict and explain first... // =============> write your prediction here +//There will be a syntax error when the function `capitalise` is called +//because the variable `str` is being declared twice in the same scope. +//The first declaration is in the function parameter, and the second declaration +//is inside the function body. This will cause a "SyntaxError: Identifier 'str' has +//already been declared" error. + + -// call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +//The error occurs because the variable `str` is being declared twice in the same +//scope. The first declaration is in the function parameter, and the second +//declaration is inside the function body. This causes a "SyntaxError: Identifier 'str' +//has already been declared" error. function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; @@ -11,3 +21,7 @@ function capitalise(str) { // =============> write your explanation here // =============> write your new code here +function capitalise(str) { + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..0c8056be96 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,6 +2,8 @@ // Why will an error occur when this program runs? // =============> write your prediction here +The error will occur because the variable `decimalNumber` is being declared twice in +the same scope. // Try playing computer with the example to work out what is going on @@ -16,5 +18,18 @@ console.log(decimalNumber); // =============> write your explanation here +An error will occur because the variable `decimalNumber` is being declared twice +in the same scope. The first declaration is in the function parameter, +and the second declaration is inside the function body. This will cause a +"SyntaxError: Identifier 'decimalNumber' has already been declared" error. + + // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(0.5)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..1ebb75ac8d 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,25 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +//There will be a syntax error. function square(3) { return num * num; } // =============> write the error message here +// "SyntaxError: Unexpected number" // =============> explain this error message here +// The error occurs because the function parameter is defined as a number (3) +// instead of a variable name. In JavaScript, function parameters must be variable +// names, not values. // Finally, correct the code to fix the problem // =============> 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 b27511b417..02c98db57c 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// The output of the code will be: undefined. function multiply(a, b) { console.log(a * b); @@ -9,6 +10,16 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// The function multiply takes two arguments, a and b, and logs their product to +// the console. However, it does not return any value, which means that when we +// call multiply(10, 32) inside the template literal, it will return undefined. +// Therefore, the output of the console.log statement will be: "The result of +// multiplying 10 and 32 is undefined". // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..5dfcf9da58 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,6 @@ // Predict and explain first... // =============> write your prediction here +// The output of the code will be: undefined. function sum(a, b) { return; @@ -9,5 +10,14 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The function sum takes two arguments, a and b, and has a return statement that +// does not return any value. The line a + b is never executed because it comes after +// the return statement. Therefore, when we call sum(10, 32) inside the template literal, +// it will return undefined. As a result, the output of the console.log statement will be: +// "The sum of 10 and 32 is undefined". + // Finally, correct the code to fix the problem // =============> write your new code here +function sum(a, b) { + return a + b; +} \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..6bd92244cb 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,6 +2,8 @@ // Predict the output of the following code: // =============> Write your prediction here +//The output of the code will be: "The last digit of 42 is 3", +//"The last digit of 105 is 3", "The last digit of 806 is 3". const num = 103; @@ -15,10 +17,31 @@ 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 +//The output of the code is: "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 +// The function getLastDigit is not working properly because it does not take any +// parameters. + // 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 +//getLastDigit is not working properly because it does not take any parameters. +//It always returns the last digit of the number 103, which is hardcoded in the +//function. To fix this, we need to modify the function to accept a parameter (num) +//and return the last digit of that parameter instead. + + + diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..6c6dafbb93 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,6 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + const bmi = weight / Math.pow(height, 2); + return Math.round(bmi * 10) / 10; } \ 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 5b0ef77ad9..baf41f4a0d 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -11,6 +11,12 @@ // Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" + // 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 toUpperSnakeCase(str) { + // return the string in UPPER_SNAKE_CASE + return str.toUpperCase().replace(/ /g, '_'); +} diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..b8dcb57302 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,47 @@ // 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 + +//(Code copied from Sprint-1 interpret/to-pounds.js) +const penceString = "399p"; + +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}`); + +//(Turning it into a resuable block of code) +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"); + + return `£${pounds}.${pence}`; +} + +console.log(toPounds("399p")); +console.log(toPounds("100p")); +console.log(toPounds("50p")); \ No newline at end of file diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..a8fae239bd 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -22,17 +22,29 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +//The function pad will be called 3 times when formatTimeDisplay is called, +//once for totalHours, once for remainingMinutes, and once for remainingSeconds. // 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 +//The value assigned to num when pad is called for the first time is 0, +//because 61 seconds is equal to 1 minute and 1 second, which means totalHours is 0. // c) What is the return value of pad is called for the first time? // =============> write your answer here +//The return value of pad when it is called for the first time is "00", +//because the value of num is 0, and the function pad adds a leading zero to +// make it a two-digit string. // 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 +//The value assigned to num when pad is called for the last time in this program is 1, +//because 61 seconds is equal to 1 minute and 1 second, which means remainingSeconds is 1. // e) What is the return value of pad when it is called for the last time in this program? Explain your answer // =============> write your answer here +//The return value of pad when it is called for the last time in this program is "01", +//because the value of num is 1, and the function pad adds a leading zero to +// make it a two-digit string.