From ff84a769e23749f80cd14762895655df3462c2c1 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:43:35 +0100 Subject: [PATCH 01/12] Fix redeclaration error in capitalise function Renamed variable 'str' to 'result' to avoid redeclaration error. --- Sprint-2/1-key-errors/0.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..2efb691c03 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,23 @@ // Predict and explain first... -// =============> write your prediction here - +// =============> I presume the str is repeatedly declared, first as a parameter and again as a variable. // 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 -// =============> write your new code here + +// =============> str is already declared in the parameter and shouldn't be declared inside the function. The error says it's been declared already for this reason. Therefore we need to rename the str inside the function. +/* ==========> function capitalise(str) { +let result = `${str[0].toUpperCase()}${str.slice(1)}; +return result; +} +OR +function capitalise(str) { +return `${str[0].toUpperCase()}${str.slice(1)}`; +} +*/ + From df6c2f80d47f11fa85445b9b3ddeafcf4bd3560b Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:44:31 +0100 Subject: [PATCH 02/12] Correct variable scope and console log usage Fixed variable declaration issue in convertToPercentage function and updated console log. --- Sprint-2/1-key-errors/1.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..ff7fcad326 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,19 @@ // Predict and explain first... - // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> The error will appear because we are trying to log the variable decimalNumber in the function. We have to call the function at this point. Besides to that, the decimalNumber is again declared inside the function where it shouldn't be. + // 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; } -console.log(decimalNumber); -// =============> write your explanation here +console.log(decimalNumber); -// Finally, correct the code to fix the problem -// =============> write your new code here From 416eee5f7729b3c81c409bf368610b3884aaca8d Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:45:42 +0100 Subject: [PATCH 03/12] Comment out erroneous code and note declaration issue Commented out the incorrect code and added a note about the declaration error. --- Sprint-2/1-key-errors/1.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index ff7fcad326..5ed47443e8 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -16,4 +16,19 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); +// =============> it says decimalNumber has aleady been declared for the above reason. + + +// Finally, correct the code to fix the problem +/* ===========> function convertToPercentage(decimalNumber) { + let decimalNum = 0.5; + const percentage = `${decimalNum * 100}%`; + + + return percentage; +} +const result = convertToPercentage(); +console.log(result); +*/ + From ee8e2c7a5052cf5bb6ff49dee95498a29b681985 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:46:40 +0100 Subject: [PATCH 04/12] Fix parameter in square function and add explanations Corrected the function parameter and added comments explaining the error. --- Sprint-2/1-key-errors/2.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..f8c6c83a00 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,30 @@ - // Predict and explain first BEFORE you run any code... + // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here + +// =============> It is going to throw an error message as 3 is written in incorrect position. That position is set only for parameters, not arguments. + function square(3) { return num * num; } -// =============> write the error message here -// =============> explain this error message here +// =============> in the node repl, it says unexpected number, as the function always expects a parameter to be declared in its definition. + + +// =============> The error is already described above. + // Finally, correct the code to fix the problem -// =============> write your new code here + +/* ===========> function square(num) { +return num * num; +} +*/ + From 03db3d4ec9cc1a0f0e3854a6bda5d17cbd2ae62f Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:50:59 +0100 Subject: [PATCH 05/12] Fix multiply function to return the product The multiply function now returns the product of a and b, fixing the undefined output issue. --- Sprint-2/2-mandatory-debug/0.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..5e66cb65b0 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,23 @@ // Predict and explain first... + // =============> write your prediction here + function multiply(a, b) { console.log(a * b); } + console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here + +// =============> The function is only printing values on the console but not returning the value to the caller. Henceforth, it outputs both 320 and undefined on the console. 320 is the result of the console.log(a * b) inside the function, whereas undefined comes from the outer console.log(...) as it tries to bring forth and print the result of the function multiply(a, b). But the function is not returning any value and hence undefined appears on 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)}`); + From e974ec104fc56fdc55e805370b1c2c969a01423b Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:52:26 +0100 Subject: [PATCH 06/12] Correct sum function to return the sum of two numbers Fixed the sum function to return the correct value of a + b. --- Sprint-2/2-mandatory-debug/1.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..f43038d82d 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,19 @@ // Predict and explain first... -// =============> write your prediction here +// =============> As the function is not retuning anything, we're going to see undefined on the console. + 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 uses parameters a and b. It calculates the addition of a and b. Supposedly it should return the result and then we can use the value of the function, but it is returning nothing at all as return is closed without being specified. // 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 ${(10, 32)}`); +*/ From 99bac7e7d35115c7591bb7afef60581dd5989750 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:53:29 +0100 Subject: [PATCH 07/12] Fix getLastDigit function to accept input parameter Refactor getLastDigit function to accept a parameter and update comments for clarity. --- Sprint-2/2-mandatory-debug/2.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..4d27b7f450 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,39 @@ // Predict and explain first... + // Predict the output of the following code: -// =============> Write your prediction here +// =============> the function is going to return error as the /1 is not valid expression or syntax + const num = 103; -function getLastDigit() { + +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)}`); +console.log(`The last digit of 7247 is ${getLastDigit(7274)}`); + // Now run the code and compare the output to your prediction -// =============> write the output here +/* ===========> function getLastDigit() { +return num.toString().slice(-1); + */ // Explain why the output is the way it is -// =============> write your explanation here +// =============> this is because the /1 is not defined and not valid in the system of javaScript languague, so it throws an error. Morever because the parameter for the function is not defined, the function takes the global variable scope to run the code inside it. So obviously, we need to put down a parameter which is num to have the function work for any number other than the global variable scope. // 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 + + + From 8fe1b932348a284b7f464c861637a52a8bef203a Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:55:32 +0100 Subject: [PATCH 08/12] Implement BMI calculation function --- Sprint-2/3-mandatory-implement/1-bmi.js | 7 +++++-- 1 file changed, 5 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..343ba7d3e8 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 + const bmi = weight / (height * height); + return Math.round(bmi * 10) / 10; +} + +console.log(`The BMI of Yonatan is ${calculateBMI(65, 1.65)}`); From 79098f2954328b04920bb481f0fcdd6071c334c3 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:56:23 +0100 Subject: [PATCH 09/12] Add snakeCaseString function for string conversion Implement snakeCaseString function to convert strings to snake case and uppercase. --- Sprint-2/3-mandatory-implement/2-cases.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..53b6da9c59 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,7 @@ // 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 snakeCaseString(str) { + return str.split(" ").join("_").toUpperCase(); +} +console.log(snakeCaseString("lord of the rings")); From 81f329ad85399dddbc43bda575316e109f15f95c Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:57:20 +0100 Subject: [PATCH 10/12] Add toPounds function for currency conversion Implemented a function to convert pence to pounds format. --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 24 +++++++++++++++++++ 1 file changed, 24 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..c037bdce0e 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 +const penceString = "399p"; + +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("39p")); +console.log(toPounds("100p")); +console.log(toPounds("4000p")); +console.log(toPounds("9p")); From ce9cf2e61b1571d7528c9cb2688a800d149ed41d Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 11:58:34 +0100 Subject: [PATCH 11/12] Implement time formatting and padding functions --- Sprint-2/4-mandatory-interpret/time-format.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..e75d3ee4ea 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -5,34 +5,36 @@ function pad(num) { } return numString; } +console.log(pad()); function formatTimeDisplay(seconds) { const remainingSeconds = seconds % 60; const totalMinutes = (seconds - remainingSeconds) / 60; const remainingMinutes = totalMinutes % 60; const totalHours = (totalMinutes - remainingMinutes) / 60; - return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(61)); +// // 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 // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> three times in terms of hours, minutes and 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 +// =============> 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 +// =============> the value assigned to num will be 1, and because the remaining seconds after 61 % 60 will be 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 +// =============> the return value of pad when it is called for the last time is "01". Because inside pad(), 1 is less than 2, and will be padded to "0" and will become "01". From 796b2ee6f000c11518020e96ff60d9eb1404ebb9 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 12:00:29 +0100 Subject: [PATCH 12/12] Fix time formatting for midnight, hours with minutes and pm/am periods --- Sprint-2/5-stretch-extend/format-time.js | 70 +++++++++++++++++++++--- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..9adf74eaed 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -10,16 +10,72 @@ function formatAs12HourClock(time) { return `${time} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput = formatAs12HourClock("00:00"); +const targetOutput = "12:00 am"; console.assert( currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + `current output: ${currentOutput}, target output: ${targetOutput}`, + "midnight should be 12:00 am" ); - -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +const currentOutput2 = formatAs12HourClock("00:30"); +const targetOutput2 = "12:30 am"; console.assert( currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + `current output: ${currentOutput2}, target output: ${targetOutput2}`, + "00:30 should be 12:30 am" +); + +const currentOutput3 = formatAs12HourClock("01:30"); +const targetOutput3 = "01:30 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}`, + "01:30 should be 01:30 am" +); +const currentOutput4 = formatAs12HourClock("11:59"); +const targetOutput4 = "11:59 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}`, + "11:59 should be 11:59 am" +); +const currentOutput5 = formatAs12HourClock("12:30"); +const targetOutput5 = "12:30 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}`, + "12:30 should be 12:30 pm" +); +const currentOutput6 = formatAs12HourClock("15:45"); +const targetOutput6 = "03:45 pm"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}`, + "15:45 should be 03:45 pm" +); +const currentOutput7 = formatAs12HourClock("23:30"); +const targetOutput7 = "11:30 pm"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}`, + "23:30 should be 11:30 pm" ); +// fixing the bugs found: +function formatAs12HourClock(time) { + const hours24 = Number(time.slice(0, 2)); // 23:00 ---> "23" + const minutes = time.slice(-2); // 23:00 ---> "00" + + let period = "am"; + let hours12 = hours24; + + if (hours24 === 0) { + hours12 = 12; // midnight + } else if (hours24 === 12) { + period = "pm"; // noon + } else if (hours24 > 12) { + hours12 = hours24 - 12; + period = "pm"; + } + + return `${String(hours12).padStart(2, "0")}:${minutes} ${period}`; // converts the return output to string type and padded to 2 length. +}