From 5547d8338eae2d669ce0480531226bd9407f33cc Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Wed, 20 May 2026 20:32:07 +0100 Subject: [PATCH 01/11] remove 2nd declaration of parameter --- Sprint-2/1-key-errors/1.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..7f1b2eb93b 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,36 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// I predict that there will be two errors in this code. +// The first error will be that the const decimalNumber +// has already been declared in the function parameter, +// it cannot be declared twice, which will cause an error because +// it is not allowed to redeclare a variable with the same name. +// The 2nd error is that console.log(decimalNumber) is trying +// to log a variable that is outside the function, which will cause +// an error because it is not defined. // 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; + return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(0.1)); // This will log "10%" +// console.log(decimalNumber); // This will cause an error because decimalNumber is not defined outside the function + // =============> write your explanation here +// decimalNumber is already a parameter so it can't be declared again +// with const inside the function. Also, variables declared inside a +// function are not accessible outside the function (scope), so +// console.log(decimalNumber) out the function would fail. +// the fix is to remove the const declaration and to log the percentage +// instead of decimalNumber, which is the variable that holds the result +// of the calculation (convertToPercentage(0.5)). // Finally, correct the code to fix the problem + + // =============> write your new code here From 455e47fd36208b0c6b6de8b4b3c0d980a1bb8afe Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Wed, 20 May 2026 20:38:27 +0100 Subject: [PATCH 02/11] replace numeric parameter 3 with valid name num --- Sprint-2/1-key-errors/2.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..7b7a898629 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,28 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// I predict that I will get a SyntaxError as a parameter cannot +// be a Number, parameters must be valid variable names. -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } -// =============> write the error message here +// =============> write the error message here: +// SyntaxError: Unexpected number // =============> explain this error message here +// Function parameters must be valid variable names, they must +// follow the same naming rules. a number like 3 is not a valid +// parameter name. +// TO fix the code, rename the parameter to num (or any valid variable +// name) and use it in the return statement.) // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} - +console.log(square(3)); // This should now work and output 9 From e90b646356ddaff44aefa15297f45bca704051df Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Wed, 20 May 2026 20:39:08 +0100 Subject: [PATCH 03/11] remove duplicate let declaration of parameter str --- Sprint-2/1-key-errors/0.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..5a54ab4bd4 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,29 @@ // Predict and explain first... // =============> write your prediction here +// I think that there will be a SyntaxError because str is already +// declared as a parameter, and I am trying to re-declare it as +// a variable with let inside the function. This is not allowed in +// Javascript. // 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; +// } + +// =============> write your explanation here +// You can't use let to declare a variable with the same name +// as a parameter in the same function. The parameter str is +// already declared, so when you try to declare it again with let, +// it causes a SyntaxError. To fix this, you can either remove +// let and just assign a new value to str, or you can use a different +// variable name for the capitalised string. +// =============> 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 5fba49295e8b219de1eedc296c6bd36f5e825645 Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Thu, 21 May 2026 12:52:54 +0100 Subject: [PATCH 04/11] change console.log to return in multiply function --- Sprint-2/2-mandatory-debug/0.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..7602b4c979 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,31 @@ // Predict and explain first... // =============> write your prediction here +// The code will print "The result of multiplying 10 and 32 is undefined" +// for the result because the function multiply does not return any +// value, it only uses console.log to print the result of the +// multiplication instead of return. Therefore when we try to use +// the result of the function in the template literal, it will be +// undefined as it logs the value but returns nothing. -function multiply(a, b) { - console.log(a * b); -} +// function multiply(a, b) { +// console.log(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 +// console.log() inside a function will print the result to the console +// but it does not return any value. When we try to use the result +// of the function in a template literal, it will be undefined +// because the function does not return anything. To use a function's +// results inside a template literal, the function must return +// a value instead of just logging it to the console. // 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 059a7310df8bf3761d2873d7657d3d75076c35e6 Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Thu, 21 May 2026 12:57:57 +0100 Subject: [PATCH 05/11] move a + b onto the same line as return --- Sprint-2/2-mandatory-debug/1.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..80504d298c 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,27 @@ // Predict and explain first... // =============> write your prediction here +// The output will be "The sum of 10 and 32 is undefined" because the +// function sum does not return the result of a + b. The return +// statement is empty, so it returns undefined by default. -function sum(a, b) { - return; - a + b; -} +// function sum(a, b) { +// return; +// a + b; +// } -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// In JS, when a function has a return statement without a value, +// it returns undefined. The expression a+b on the next line is +// never reached. Fix -> put a+b on the same line as return, +// or remove the return statement and just have a+b as the last line +// of the function. + // 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)}`); \ No newline at end of file From aa93d906fcad5c451a1491c30f6ffa70ad452ec3 Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Thu, 21 May 2026 13:05:02 +0100 Subject: [PATCH 06/11] use parameter instead of hardcoded num variable --- Sprint-2/2-mandatory-debug/2.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..f2d84c3e2c 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,6 +2,9 @@ // Predict the output of the following code: // =============> Write your prediction here +// All 3 console.logs will print "3" because the getLastDigit +// function ignores its parameter and always returns the hardcoded +// const num which is 103. const num = 103; @@ -15,10 +18,30 @@ 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 is: +// 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 uses a hardcoded variable num = 103 from outside +// the function instead of the num parameter that is passed to the +// function. Therefore, it always returns the last digit of 103. +// To fix this I can remove the const num = 103 and add num as a +// parameter to the function so each call to getLastDigit will use +// the number passed in as an argument (its own input) instead of the +// hardcoded value. + // 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 dd2ba4508e3f5d8ac53ea97bd6636a3f7436ea22 Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Thu, 21 May 2026 13:42:21 +0100 Subject: [PATCH 07/11] implement calculateBMI function -> divides weight by height squared, returns 1dp --- Sprint-2/3-mandatory-implement/1-bmi.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..e9396685a3 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,9 @@ 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 Number(bmi.toFixed(1)); +} +console.log(calculateBMI(58, 1.64)); // 21.6 +console.log(calculateBMI(70, 1.73)); // 23.4 +console.log(calculateBMI(90, 1.8)); // 27.8 \ No newline at end of file From 777e5f8fdacb6eada0cdf4e838ab0814f0b4b92f Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Thu, 21 May 2026 13:52:29 +0100 Subject: [PATCH 08/11] coverts string to UPPER_SNAKE_CASE, 2 options --- Sprint-2/3-mandatory-implement/2-cases.js | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..d2108b7693 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,42 @@ // 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 + +// Declare a function called toUpperSnakeCase with named parameter inputString + +function toUpperSnakeCase(inputString) { + // Convert the input string to uppercase + const upperCaseString = inputString.toUpperCase(); + // Replace spaces with underscores + const snakeCaseString = upperCaseString.replace(/ /g, '_'); + return snakeCaseString; +} + +// Call the function with different inputs to check it works +console.log(toUpperSnakeCase("hello there")); // Output: "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // Output: "LORD_OF_THE_RINGS" +console.log(toUpperSnakeCase("javascript is fun")); // Output: "JAVASCRIPT_IS_FUN" +console.log(toUpperSnakeCase("test case")); // Output: "TEST_CASE" +console.log(toUpperSnakeCase("multiple words here")); // Output: "MULTIPLE_WORDS_HERE" + +//can also test with edge cases +console.log(toUpperSnakeCase("")); // Output: "" +console.log(toUpperSnakeCase("singleword")); // Output: "SINGLEWORD" +console.log(toUpperSnakeCase(" leading and trailing spaces ")); // Output: "___LEADING_AND_TRAILING_SPACES___" + +// Alternative solution using replaceAll method +function toUpperSnakeCase(str) { + return str.toUpperCase().replaceAll(" ", "_"); +} + +// Differences between the two methods: +// 1. The first method uses a regular expression with the replace method +// to replace all spaces, while the second method uses the replaceAll +// method which is more straightforward for this specific case. +// Regex is a more complex tool that can be used for more advanced +// string manipulation and "replaces all matches globally", while the +// replaceAll methods is simpler and more efficient for replacing all +// occurrences of a specific substring. +// 2. The replaceAll method is a newer addition to JavaScript and may +// not be supported in older environments, while the replace method +// with a regular expression is widely supported. \ No newline at end of file From 28f428030397bdb29268aeaba7383ca631082221 Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Thu, 21 May 2026 14:09:58 +0100 Subject: [PATCH 09/11] wraps sprint-1 logic in reusable function --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 38 +++++++++++++++++++ 1 file changed, 38 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..2360748641 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,41 @@ // 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 + +// const penceString = "399p"; + +// removed the hardcoded penceString and replaced with a parameter +// called penceString +function toPounds(penceString) { // penceString is now a parameter, not + // a hardcoded variable + +// the rest of the code remains unchanged, but it is now inside the +// toPounds function, and it uses the penceString parameter instead +// of the hardcoded variable. +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); + +// the old console.log statement is removed and replaced with a return +// stateement that returns the formmated string with the pounds and pence. + return `£${pounds}.${pence}`; +} + +// console.log(`£${pounds}.${pence}`); + +// the function toPounds can now be called with different pence strings +// to test its functionality. +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("5p")); // £0.05 +console.log(toPounds("1234p")); // £12.34 + \ No newline at end of file From 564840838395b0534b5ab4cef0a4d9467eda0c38 Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Thu, 21 May 2026 14:32:43 +0100 Subject: [PATCH 10/11] answers all questions about pad and formatTimeDisplay --- Sprint-2/4-mandatory-interpret/time-format.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..3045c6535d 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -15,6 +15,8 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(61)); // Should print "00:01:01" + // 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 @@ -22,17 +24,43 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// The pad function is called 3 times within formatTimeDisplay: once +// for totalHours, once for remainingMinutes, and once for +// remainingSeconds. Therefore, pad will be called 3 times. + // 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 (totalHours) is assigned to num because 61 seconds is less than +// 3600 seconds (1 hour), so totalHours is calculated as 0. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// "00" is the return value of pad because num is 0 because totalHours +// is 0, and the while loop in pad will add a "0" to the left of +// numString until its length is at least 2. Since numString starts as +// "0", it will become "00" after one iteration of the loop, and then it +// will be returned as the final result of the first call to pad. Also, +// "0".length is 1, which is less than 2, so the loop will execute once, +// prepends "0" and makes "00". Now "00".length is 2, which is not less +// than 2, so the loop will stop and "00" will be returned. // 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 +// For seconds = 61, the last call to pad is for remainingSeconds. +// AS calculated in (b), remainingSeconds is 1. So the value assigned +// to num when pad is called for the last time is 1. This is because +// remainingSeconds is calculated as seconds % 60, which gives the +// remainder of 61 divided by 60, resulting in 1. Therefore, num is +// assigned the value of 1 during the last call to pad in this program. + // 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 +// As calculated in (d), num is 1. The pad function converst num to +// a string ("1") and then checks its length. Since the length is 1, +// it prepends "0" to make it to "01". So, the return value of pad +// when called for the last time is "01". This is because the while loop +// in pad will execute once. From 1450d259ea0a291a63cba6575d216f6fe8d8aa74 Mon Sep 17 00:00:00 2001 From: Remi Marcelle Date: Thu, 21 May 2026 14:45:53 +0100 Subject: [PATCH 11/11] verify edge cases with console.log --- Sprint-2/5-stretch-extend/format-time.js | 55 ++++++++++++++++++------ 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..9a743bb455 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,23 +3,52 @@ // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); + const hours = Number(time.slice(0, 2)); // captures the hours part of the time string, e.g. "08" from "08:00" + const minutes = time.slice(2); // captures the full minutes part e.g. ":30", not just ":00" + if (hours === 0) { + return `12${minutes} am`; // midnight edge case - 00:00 should be 12:00 am, not 0:00 am + } + if (hours === 12) { + return `12${minutes} pm`; // noon edge case - 12:00 should be 12:00 pm, not 12:00 am + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}${minutes} pm`; // afternoon - subtract 12 to convert to 12-hour format } - return `${time} am`; + return `${time} am`; // morning hours 1-11, no change needed } +// === means strict equality - checks that both the value AND the type match +// e.g. 1 === "1" is false (number vs string), but 1 === 1 is true +// console.assert checks if the condition is true - if not, it prints the error message + +// Existing tests const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); +const targetOutput = "08:00 am"; // normal morning time - no change expected +console.assert(currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}`); const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +const targetOutput2 = "11:00 pm"; // normal afternoon time - 23 - 12 = 11 +console.assert(currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}`); + +// Additional tests for edge cases +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; // midnight - 00:00 hours should display as 12:00 am +console.assert(currentOutput3 === targetOutput3, `current output: ${currentOutput3}, target output: ${targetOutput3}`); + +const currentOutput4 = formatAs12HourClock("12:00"); +const targetOutput4 = "12:00 pm"; // noon - sits on the boundary between am and pm +console.assert(currentOutput4 === targetOutput4, `current output: ${currentOutput4}, target output: ${targetOutput4}`); + +const currentOutput5 = formatAs12HourClock("01:00"); +const targetOutput5 = "01:00 am"; // earliest normal morning hour +console.assert(currentOutput5 === targetOutput5, `current output: ${currentOutput5}, target output: ${targetOutput5}`); + +const currentOutput6 = formatAs12HourClock("13:30"); +const targetOutput6 = "1:30 pm"; // tests that actual minutes (:30) are preserved, not hardcoded as :00 +console.assert(currentOutput6 === targetOutput6, `current output: ${currentOutput6}, target output: ${targetOutput6}`); + +console.log(formatAs12HourClock("08:00")); // "08:00 am" +console.log(formatAs12HourClock("23:00")); // "11:00 pm" +console.log(formatAs12HourClock("00:00")); // "12:00 am" +console.log(formatAs12HourClock("12:00")); // "12:00 pm" +console.log(formatAs12HourClock("13:30")); // "1:30 pm" \ No newline at end of file