diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..e37626f01 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,21 @@ // Predict and explain first... // =============> write your prediction here +// The code will throw a SyntaxError // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring -function capitalise(str) { +// Original code with error: +/* function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; -} +} */ // =============> write your explanation here +// The error occurs because because the variable 'str' is being redeclared inside the function. +// The parameter 'str' is already defined, and trying to declare it again with 'let' causes a conflict. + // =============> write your new code here +function capitalise(str) { + return `${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..df1ad1fe9 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,9 +2,10 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// The error will occur because the variable 'decimalNumber' is being redeclared. // Try playing computer with the example to work out what is going on - +/* function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -13,8 +14,19 @@ function convertToPercentage(decimalNumber) { } console.log(decimalNumber); +*/ // =============> write your explanation here +// The parameter decimalNumber is already declared as part of the function. +// When we try to declare it again inside the function, it causes a syntax error because +// we cannot redeclare a variable that is already declared in the same scope. // 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 aad57f7cf..7d9c5e11e 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,25 @@ - // Predict and explain first BEFORE you run any code... +// It will throw a syntax error because 3 is not a valid. // 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) { return num * num; } - +*/ // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// The error occurs because function parameters must be valid identifiers (i.e., variable names), not literal numbers. // Finally, correct the code to fix the problem // =============> 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..879d6828b 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 - +// I think it will return undefined. +/* 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 +// The function multiply does not have a return statement, so it returns undefined by default. +// The string, it will show "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)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..99cc67035 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,24 @@ // Predict and explain first... // =============> write your prediction here +// It will return undefined. +/* function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +*/ // =============> write your explanation here +// The function sum returns nothing because the return statement is followed by a semicolon +// and the expression `a + b` is not returned. + // 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..0a1063639 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,9 @@ // Predict the output of the following code: // =============> Write your prediction here +// The output will be 3 in each expression. +/* const num = 103; function getLastDigit() { @@ -12,13 +14,31 @@ function getLastDigit() { 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 function getLastDigit always returns the last digit of the variable `num`, which is 103. + // 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 + +// The function getLastDigit was not working properly because it was using a variable `num` +// instead of the parameter passed to the function. By changing the function to accept a parameter `num`, +// it now correctly returns the last digit of the number passed to it. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..40d8f094a 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,6 @@ // 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 + return Number((weight / (height * height)).toFixed(1)); +} diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..6dd1189c2 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 toUpperSnakeCase(input) { + return input.trim().toUpperCase().replace(/\s+/g, "_"); +} + +console.log(toUpperSnakeCase("manchester united")); // "MANCHESTER_UNITED" diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..32770a4d3 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,12 @@ // 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(penceAmount) { + const padded = penceAmount.padStart(3, "0"); + const pounds = padded.substring(0, padded.length - 2); + const pence = padded.substring(padded.length - 2); + return `£${pounds}.${pence}p`; +} + +console.log(toPounds("399")); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..3624abf17 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,35 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// Answer: 3 times +// pad(totalHours) +// pad(remainingMinutes) +// pad(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 +// pad(num) = pad(0) = 0 // c) What is the return value of pad is called for the first time? // =============> write your answer here +// pad(num) = pad(0) = "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 +// Answer: pad(num) = pad(1) = 1 +// Explanation below: +// Last call: pad(remainingSeconds) +// remainingSeconds = 1 +// pad(1) → num = 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 +// Answer: pad(num) = pad(1) = "01" +// Explanation below: +// Last call: pad(remainingSeconds) +// remainingSeconds = 1 +// toString() → "1" +// "1".padStart(2, "0") → "01" +// pad(1) → "01"