From e1af344e710f662ce0b211390ec697d86ac981cb Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 21 Oct 2025 13:32:38 +0100 Subject: [PATCH 01/12] Completed 0.js exercise --- Sprint-2/1-key-errors/0.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..14657ef5f 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,6 +1,10 @@ // Predict and explain first... // =============> write your prediction here +// I predict that the function will capitalise the first character of the string +// And then return a string using string interpolation using the first character of the string +// and a slice of the remaining characters of the string excluding the first character. + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +14,13 @@ function capitalise(str) { } // =============> write your explanation here + +// An error is thrown because of the variable name collision. The function has a parameter called `str` +// but in the body of the function, a variable `str` is declared and that conflicts with the parameter. + // =============> write your new code here + +function upperCased(string) { + const capitalised = `${string[0].toUpperCase()}${string.slice(1)}`; + return capitalised; +} \ No newline at end of file From 1028541bd4f7dc41e446c4b5ed85a09e55981e6e Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 21 Oct 2025 13:39:02 +0100 Subject: [PATCH 02/12] Completed 1.js exercise --- Sprint-2/1-key-errors/1.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..5884be2e7 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -3,6 +3,9 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// Because of the variable name collision. The function parameter is called decimalNumber +// but another decimalNumber is declared inside the body of the function. + // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -16,5 +19,14 @@ console.log(decimalNumber); // =============> write your explanation here +// The console prints: SyntaxError: Identifier 'decimalNumber' has already been declared + // Finally, correct the code to fix the problem // =============> write your new code here + +function percentaged(decimalNumber) { + const constant = 0.5; + const percentage = `${constant * 100}%`; + + return percentage; +} \ No newline at end of file From f342a4cbb909efbfa0d7889e1d7adfd121c82abf Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 21 Oct 2025 13:43:57 +0100 Subject: [PATCH 03/12] Completed 2.js exercise --- Sprint-2/1-key-errors/2.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..46078e1af 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -5,16 +5,26 @@ // =============> write your prediction of the error here +// The function will not execute because of the undefined variable `num` and the parameter `3`, +// which is not allowed to be defined with just numbers. There must be letters to define a variable +// but can begins with numbers. + function square(3) { return num * num; } // =============> write the error message here +// SyntaxError: Unexpected number + // =============> explain this error message here +// It's referring to the function parameter `3` + // Finally, correct the code to fix the problem // =============> write your new code here - +function squared(value) { + return value * value; +} \ No newline at end of file From 01dccfc4206ec7505ee68860eb6e7deecaf9e5a5 Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 21 Oct 2025 13:53:29 +0100 Subject: [PATCH 04/12] Completed 0.js exercise --- Sprint-2/2-mandatory-debug/0.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..52c2de4f9 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -2,6 +2,8 @@ // =============> write your prediction here +// The function prints to the console by multiplying the parameter `a` and `b` + function multiply(a, b) { console.log(a * b); } @@ -10,5 +12,14 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// I missed line 11, the function does indeed prints to the console but returns nothing +// which explains `undefined` in the string interpolation of the function + // Finally, correct the code to fix the problem // =============> write your new code here + +function product(a, b) { + const result = a * b; + console.log(result) + return result; +} \ No newline at end of file From 4bf33763cae2da0d60b6c8cf1e8e63ff96dd5ccc Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 21 Oct 2025 14:00:23 +0100 Subject: [PATCH 05/12] Completed 1.js exercise --- Sprint-2/2-mandatory-debug/1.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..16cb07d0b 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,8 @@ // Predict and explain first... // =============> write your prediction here +// It will not say `undefined` but will not add parameter `a` and `b` + function sum(a, b) { return; a + b; @@ -9,5 +11,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here + +// The console prints: The sum of 10 and 32 is undefined + // Finally, correct the code to fix the problem // =============> write your new code here + +function add(a, b) { + return a + b; +} \ No newline at end of file From c929c3c64eb01ca3bb073a24f75e2a7cbc67ad6c Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 21 Oct 2025 14:05:20 +0100 Subject: [PATCH 06/12] Completed 2.js exercise --- Sprint-2/2-mandatory-debug/2.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..6b1bc64a8 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,10 +3,12 @@ // Predict the output of the following code: // =============> Write your prediction here +// It will convert 103 number into a String representation and then will either do no manipulations or invert the string + const num = 103; -function getLastDigit() { - return num.toString().slice(-1); +function getLastDigit(digit) { + return digit.toString().slice(-1); } console.log(`The last digit of 42 is ${getLastDigit(42)}`); @@ -22,3 +24,7 @@ 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 + +// The function getLastDigit() has no parameters and referenced a globally scoped constant `num`. +// In order for it to `work properly`, a parameter is required and referencing it in order to +// return an expected value \ No newline at end of file From eae3b79f2820e6d851534fbd1e12480065a53fb9 Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 21 Oct 2025 14:15:17 +0100 Subject: [PATCH 07/12] Completed 1-bmi.js exercise --- Sprint-2/3-mandatory-implement/1-bmi.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..573a6a703 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,6 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + const result = weight / Math.pow(height, 2); + return result.toFixed(1); } \ No newline at end of file From f6354f06f936cccbf6b8091e0f05a763a309262e Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 21 Oct 2025 14:36:46 +0100 Subject: [PATCH 08/12] Completed 2-cases.js exercise --- Sprint-2/3-mandatory-implement/2-cases.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..dfa0b6ba6 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,12 @@ // 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(string) { + let result = `${string}` + .split(" ") + .map(word => word.toUpperCase()) + .reduce((accumulator, currentValue) => accumulator + "_" + currentValue); + + return result; +} \ No newline at end of file From 38d97c28cc6e574d8ed664cffb0e196fb63a9428 Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 21 Oct 2025 14:52:30 +0100 Subject: [PATCH 09/12] Completed 3-to-pounds.js exercise --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..4e4847d90 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,23 @@ // 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}`; +} From 543ba9d73269659e1ed565f9bc5f3704d74b7cf4 Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 21 Oct 2025 15:06:28 +0100 Subject: [PATCH 10/12] Completed time-format.js exercise --- Sprint-2/4-mandatory-interpret/time-format.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..800f5d600 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,6 +11,9 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +const number = 61; +console.log(formatTimeDisplay(number)); + // 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 @@ -19,16 +22,26 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// 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 + // c) What is the return value of pad 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 +// 1 + // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer // =============> write your answer here + +// 01 From 75172a0acaebbfe9c8fe9a7c5417114a31502f85 Mon Sep 17 00:00:00 2001 From: Ammad Date: Wed, 22 Oct 2025 15:21:45 +0100 Subject: [PATCH 11/12] Completed format-time.js exercise --- Sprint-2/5-stretch-extend/format-time.js | 43 +++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..9221df616 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,11 +3,18 @@ // 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)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + let hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + + if (hours === 24) { + hours = 0; } - return `${time} am`; + + const period = hours < 12 ? "am" : "pm"; + hours = hours % 12 || 12; + const paddedHours = String(hours).padStart(2, "0"); + + return `${paddedHours}:${minutes} ${period}`; } const currentOutput = formatAs12HourClock("08:00"); @@ -23,3 +30,31 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("24:00"); +const targetOutput4 = "12:00 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("12:00"); +const targetOutput5 = "12:00 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + +const currentOutput6 = formatAs12HourClock("09:41"); +const targetOutput6 = "09:41 am"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); From 851893ccadebe5a13d6cfa749e8102ec9c6f3a74 Mon Sep 17 00:00:00 2001 From: Ammad Date: Tue, 28 Oct 2025 19:32:43 +0000 Subject: [PATCH 12/12] Address the issues from the pull request comments --- Sprint-2/1-key-errors/1.js | 7 ++----- Sprint-2/2-mandatory-debug/0.js | 6 ++---- Sprint-2/3-mandatory-implement/1-bmi.js | 9 +++++---- Sprint-2/3-mandatory-implement/2-cases.js | 9 ++------- Sprint-2/4-mandatory-interpret/time-format.js | 8 ++++---- Sprint-2/5-stretch-extend/format-time.js | 2 +- 6 files changed, 16 insertions(+), 25 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index 5884be2e7..ec4b3e97b 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -25,8 +25,5 @@ console.log(decimalNumber); // =============> write your new code here function percentaged(decimalNumber) { - const constant = 0.5; - const percentage = `${constant * 100}%`; - - return percentage; -} \ No newline at end of file + return `${decimalNumber * 100}%`; +} diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index 52c2de4f9..a513b1479 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -19,7 +19,5 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your new code here function product(a, b) { - const result = a * b; - console.log(result) - return result; -} \ No newline at end of file + return a * b; +} diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 573a6a703..60392f813 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,7 +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 - const result = weight / Math.pow(height, 2); - return result.toFixed(1); -} \ No newline at end of file + // return the BMI of someone based off their weight and height + const result = weight / Math.pow(height, 2); + const bmiString = result.toFixed(1); + return Number(bmiString); +} diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index dfa0b6ba6..7e2ec7fa5 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -16,10 +16,5 @@ // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase function toUpperSnakeCase(string) { - let result = `${string}` - .split(" ") - .map(word => word.toUpperCase()) - .reduce((accumulator, currentValue) => accumulator + "_" + currentValue); - - return result; -} \ No newline at end of file + return String(string).toUpperCase().replaceAll(" ", "_"); +} diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 800f5d600..99829ccad 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -29,19 +29,19 @@ console.log(formatTimeDisplay(number)); // b) What is the value assigned to num when pad is called for the first time? // =============> write your answer here -// 0 +// "0" // c) What is the return value of pad is called for the first time? // =============> write your answer here -// 00 +// "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 -// 1 +// "1" // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer // =============> write your answer here -// 01 +// "01" diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 9221df616..efef00253 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,7 +4,7 @@ function formatAs12HourClock(time) { let hours = Number(time.slice(0, 2)); - const minutes = time.slice(3, 5); + const minutes = time.slice(-2); if (hours === 24) { hours = 0;