From a81b202837921f5914323e0639c94cd61e4c47a4 Mon Sep 17 00:00:00 2001 From: Pirikita Date: Thu, 23 Oct 2025 18:09:07 +0200 Subject: [PATCH 01/12] - Guessed what will happen based on the original code; - tested the original code; confirmed prediction; - modified the code to fix issues; - tested the modified code to confirm it works as expected. --- Sprint-2/1-key-errors/1.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..e8c190b72 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,30 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here - will not run because decimalNumber is declared twice // 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); -// =============> write your explanation here +// =============> write your explanation here - An error occur because we are declaring the decimalNumber variable twice. +//we declare it in the function and in the first const. +//we are also logging decimalNumber we should log the function // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage() { + const decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} +console.log(convertToPercentage()); From aaeaa86e34c33ba1556f880df0177fb0f8f937fd Mon Sep 17 00:00:00 2001 From: Pirikita Date: Thu, 23 Oct 2025 18:26:33 +0200 Subject: [PATCH 02/12] Predicted the error registered the error wrote the new code in order to be flexible --- Sprint-2/1-key-errors/2.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..f8359050c 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,26 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> write your prediction of the error here - I believe that square is not valid because 3 is not a variable -function square(3) { - return num * num; -} +//ORIGINAL CODE +//function square(3) { +// return num * num; +//} +//console.log(square(3)); -// =============> write the error message here +// =============> write the error message here - SyntaxError: Unexpected number -// =============> explain this error message here +// =============> explain this error message here - // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} + +console.log(square(38)); + From eb5fd6152c075e284507b185f6d1013e9471c7bf Mon Sep 17 00:00:00 2001 From: Pirikita Date: Sun, 26 Oct 2025 13:12:16 +0100 Subject: [PATCH 03/12] Adapted the code to run any number and return its square. --- Sprint-2/1-key-errors/2.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index f8359050c..8c7008a8c 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,7 +3,7 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here - I believe that square is not valid because 3 is not a variable +// =============> write your prediction of the error here - I believe that square is not valid because 3 is not a valid variable //ORIGINAL CODE //function square(3) { @@ -13,7 +13,7 @@ // =============> write the error message here - SyntaxError: Unexpected number -// =============> explain this error message here - +// =============> explain this error message here - the number 3 is not a valid parameter name. It has to be a name and not a number. // Finally, correct the code to fix the problem @@ -22,7 +22,7 @@ function square(num) { return num * num; } - console.log(square(38)); +//With this code, we can chose any number to square From 18b1bcd0cff2dd73079aa0a0140bb0e92a74704b Mon Sep 17 00:00:00 2001 From: Pirikita Date: Tue, 28 Oct 2025 15:10:10 +0100 Subject: [PATCH 04/12] predicted and explain the problem wrote new code ran tests --- Sprint-2/1-key-errors/0.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..949b92dd8 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,21 @@ // Predict and explain first... -// =============> write your prediction here +// =============> we declare a variable str twice // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring -function capitalise(str) { +/*function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } - -// =============> write your explanation here +*/ +// =============> write your explanation here +// The error occurs because we are trying to declare a variable 'str' inside the function using 'let', // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +console.log(capitalise("beatriz")); // Output: "Beatriz" From 508bdf9f939357026724a9083f3d97eeeb71d09b Mon Sep 17 00:00:00 2001 From: Pirikita Date: Tue, 28 Oct 2025 15:24:07 +0100 Subject: [PATCH 05/12] predicted the output wrote return statement in function did 2 tests with different inputs --- Sprint-2/2-mandatory-debug/0.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..0af5f64d0 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,33 @@ // Predict and explain first... // =============> write your prediction here + // we have a function that multiplies two numbers and logs the result, -function multiply(a, b) { + +/*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 + //we have the console doing the multiplication and loggin it, but we +//are not returning anything from the funcntion. there's why the output is undefined. // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + console.log(a * b); + return a * b; +} + +console.log(`The result of multiplying 27 and 39 is ${multiply(27, 39)}`); + +// Test 1 answer: +// 320 +// The result of multiplying 10 and 32 is 320 +// Test 2 answer: +// 1053 +// The result of multiplying 27 and 39 is 1053 \ No newline at end of file From 316e8667e5a4e39e6550c5aa297946b453c32e40 Mon Sep 17 00:00:00 2001 From: Pirikita Date: Tue, 28 Oct 2025 15:30:29 +0100 Subject: [PATCH 06/12] predicted the output and the error in the code. Explained the error and wrote the corrected code. --- Sprint-2/2-mandatory-debug/0.js | 1 - Sprint-2/2-mandatory-debug/1.js | 18 ++++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index 0af5f64d0..898a6326c 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -6,7 +6,6 @@ /*function multiply(a, b) { console.log(a * b); - return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..cac6abc3e 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 function sum is intended to return the sum of two numbers, a and b. there's an error in the return statement. +// And again it's returning undefined because the return statement is divided. +// Ouput is the sum and then undefined -function sum(a, b) { +/*function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); - +*/ // =============> write your explanation here + // ERROR:The sum of 10 and 32 is undefined + // the return statement should have been written in one line as "return a + b;" + // 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 59 and 72 is ${sum(59 , 72)}`); + +// Test 1 : The sum of 10 and 32 is 42 +// Test 2 : The sum of 59 and 72 is 131 \ No newline at end of file From 9d7a2c296e6726a30f1f88c35e04c716e1bccb83 Mon Sep 17 00:00:00 2001 From: Pirikita Date: Tue, 28 Oct 2025 15:36:25 +0100 Subject: [PATCH 07/12] predicted output and explained why wrote new code. ran test --- Sprint-2/2-mandatory-debug/2.js | 34 +++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..b4f123a83 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,9 +1,18 @@ // Predict and explain first... + // The function getLastDigit is intended to return the last digit of a given number. + // We are declaring the given number as a constant variable 'num' with a value of 103. + // However, the function does not take any parameters and always uses the constant 'num'. + // Therefore, when we call getLastDigit with different numbers (42, 105, 806), + // it still returns the last digit of 103, which is '3'. + -// Predict the output of the following code: +// Predict the output of the following code: // =============> Write your prediction here + // The last digit of 42 is 3 + // The last digit of 105 is 3 + // The last digit of 806 is 3 -const num = 103; +/*const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -12,13 +21,34 @@ function getLastDigit() { 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 + // because we are declaring the num = 103, even if we change the parameter in the console it still considers the num 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)}`); + + +//NEW OUTPUT + //The last digit of 42 is 2 + //The last digit of 105 is 5 + //The last digit of 806 is 6 + // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem From 16fc3dcbf31e4974aa742f66845a4b821385c06f Mon Sep 17 00:00:00 2001 From: Pirikita Date: Wed, 29 Oct 2025 09:56:27 +0100 Subject: [PATCH 08/12] impletment the function calculateBMI that takes in weight (in kg) and height (in meters) and returns --- 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 17b1cbde1..c8bc3aa17 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 +return (weight / (height * height)).toFixed(1); +} + +module.exports = calculateBMI; + +console.log(calculateBMI(70, 1.73)); // Expected output: 23.4 \ No newline at end of file From 2ad09814a84a50e0e3128e34180fca36a200c446 Mon Sep 17 00:00:00 2001 From: Pirikita Date: Wed, 29 Oct 2025 10:12:07 +0100 Subject: [PATCH 09/12] wrote code and comments to better understand --- Sprint-2/3-mandatory-implement/1-bmi.js | 2 +- Sprint-2/3-mandatory-implement/2-cases.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index c8bc3aa17..cb7c8c952 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -19,6 +19,6 @@ function calculateBMI(weight, height) { return (weight / (height * height)).toFixed(1); } -module.exports = calculateBMI; +/* module.exports = calculateBMI; */ console.log(calculateBMI(70, 1.73)); // Expected output: 23.4 \ 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 5b0ef77ad..eccb6805c 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,13 @@ // 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 + +export function toUpperSnakeCase(inputString) { + // Replace spaces with underscores + const stringWithUnderscores = inputString.replace(/ /g, '_'); // / /g means that is replacing all spaces globally with _ + // Convert the string to uppercase + const upperSnakeCaseString = stringWithUnderscores.toUpperCase(); + return upperSnakeCaseString; +} +console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS" From 227d94d5feb6a2093bd6b5ddbfa9d7e1111f276d Mon Sep 17 00:00:00 2001 From: Pirikita Date: Wed, 29 Oct 2025 10:50:04 +0100 Subject: [PATCH 10/12] with the help of AI adapted the code to be reusable. ran tests to ensure it works. --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 32 +++++++++++++++++++ 1 file changed, 32 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..1399ef8ef 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,35 @@ // 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) { + // Remove the trailing 'p' from the input string + const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 + ); + + // Pad with leading zeros to ensure we have at least 3 digits + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + // Extract pounds (all digits except the last two) + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + // Extract pence (last two digits) and ensure it has exactly 2 digits + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + // Return the formatted result + return `£${pounds}.${pence}`; +} + +// Tested the function with different inputs +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("199p")); // £1.99 +console.log(toPounds("50p")); // £0.50 +console.log(toPounds("5p")); // £0.05 +console.log(toPounds("1234p")); // £12.34 \ No newline at end of file From d555d3b9f77cc62aba1fc617355be4747d2cb256 Mon Sep 17 00:00:00 2001 From: Pirikita Date: Wed, 29 Oct 2025 11:33:03 +0100 Subject: [PATCH 11/12] answer to all questions --- Sprint-2/4-mandatory-interpret/time-format.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..0c5b5a235 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,24 +11,33 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(61)); // Expected output: "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 // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> 3 - hours minutes 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 (totalHours = 0 when input is 61 seconds) // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> "00" (0 padded to 2 digits becomes "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 (remainingSeconds = 61 % 60 = 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 +// =============> return value is "01". +/*Explanation: +num = 1 (remainingSeconds) + +num.toString() converts 1 to "1" + +.padStart(2, "0") pads "1" to 2 digits with leading zeros + +*/ From c75d5f2595b30dfd2427b555a4642bf6f9a99340 Mon Sep 17 00:00:00 2001 From: Pirikita Date: Wed, 29 Oct 2025 11:52:20 +0100 Subject: [PATCH 12/12] corrected code; wrote comments with explanations with help of AI; ran tests to ensure correctness. installed jest as a dev dependency and added test script to package.json. --- Sprint-2/4-mandatory-interpret/time-format.js | 2 +- Sprint-2/5-stretch-extend/format-time.js | 27 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 0c5b5a235..d8b22ab51 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,7 +18,7 @@ console.log(formatTimeDisplay(61)); // Expected output: "00:01:01" // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> 3 - hours minutes seconds +// =============> 3 - for hours, minutes and seconds. // Call formatTimeDisplay with an input of 61, now answer the following: diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..e6e40894d 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,14 +4,26 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const minutes = time.slice(3, 5); // Get the minutes part + + let period = "am"; + let displayHours = hours; + + if (hours === 0) { + displayHours = 12; // midnight + } else if (hours === 12) { + period = "pm"; + } else if (hours > 12) { + displayHours = hours - 12; + period = "pm"; } - return `${time} am`; + + return `${displayHours}:${minutes} ${period}`; } +// Test cases const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const targetOutput = "8:00 am"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` @@ -23,3 +35,10 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +console.log("If no errors were logged, all tests passed!"); + +// Export correctly - choose ONE of these: +module.exports = formatAs12HourClock; // Direct export +// OR if you want named exports: +// module.exports = { formatAs12HourClock }; \ No newline at end of file