From 347d6c4d9aa8c350f17f4605be450366c608ffed Mon Sep 17 00:00:00 2001 From: eyob tech Date: Wed, 22 Jul 2026 17:24:05 +0100 Subject: [PATCH] Complete Sprint 2 key errors, mandatory debug, implement, and interpret exercises --- Sprint-2/1-key-errors/0.js | 14 ++++++----- Sprint-2/1-key-errors/1.js | 19 ++++++++------- Sprint-2/1-key-errors/2.js | 22 ++++++++---------- Sprint-2/2-mandatory-debug/0.js | 10 ++++---- Sprint-2/2-mandatory-debug/1.js | 12 ++++++---- Sprint-2/2-mandatory-debug/2.js | 12 ++++++---- Sprint-2/3-mandatory-implement/1-bmi.js | 6 +++-- Sprint-2/3-mandatory-implement/2-cases.js | 6 +++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 23 +++++++++++++++++++ Sprint-2/4-mandatory-interpret/time-format.js | 19 ++++++++------- 10 files changed, 93 insertions(+), 50 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..00201b6bd6 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,15 @@ // Predict and explain first... -// =============> write your prediction here +// ==============> This will throw a SyntaxError, because 'str' is already declared as the function's parameter, and line 8 tries to declare a new variable with the same name 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; + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; } - -// =============> write your explanation here -// =============> write your new code here +// ==============> The error is "SyntaxError: Identifier 'str' has already been declared". This happens because you can't declare a new variable with let using a name that's already taken - in this case, the parameter str. +// ==============> function capitalise(str) { +// let capitalisedStr = ${str[0].toUpperCase()}${str.slice(1)}; +// return capitalisedStr; +// } \ 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..bf0e817c6a 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,23 @@ // Predict and explain first... - +// // Why will an error occur when this program runs? -// =============> write your prediction here +// ==============> This will throw a SyntaxError because decimalNumber is already declared as the function's parameter, and line 9 tries to redeclare it with const. -// Try playing computer with the example to work out what is going on +// Try playing computer with the example to work out what will happen 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 +// ==============> The error is "SyntaxError: Identifier 'decimalNumber' has already been declared". It happens for the same reason as before - you can't redeclare a variable with the same name as an existing function parameter using const. // 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..5b2305d90c 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,18 @@ +// Predict and explain first BEFORE you run any code. -// Predict and explain first BEFORE you run any code... +// this function should square any number but instead -// this function should square any number but instead we're going to get an error +// ==============> This will throw a SyntaxError, because 3 is used as the parameter name in function square(3), but parameter names can't be numbers - they need to be valid identifiers like "num". -// =============> write your prediction of the error here - -function square(3) { - return num * num; +function square(num) { + return num * num; } -// =============> write the error message here +// ==============> The error is "SyntaxError: Unexpected number" -// =============> explain this error message here +// ==============> This happens because 3 is a number, not a valid parameter name. Parameter names must be identifiers, like "num" - JavaScript doesn't know what to do with a number in that position. // 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..256822af2d 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,16 @@ // Predict and explain first... -// =============> write your prediction here +// ==============> This will print "320" first (from inside the function), then print "The result of multiplying 10 and 32 is undefined", because multiply() doesn't return a value - it only logs it. 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 +// ==============> The function was using console.log to display the answer, instead of returning it. This meant when the outer console.log tried to use the value from multiply(10, 32), it got undefined instead of the actual number, since the function returned nothing. // Finally, correct the code to fix the problem -// =============> write your new code here +// ==============> function multiply(a, b) { +// return a * b; +// } diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..dc18ce8d1c 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,15 @@ // Predict and explain first... -// =============> write your prediction here +// ==============> This will print "The sum of 10 and 32 is undefined", because line 5 has a bare "return;" with nothing after it - this immediately exits the function and returns undefined. Line 6 (a + b) never runs. function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// ==============> The function had "return;" on its own line, followed by "a + b;" on the next line. Once JavaScript hits return; with nothing after it, the function ends immediately and returns undefined - the a + b line is unreachable and never executes. + // Finally, correct the code to fix the problem -// =============> write your new code here +// ==============> function sum(a, b) { +// return a + b; +// } diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..bbd79fb06c 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,11 +1,11 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// ==============> All three lines will print "3", because getLastDigit() ignores the number passed in and always uses the outer variable num (103), whose last digit is 3. const num = 103; -function getLastDigit() { +function getLastDigit(num) { return num.toString().slice(-1); } @@ -14,11 +14,13 @@ 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 output was "3", "3", "3" as predicted. // Explain why the output is the way it is -// =============> write your explanation here +// ==============> getLastDigit didn't accept a parameter, so it always used the outer num variable (103) instead of the number passed in when calling the function. // Finally, correct the code to fix the problem -// =============> write your new code here +// ==============> function getLastDigit(num) { +// return num.toString().slice(-1); +// } // 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-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..c12c783d78 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,7 @@ // 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 + const bmi = weight / (height * height); + return Math.round(bmi * 10) / 10; +} +console.log(calculateBMI(70, 1.73)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..df95b910cc 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(str) { + return str.split(" ").join("_").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..cb10584eee 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,26 @@ // 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("9p")); +console.log(toPounds("1050p")); + diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..34680f9bf5 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -15,24 +15,27 @@ function formatTimeDisplay(seconds) { 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 +// You will need to play computer with this example - use the Python Visualiser https:// // to help you answer these questions // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// ==============> pad will be called 3 times - once for totalHours, once for remainingMinutes, 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 +// ==============> num is 0 (totalHours is called first, and its value is 0). // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// ==============> "00" (0 padded with a leading zero to make it 2 characters long). -// 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? +// ==============> num is 1 (remainingSeconds is called last, and its value is 1). + +// e) What is the return value of pad when it is called for the last time in this program? +// ==============> "01" (1 padded with a leading zero). + +console.log(formatTimeDisplay(61)); -// 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