diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..f68de5cdfa 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// It will give an error because the variable `str`, is declared twice inside the same function, +// It is already a parameter, and then it is redeclared using `let`. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring -function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; -} +//function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } // =============> write your explanation here +// The error happens because 'str' is written two times, first, it is the function parameter. +// Then, it is declared again using 'let', javaScript does not allow declaring the same variable twice, inside the same function. // =============> write your new code here + +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} +console.log(capitalise("Code")); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..06ef3a3551 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,27 @@ // Predict and explain first... +// The code will give an error because `decimalNumber` is declared twice, +// once as the function parameter, and again with `const` inside the function. // Why will an error occur when this program runs? // =============> write your prediction here - +// The error occurs because `decimalNumber` has already been declared. // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; - - return percentage; -} +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; -console.log(decimalNumber); +// return percentage; +// } +// console.log(decimalNumber); // =============> write your explanation here - +// When I run the code, it gives a SyntaxError saying that, `decimalNumber` has already been declared. // 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)); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..b581d2405c 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,29 @@ // Predict and explain first BEFORE you run any code... +// Function parameters must be variables, not numbers. // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// The code will give a SyntaxError because `3` is not a valid parameter name. -function square(3) { - return num * num; -} +//function square(3) { + // return num * num; +// } // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here - +// The error happens because `3` is used as a parameter name. +// Parameters must be variable names, not numbers. // Finally, correct the code to fix the problem // =============> write your new code here +function square(number) { + return number * number; +} +console.log(square(3)); + diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..fc55718aa4 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// When multiply(10, 32) runs, it prints 320, But the function does not return anything. -function multiply(a, b) { - console.log(a * b); -} -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +// 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 +// multiply prints the result but does not return anything, so using it in a template string shows undefined. // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + return (a * b) +} + +console.log(multiply(10, 32)); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..fa161f41ed 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,21 @@ // Predict and explain first... // =============> write your prediction here +// The function sum does not return anything, +// so the console will show "The sum of 10 and 32 is undefined". -function sum(a, b) { - return; - a + b; -} +// function sum(a, b) { +// return +// a + b; +// } -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// 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); +} + +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 57d3f5dc35..db018beaa7 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,43 @@ // Predict the output of the following code: // =============> Write your prediction here +// The output will always be 3.This is because the function uses the variable `num`, +// which is set to 103. +// 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 + +// 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 output is always 3 because the function uses `num` (103),and ignores the number passed into it. // 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 +// We must pass the number into the function as a parameter. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..57cc28f402 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) { + const bmi = weight / (height * height); + const roundedBMI = Math.round(bmi * 10) / 10; + return roundedBMI; + +} +console.log(calculateBMI(70, 1.73)); // return the BMI of someone based off their weight and height -} \ 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..180682c6c2 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,12 @@ // 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(inputString) { + return inputString.replaceAll(" ", "_").toUpperCase(); + + +} +console.log(toUpperSnakeCase("hello there")); +console.log (toUpperSnakeCase("lord of the rings")); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..7fd793aad1 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,29 @@ // 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 + +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("400p")); +console.log(toPounds("243p")); +console.log(toPounds("9876p")); +console.log(toPounds("18p")); \ 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 7c98eb0e8c..b38353dcbe 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,22 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// It is called 3 times, once for hours, once for minutes, and once for seconds. // 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 first argument passed to pad is totalHours, which is 0. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The first call to pad returns "00". // 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 last call to pad receives num = 1, because remainingSeconds = 61 % 60 = 1. // 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 +// The last call to pad returns "01". The number 1 is converted to a string and padded to two characters with a leading 0. \ No newline at end of file