From 0fc35ede51bb43bc8ae013ace3ea8dc266b2c71c Mon Sep 17 00:00:00 2001 From: Zadri Date: Sat, 8 Nov 2025 21:20:29 +0000 Subject: [PATCH 01/14] Interpret error message and explain cause in 1-key-errors/0.js --- Sprint-2/1-key-errors/0.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..90ebd6b4f 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,6 @@ // Predict and explain first... // =============> write your prediction here +// It will show a error // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -8,6 +9,13 @@ function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +capitalise("Hello"); // =============> write your explanation here +// SyntaxError: Identifier 'str' has already been declared - This error means that the variable 'str' was declared more than once in the same scope. +// JavaScrip doesn't allow that - once a variable name is declared with let or const, you can't declare it again. // =============> write your new code here +function capitalise(str) { + return str[0].toUpperCase() + str.slice(1); +} + From f4e38d3d3bde27ad2a45c00bab758b797104b830 Mon Sep 17 00:00:00 2001 From: Zadri Date: Sat, 8 Nov 2025 22:20:51 +0000 Subject: [PATCH 02/14] Fix and explain redeclaration SyntaxError in convertToPercentage (1-key-errors/1.js) --- Sprint-2/1-key-errors/1.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..31cbd537c 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,6 +2,7 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// I predict that this program will throw a error message - due to the variable decimalNumber being redeclared // Try playing computer with the example to work out what is going on @@ -15,6 +16,22 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// Function covertToPercentage(decimalNumber) - it has a parameter named decimalNumber +// Inside the function: const decimalNumber = 0.5; - this redeclares the same variable name that's already used for the parameter. +// That causes a error: SyntaxError: Identifier 'decimalNumber' has already been declared - because you can't declare a const (or let) +// with the same name as a parameter inside the same function scope. +// JavaScript doesn't allow us to declare a new variable with the same name in the same scope, so it caused a error // 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)); + +// Function decimalNumber = 0.5 +// It calculates 0.5 * 100 = 50 +// It returns "50%" +// console logs 50% From 8a162e393efaa68d5640957aeb01caf39f80618a Mon Sep 17 00:00:00 2001 From: Zadri Date: Sat, 8 Nov 2025 22:35:53 +0000 Subject: [PATCH 03/14] Fix and explain 'unexpected number' SyntaxError in square() (1-key-errors/2.js) --- Sprint-2/1-key-errors/2.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..aac154655 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -10,11 +10,16 @@ function square(3) { } // =============> write the error message here - +// SyntaxError: Unexpected number // =============> explain this error message here - +// The "Unexpected number" // Finally, correct the code to fix the problem - +// The "Unexpected number" error arises when a number is improperly positioned or used within JvaScript code // =============> write your new code here + function square(num){ + return num * num; +} + +console.log(square); From 2bf34878fd4d1dad860f470d91b9f206abfd8393 Mon Sep 17 00:00:00 2001 From: Zadri Date: Sat, 8 Nov 2025 23:54:01 +0000 Subject: [PATCH 04/14] Fix undefined return in sum() and explain cause (2-mandatory-debug/0.js --- Sprint-2/2-mandatory-debug/0.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..d17428e24 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,20 @@ // Predict and explain first... // =============> write your prediction here - +// It will show a undefined error 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 - +// The function printed the answer but did't return it, so when it was used in the template string, it came out as 'undefined.' // Finally, correct the code to fix the problem // =============> write your new code here + function multiply(a, b) { + return a * b; // Once I put a * b on the same line as return, it worked +} + +console.log(` The sum of 10 and 32 is ${sum(10, 32)}`); + From 54ca144c558ab08b5e60b7b5e738c73cece48b9b Mon Sep 17 00:00:00 2001 From: Zadri Date: Sun, 9 Nov 2025 12:33:10 +0000 Subject: [PATCH 05/14] add changes to 2-mandatory-debug/0.js --- Sprint-2/2-mandatory-debug/0.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index d17428e24..4b11f2a88 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -13,7 +13,7 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)} // Finally, correct the code to fix the problem // =============> write your new code here function multiply(a, b) { - return a * b; // Once I put a * b on the same line as return, it worked + return a * b; } console.log(` The sum of 10 and 32 is ${sum(10, 32)}`); From ff147212829be1b86b748dd7884224b880ce3154 Mon Sep 17 00:00:00 2001 From: Zadri Date: Sun, 9 Nov 2025 12:46:43 +0000 Subject: [PATCH 06/14] add prediction and explanation --- Sprint-2/2-mandatory-debug/1.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..89fa511e0 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,6 @@ // Predict and explain first... // =============> write your prediction here +// I predict it will show a undefined error function sum(a, b) { return; @@ -9,5 +10,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The problem was the semicolon after return, which made the function stop before adding the numbers // Finally, correct the code to fix the problem // =============> write your new code here +function sum(a, b){ + return a + b; // Once I put a + b on the same line as return, it worked +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); From 97dc683cd5bf62db9968cdeb8542746112d665e4 Mon Sep 17 00:00:00 2001 From: Zadri Date: Sun, 9 Nov 2025 19:24:21 +0000 Subject: [PATCH 07/14] predict the output --- Sprint-2/2-mandatory-debug/2.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..4ee83bf4d 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,7 @@ // Predict the output of the following code: // =============> Write your prediction here - +// Since getLastDigit() does not declare any parameters, the arguments passed when calling it are going to be ignored const num = 103; function getLastDigit() { @@ -15,10 +15,25 @@ 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 +// The function getLastDigit() does not have any parameters, but it is being called with arguments: +// getLastDigit(42), getLastDigit(105), and getLastDigit(806). Inside the function, it always uses the global const num, which is set to 103. +// The means every call ignores the last digit of 103. Which the '3' + + // 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 2f8d74ec01ce01331759726975587c78a86d3d93 Mon Sep 17 00:00:00 2001 From: Zadri Date: Sun, 9 Nov 2025 19:45:35 +0000 Subject: [PATCH 08/14] add and explain calculateBMI() function to compute BMI to one decimal place --- Sprint-2/3-mandatory-implement/1-bmi.js | 9 ++++++++- 1 file changed, 8 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..43614ac74 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,11 @@ 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 bmi.toFixed(1); +} + +console.log(`Your BMI is ${calculateBMI(68, 1.75)}`); +// How it works: function calculateBMI(weight, height) +// Parameters: weight - number, person's weight in kilograms height - number, person's height in meters +// Returns: the body mass index (BMI), rounded to one decimal place \ No newline at end of file From c52e0875d5031e59995ccde9b0f638f97c4f3cfd Mon Sep 17 00:00:00 2001 From: Zadri Date: Sun, 9 Nov 2025 19:57:41 +0000 Subject: [PATCH 09/14] add using String.toUpperCase() to convert greeting to uppercase --- Sprint-2/3-mandatory-implement/2-cases.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..c6eeec8dd 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,6 @@ // 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 +const greeting = "Hello there." + +console.log(greeting.toUpperCase()); \ No newline at end of file From 0de1e194d343caabd9daf0c3a77fbb42731c6729 Mon Sep 17 00:00:00 2001 From: Zadri Date: Sun, 9 Nov 2025 20:33:04 +0000 Subject: [PATCH 10/14] turn interpret/to-pounds.js into a reuseable block of code --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 10 ++++++++++ 1 file changed, 10 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..eaa598a66 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,13 @@ // 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 formaPenceToPounds(penceString){ + const penceStringWithoutTrailingP = penceString.substring(0, penceString.length -1); + const pounds = paddedPenceNumberString.slice(0, -2); + const pence = paddedPenceNumberString.slice(-2); + return `£${pounds}.${pence}`; +} + +console.log(formatPenceToPounds("9p")); +console.log(formatPenceToPounds("39p")); +console.log(formatPenceToPounds("399")); \ No newline at end of file From a451edae52c82f38608e433c1453c1e1c12cd159 Mon Sep 17 00:00:00 2001 From: Zadri Date: Sun, 9 Nov 2025 23:35:40 +0000 Subject: [PATCH 11/14] add formatTimeDisplay() answers explaining pad() calls and returning values --- Sprint-2/4-mandatory-interpret/time-format.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..f8df8e934 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,27 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// Three 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 - +/* The first time pad() run, its used for hours part of time. + Because 61 seconds equals 0 hours, the value given to num is 0. */ // c) What is the return value of pad is called for the first time? // =============> write your answer here +/* The first time pad() runs, it's formatting the hours part of time. +Since the hours value is 0, it turns that into the string '00' by adding a leading zero before returning it. */ // 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 +/* When the function runs with 61 seconds, the last time pad() is called is for the second part of the time. +Since 61 seconds has 1 second left over after making a full minute, the value passed into pad() is 1. +Inside the function, num equals 1, and it gets turned into '01' so the final time shows as 00:01:01. */ // 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 +/* When pad() is called for the last time, num is 1 because there's 1 second left over. + The function turns that number into the string '01' by adding a leading zero. + That '01' is the value that gets returned and used as the seconds part of the final time display (00:01:01). */ From 083a9d288378aecf19368945570ba8fb312813b8 Mon Sep 17 00:00:00 2001 From: Zadri Date: Mon, 10 Nov 2025 17:13:46 +0000 Subject: [PATCH 12/14] add and test formatAs12HourClock function with passing assertions --- Sprint-2/5-stretch-extend/format-time.js | 53 +++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..9c1ec09b0 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,7 +2,7 @@ // Make sure to do the prep before you do the coursework // 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) { + /*function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); if (hours > 12) { return `${hours - 12}:00 pm`; @@ -22,4 +22,55 @@ const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` +); */ + +// The function ignores the minutes and did'not handle midnight (00:00) or noon (12:00) properly. + // Better version: + function formatAs12HourClock(time) { + const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + + if (hours == 0) return `12:${minutes} am`; + if (hours < 12) return `${time} am`; + if (hours === 12) return `${time} pm`; + + return `${hours - 12}:${minutes} pm`; +} +const currentOutput1 = formatAs12HourClock("08:00"); +const targetOutput1 = "08:00 am"; +console.assert( + currentOutput1 === targetOutput1, + `current output: ${currentOutput1}, target output: ${targetOutput1}` ); +// Test failed: due to a typo +const currentOutput2 = formatAs12HourClock("23:00"); +const targetOutput2 = "11:00 pm"; +console.assert( + currentOutput2 === targetOutput2, + `current output: ${currentOutput2}, target output: ${targetOutput2}` + +); + +const currentOutput3 = formatAs12HourClock("13:00"); +const targetOutput3 = "1:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3} target output: {targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("15:00"); +const targetOutput4 = "3:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4} target output: {targetOutput4}` +); + +console.log(formatAs12HourClock("08:00")); +console.log(formatAs12HourClock("23:00")); +console.log(formatAs12HourClock("15:00")); +console.assert(formatAs12HourClock("00:00") === "12:00 am"); +console.assert(formatAs12HourClock("12:00") === "12:00 pm"); + +// The test passed, so my function gave the expected results. +// All the console.assert() tests passed. +// formatAs12HourClock() function works correctly for the inputs I tested. \ No newline at end of file From f4c8d8569917dfe794e495d25dd0d6126feb542b Mon Sep 17 00:00:00 2001 From: Zadri Abdule Date: Thu, 13 Nov 2025 14:19:39 +0000 Subject: [PATCH 13/14] Fix console log to use multiply function --- Sprint-2/2-mandatory-debug/0.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index 4b11f2a88..a91426549 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -16,5 +16,5 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)} return a * b; } -console.log(` The sum of 10 and 32 is ${sum(10, 32)}`); +console.log(`The result of multipying 10 and 32 is ${multiply(10, 32)}`); From 2776d16024f351ea4c6665b8a6df5fe2087cecc4 Mon Sep 17 00:00:00 2001 From: Zadri Abdule Date: Thu, 13 Nov 2025 14:21:58 +0000 Subject: [PATCH 14/14] Fix typo in function name from forma to format --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index eaa598a66..ad8faaf73 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,7 +4,7 @@ // 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 formaPenceToPounds(penceString){ +function formatPenceToPounds(penceString){ const penceStringWithoutTrailingP = penceString.substring(0, penceString.length -1); const pounds = paddedPenceNumberString.slice(0, -2); const pence = paddedPenceNumberString.slice(-2); @@ -13,4 +13,4 @@ function formaPenceToPounds(penceString){ console.log(formatPenceToPounds("9p")); console.log(formatPenceToPounds("39p")); -console.log(formatPenceToPounds("399")); \ No newline at end of file +console.log(formatPenceToPounds("399"));