From 23a8d2a561696b6cc2c4a8034f8ac709686d6fca Mon Sep 17 00:00:00 2001 From: fromonda Date: Sat, 4 Jul 2026 16:52:00 +0100 Subject: [PATCH 1/5] Rename local variable to avoid redeclaration --- Sprint-2/1-key-errors/0.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..50ba5975f1 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,10 @@ // Predict and explain first... -// =============> write your prediction here +// The error in the code occurs because the variable 'str' is being declared twice within the same scope. +// The first declaration is in the function parameter, and the second declaration is inside the function body. +// This causes a syntax error because JavaScript does not allow redeclaration of variables in the same scope using 'Let'. +// To fix this error, we can simply remove the 'Let' keyword from the second declaration of 'str' inside the function body. +// The correction would be to assign the new variable without redeclaration, like this: 'str = `${str[0].toUpperCase()}${str.slice(1)}`;'. +// This way, we are reassigning the value of 'str' without trying to declare it again. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +14,12 @@ function capitalise(str) { return str; } -// =============> write your explanation here -// =============> write your new code here +// The error in the code occurs because the variable 'str' is being declared twice within the same scope. +// The first declaration is in the function parameter, and the second declaration is inside the function body. +// This causes a syntax error because JavaScript does not allow redeclaration of variables in the same scope using 'Let'. +// To fix this error, I can rename the variable inside the function body to something else, like 'caps', to avoid redeclaration. + +function capitalise(str) { + let caps = `${str[0].toUpperCase()}${str.slice(1)}`; + return caps; +} From 5c99b42f7431204ceb1dc500cd13a559947dc106 Mon Sep 17 00:00:00 2001 From: fromonda Date: Sat, 11 Jul 2026 15:52:40 +0100 Subject: [PATCH 2/5] Remove duplicate variable declaration in function --- Sprint-2/1-key-errors/1.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..a6ea00e1c5 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,7 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// Declaring a variable with the same name as the function parameter in the function body causes a SyntaxError. // Try playing computer with the example to work out what is going on @@ -14,7 +14,13 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// After running the code, the following error message was displayed: +// Uncaught SyntaxError: Identifier '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)); From 9af074ca7b6f35f5f2964b17766b4114411634a2 Mon Sep 17 00:00:00 2001 From: fromonda Date: Sat, 11 Jul 2026 16:28:41 +0100 Subject: [PATCH 3/5] Fix invalid function parameter --- Sprint-2/1-key-errors/2.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..f38101a43a 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,19 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// The error occurs because 3 is used as a function parameter. +// Parameters must be variable names, whereas values are passed as arguments when the function is called. function square(3) { return num * num; } -// =============> write the error message here +// Uncaught SyntaxError: Unexpected number -// =============> explain this error message here +// The parser evaluates the code as a SyntaxError before execution because a value cannot be used as a parameter name in JavaScript. // Finally, correct the code to fix the problem -// =============> write your new code here - - +// function square(num) { +// return num * num; +// } From ed78bdd422263d15bc0d629ed97d604ef786fe39 Mon Sep 17 00:00:00 2001 From: fromonda Date: Sat, 11 Jul 2026 16:52:41 +0100 Subject: [PATCH 4/5] Replace console.log with return --- Sprint-2/2-mandatory-debug/0.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..6ecd402014 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,8 @@ // Predict and explain first... -// =============> write your prediction here +// The result of multiplying 10 and 32 is expected to be undefined because the multiply() function does not return the result. +// It only prints it using console.log(). + function multiply(a, b) { console.log(a * b); @@ -8,7 +10,12 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// After running the code, the result of multiplying 10 and 32 is undefined indeed. +// The problem occurs because the function has no return statement to send a value back to where the function was called. // 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)}`); \ No newline at end of file From a2491ccb8bcf29052d21ff3940f8735b87cb2319 Mon Sep 17 00:00:00 2001 From: Francesco Romano Monda Date: Wed, 22 Jul 2026 15:19:43 +0100 Subject: [PATCH 5/5] Complete JavaScript practice exercises --- Sprint-2/2-mandatory-debug/1.js | 12 +++++++--- Sprint-2/2-mandatory-debug/2.js | 23 ++++++++++++++---- Sprint-2/3-mandatory-implement/1-bmi.js | 7 ++++-- Sprint-2/3-mandatory-implement/2-cases.js | 7 ++++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 24 +++++++++++++++++++ Sprint-2/4-mandatory-interpret/time-format.js | 17 ++++++------- 6 files changed, 72 insertions(+), 18 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..c33aea359f 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// The issue is that return is on one line and a + b is on the next. +// Therefore, a + b is never returned. function sum(a, b) { return; @@ -8,6 +9,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// After running the code, the sum of 10 and 32 is undefined. +// JavaScript inserts a semicolon after return when a + b is placed on the next line. +// This causes the function to return before calculating the sum. + // 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..c345cfcc55 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,7 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// I believe that in each case, the output will be the last digit of 103, which is 3. const num = 103; @@ -14,11 +14,24 @@ 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() does not accept a parameter. +// Consequently, it always uses the global variable num, whose value is 103. + // Finally, correct the code to fix the problem -// =============> write your new code here +// const num = 103; +// 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 +// getLastDigit() is not working properly because it always uses the global num variable (103). +// I added num as a parameter so that the function can accept the value passed as an argument. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..ff7c0c1ee4 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,8 @@ // 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 + let bmi = weight / (height * height); + return bmi.toFixed(1); +} + +console.log(calculateBMI(70, 1.73)); \ 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..92bcf4e1f4 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,10 @@ // 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.toUpperCase().replaceAll(" ", "_"); +} + +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..5020c54732 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/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("5p")); +console.log(toPounds("1200p")); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..846cb6cfa9 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -21,18 +21,19 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// pad() is called 3 times because it is used 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 +// Because the first call to pad() is pad(totalHours), and formatTimeDisplay(61) calculates totalHours as 0, the value passed to pad() as num is 0. -// c) What is the return value of pad is called for the first time? -// =============> write your answer here +// c) What is the return value of pad when called for the first time? +// When pad() is called for the first time, num is 0 because totalHours is 0. +// The function converts it to a string and adds a leading zero, so the return value is "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 +// Because the last call to pad() is pad(remainingSeconds) and formatTimeDisplay(61) calculates remainingSeconds as 1 (61 % 60 = 1), the value passed into pad() as num is 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 +// The return value of pad() when it is called for the last time is "01" because num is 1, and the function pads numbers by adding a leading zero. \ No newline at end of file