From ba77955ac62b4c4197976e0ea2abfa75d510cb63 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Wed, 29 Jul 2026 06:58:29 +0100 Subject: [PATCH 01/11] Fix the SyntaxError and write an explanation of why it happened. --- Sprint-2/1-key-errors/0.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..b2997a36c2 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -4,10 +4,17 @@ // 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; +// } + +// =============> SyntaxError: Identifier 'str' has already been declared, +// So In JavaScript, we cannot declare a let variable with the same name as an existing parameter within the same scope. This causes a SyntaxError + +// =============> write your new code here function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } - -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("hello")); \ No newline at end of file From b8bb36c9e3625620718197b3b338d8d94f2ef6f9 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Wed, 29 Jul 2026 21:17:49 +0100 Subject: [PATCH 02/11] Fix duplicate parameter and scope errors --- Sprint-2/1-key-errors/1.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..e4ba25798b 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -5,16 +5,27 @@ // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; - return percentage; -} +// return percentage; +// } + +// console.log(decimalNumber); -console.log(decimalNumber); +// =============> JavaScript does not allow a parameter and a const variable inside the same scope to have the same name, so it gives an error. +//console.log(decimalNumber); -// =============> write your explanation here +//decimalNumber only exists inside the function. It is a local variable, so it cannot be accessed outside the function. This causes 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)); \ No newline at end of file From 4d2ea3788269ab159d85c9ffc666fcaec28d7cc8 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Wed, 29 Jul 2026 22:21:03 +0100 Subject: [PATCH 03/11] Fix invalid function parameter in square --- Sprint-2/1-key-errors/2.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..34912ce28f 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -5,16 +5,22 @@ // =============> write your prediction of the error here -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } // =============> write the error message here - +//SyntaxError: Unexpected number // =============> explain this error message here +//A SyntaxError will occur because `3` cannot be used as a function parameter name. +//Function parameters must be valid variable names, such as `num`. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(3)); From e5e90fe1a18babb348cc76e75194a295edb2350e Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Wed, 29 Jul 2026 22:32:56 +0100 Subject: [PATCH 04/11] Fix multiply function to return result --- Sprint-2/2-mandatory-debug/0.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..5ef56cffdb 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,26 @@ -// Predict and explain first... - // =============> write your prediction here - +/* +The function should return the multiplication result so that +the returned value can be used inside the template literal. +*/ 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 original function used console.log() instead of return. +console.log() displays 320 but does not give the value back to +the calling code. Therefore multiply(10, 32) returned undefined. +Using return allows the value 320 to be inserted into the string. +*/ // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> 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 ad521803605e653c9b12351e2becdd1a78942b4b Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Thu, 30 Jul 2026 06:04:00 +0100 Subject: [PATCH 05/11] Fix getLastDigit to use function parameter --- Sprint-2/2-mandatory-debug/2.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..7ffe93771c 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,38 @@ // Predict and explain first... -// Predict the output of the following code: +// Predict the output of the following code: The function will return "3" every time. // =============> Write your prediction here -const num = 103; +/*Even though 42, 105, and 806 are passed to getLastDigit(), +the function does not have a parameter to receive those values. +Instead, it always uses the global variable num, whose value is 103. +Therefore, all three lines will output 3.*/ -function getLastDigit() { - return num.toString().slice(-1); -} +// const num = 103; -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)}`); +// function getLastDigit() { +// 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)}`); // 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 // 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 From e974535ba87c309fbe899001819b868e12904cd2 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Thu, 30 Jul 2026 06:06:26 +0100 Subject: [PATCH 06/11] Fix multiply function to return result --- Sprint-2/2-mandatory-debug/0.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index 5ef56cffdb..846dd0ce09 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -3,11 +3,11 @@ The function should return the multiplication result so that the returned value can be used inside the template literal. */ -function multiply(a, b) { - return a * b; -} +// function multiply(a, b) { +// return a * b; +// } -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here /* From aa116335567bb9ea18f0f61160079829cad938eb Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Thu, 30 Jul 2026 06:07:55 +0100 Subject: [PATCH 07/11] Fix sum function return statement --- Sprint-2/2-mandatory-debug/1.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..9fe4c968d7 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,20 @@ -// Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here -function sum(a, b) { - return; - a + b; -} - -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +/*The program will output: +"The sum of 10 and 32 is undefined" +because the function returns before performing the addition.*/ // =============> write your explanation here + +/*The `return` statement immediately stops the function. +Because it has no value after it, the function returns `undefined`. +The `a + b` line is never executed.*/ + // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here + +function sum(a, b) { + return a + b; +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); \ No newline at end of file From bc00c87e904336e0438f4b275c8afa2f906501e8 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Thu, 30 Jul 2026 06:16:55 +0100 Subject: [PATCH 08/11] Implement BMI calculation --- Sprint-2/3-mandatory-implement/1-bmi.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..b276c32286 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)); \ No newline at end of file From 2e453e7ada86379352ff47b3d7a3c95cf7ee08ad Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Thu, 30 Jul 2026 06:18:38 +0100 Subject: [PATCH 09/11] Implement upper snake case conversion --- Sprint-2/3-mandatory-implement/2-cases.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..f929fd0506 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.toUpperCase().replaceAll(" ", "_"); +} + +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); \ No newline at end of file From f5758a21eba2b893af9583e3222ac9a2c16dda5d Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Thu, 30 Jul 2026 06:24:45 +0100 Subject: [PATCH 10/11] Explain pence to pounds conversion --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..9c412676c3 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,31 @@ // 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 +// This program takes a string representing a price in pence. +// The program then builds up a string representing the price in pounds. + +// 1. Initialise a string variable with the value "399p" +const penceString = "399p"; + +// 2. Remove the trailing "p" from the string +const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 +); + +// 3. Make sure the pence value has at least 3 digits +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + +// 4. Get the pounds part by removing the last two digits +const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +); + +// 5. Get the pence part (the last two digits) +const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + +// 6. Display the final price +console.log(`£${pounds}.${pence}`); \ No newline at end of file From 7d37691d375bc383ba011f134ac5f23f199b9475 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Thu, 30 Jul 2026 06:27:28 +0100 Subject: [PATCH 11/11] Answer formatTimeDisplay tracing questions --- Sprint-2/4-mandatory-interpret/time-format.js | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..9e69e167c1 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here - -// Call formatTimeDisplay with an input of 61, now answer the following: +// =============> 3 times // 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 is called for the first time? -// =============> write your answer here +// c) What is the return value of pad when it is called for the first time? +// =============> "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 formatTimeDisplay(61) has 1 remaining second, +// and the last call to pad is pad(remainingSeconds). -// 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 zero to the beginning of "1" +// to make it two characters long. \ No newline at end of file