diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..b2ced95eb 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,7 +1,9 @@ // Predict and explain first... // =============> write your prediction here +// I predict this code will produce a SyntaxError because str is declared twice in the same scope using let. // call the function capitalise with a string input +capitalise("hello"); // interpret the error message and figure out why an error is occurring function capitalise(str) { @@ -10,4 +12,14 @@ function capitalise(str) { } // =============> write your explanation here +// The error occurs because str is already declared as a parameter +// Inside the function, let str attempts to declare another variable +// with the same name in the same scope. +// We can fix the problem by removing let and assigning the new value +// directly to the existing parameter. + // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..bea122587 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -2,6 +2,8 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// An error will occur because `decimalNumber` is declared twice +// inside the function: once as a parameter and again using `const`. // Try playing computer with the example to work out what is going on @@ -15,6 +17,16 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// The function already has a parameter called decimalNumber. +// The line const decimalNumber = 0.5 tries to declare another variable with the same name in the same scope. +// JavaScript does not allow this, so a SyntaxError occurs. +// There is also another problem: decimalNumber only exists inside the function, so console.log(decimalNumber) outside the function would cause a ReferenceError. // 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-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..4b3397ef9 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -4,17 +4,23 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// A SyntaxError will occur because 3 is not a valid parameter name. function square(3) { return num * num; } // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// A parameter must be an identifier (variable name), but 3 is a number. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..96b10a76f 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,6 +1,9 @@ // Predict and explain first... // =============> write your prediction here +// The output will be: +// 320 +// The result of multiplying 10 and 32 is undefined function multiply(a, b) { console.log(a * b); @@ -9,6 +12,14 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// multiply(10, 32) runs the function and console.log(a * b) prints 320. +// However, the function does not have a return statement. +// A JavaScript function without a return statement returns 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-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..834967286 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,5 +1,6 @@ // Predict and explain first... // =============> write your prediction here +// Output: The sum of 10 and 32 is undefined function sum(a, b) { return; @@ -9,5 +10,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// Because there is no value after `return`, the function returns undefined. +// The line a + b is unreachable code, meaning it will never run. // 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-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..e9be4b966 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,6 +2,10 @@ // Predict the output of the following code: // =============> Write your prediction here +// Output: +// 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 +19,21 @@ 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 +// getLastDigit() does not have a parameter, so the values 42, 105 and 806 passed to it are not being used. // 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-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..93b6a2e1c 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/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 / (height * height); + return bmi.toFixed(1); } diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..1bb0a5b72 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,6 @@ // 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 str.toUpperCase().replaceAll(" ", "_"); +} diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..c1b79c939 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,27 @@ // 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("50p")); +console.log(toPounds("5p")); +console.log(toPounds("1250p")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..21dbd48f0 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -22,17 +22,30 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// 3 // 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 +// num = 0 // c) What is the return value of pad when it is called for the first time? // =============> write your answer here +// "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 +// num = 1 +// The three calls are: +// pad(totalHours) -> pad(0) +// pad(remainingMinutes) -> pad(1) +// pad(remainingSeconds) -> pad(1) +// Therefore, the last call receives 1 as num. // 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 +// "01" +// num is 1, so numString starts as "1". +// Because its length is less than 2, "0" is added to the beginning. +// The function returns "01".