diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..7163d38b4 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,21 @@ // Predict and explain first... -// =============> write your prediction here +// Prediction: I predict that the code will produce an error because +// str is already declared as a function parameter and is declared +// again with 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; + return `${str[0].toUpperCase()}${str.slice(1)}`; } -// =============> write your explanation here -// =============> write your new code here +// Explanation: The parameter str is already declared when the +// function is created. We cannot declare another variable called +// str with let in the same scope. + +// New code: +// The function now uses the existing str parameter and returns +// the capitalised string. + +console.log(capitalise("hello")); \ No newline at end of file diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..3caa2e5c5 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,28 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// Prediction: An error will occur because decimalNumber is already +// declared as a function parameter and is declared again with const. +// Also, decimalNumber is being used outside the function where it +// is not available. // 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; } -console.log(decimalNumber); +console.log(convertToPercentage(0.5)); -// =============> write your explanation here +// Explanation: The function parameter decimalNumber already contains +// the value we give to the function. We cannot declare another +// decimalNumber inside the function with const. The original code +// also tried to use decimalNumber outside the function, where it +// does not exist. // Finally, correct the code to fix the problem -// =============> write your new code here + +// New code: The function uses the decimalNumber parameter and returns +// the percentage. We call the function with 0.5 and log the result. \ No newline at end of file diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..af55816b8 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,29 @@ - // Predict and explain first BEFORE you run any code... // this function should square any number but instead we're going to get an error +// Prediction: I predict that an error will occur because 3 cannot +// be used as a function parameter name. + // =============> write your prediction of the error here -function square(3) { +function square(num) { + return num * num; + } // =============> write the error message here +// SyntaxError: Unexpected number + // =============> explain this error message here +// The error happens because 3 is a number, not a valid parameter name. +// A function parameter needs to be a variable name, such as num. + // Finally, correct the code to fix the problem // =============> write your new code here - +console.log(square(3)); \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..b84374400 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,25 @@ // Predict and explain first... +// Prediction: I predict that the function will print 320, +// but the final message will say undefined because the function +// does not return the result. + // =============> write your prediction here function multiply(a, b) { - console.log(a * b); + return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// Explanation: console.log() displays a value on the screen, +// but return sends a value back from the function. +// The original function used console.log() instead of return, +// so the function returned undefined. + // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here + +// The corrected function returns the result of multiplying a and b. \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..496acbb87 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,21 @@ // Predict and explain first... + +// Prediction: I predict that the function will return undefined +// because return stops the function before a + b can be calculated. + // =============> write your prediction here function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// Explanation: The original code used return on its own line. +// When JavaScript reaches return, the function stops and returns +// undefined. The line a + b is therefore never used. +// We need to return a + b together. + // =============> write your explanation here // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..2bf7df029 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,12 +1,15 @@ // Predict and explain first... // Predict the output of the following code: +// Prediction: I predict that the program will print 3 for all +// three numbers because the function always uses the value 103. + // =============> Write your prediction here const num = 103; -function getLastDigit() { - return num.toString().slice(-1); +function getLastDigit(number) { + return number.toString().slice(-1); } console.log(`The last digit of 42 is ${getLastDigit(42)}`); @@ -14,11 +17,25 @@ 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 +// Output after correction: +// The last digit of 42 is 2 +// The last digit of 105 is 5 +// The last digit of 806 is 6 + // =============> write the output here -// Explain why the output is the way it is + +// Explanation: The original getLastDigit function did not have +// a parameter, so the numbers 42, 105, and 806 passed to the +// function were ignored. Instead, the function always used the +// global value 103, so it returned 3 every time. +// +// The corrected function has a parameter called number. +// This allows it to receive the number passed into the function. + // =============> write your explanation here + // Finally, correct the code to fix the problem // =============> write your new code here -// This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +// The function now receives a number as an argument and returns +// its last digit. \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..2a839efde 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,9 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height + const bmi = weight / (height * height); + + return bmi.toFixed(1); } + +console.log(calculateBMI(70, 1.73)); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..0074bff2f 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,7 @@ // 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.replaceAll(" ", "_").toUpperCase(); +} \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..e49d63f1c 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -1,6 +1,7 @@ -// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js +function toPounds(kg) { + return kg * 2.20462; +} -// You will need to take this code and turn it into a reusable block of code. -// 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 +console.log(toPounds(1)); +console.log(toPounds(5)); +console.log(toPounds(10)); \ No newline at end of file diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..7ad684bf8 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -1,38 +1,16 @@ -function pad(num) { - let numString = num.toString(); - while (numString.length < 2) { - numString = "0" + numString; - } - return numString; -} - -function formatTimeDisplay(seconds) { - const remainingSeconds = seconds % 60; - const totalMinutes = (seconds - remainingSeconds) / 60; - const remainingMinutes = totalMinutes % 60; - const totalHours = (totalMinutes - remainingMinutes) / 60; - - return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; -} - -// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit -// to help you answer these questions - -// Questions - // 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 +// =============> 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 +// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer +// =============> 1, because the remaining seconds are 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 +// e) What is the return value of pad when it is called for the last time in this program? Explain your answer +// =============> "01", because pad adds a 0 to the front of 1. \ No newline at end of file