From e3dd6093bcc25be12a6c17881436e06638ccc043 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Thu, 2 Oct 2025 11:20:12 +0100 Subject: [PATCH 01/19] predict,explain and update the error --- Sprint-2/1-key-errors/0.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..5453fa61e 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,20 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here : There will be an error. // 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; -} + let str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; + +// =============> write your explanation here : Because str has been used(declared) twice. As an input(parameter) to the function(capitalise) and with let. We should used a different variable. -// =============> write your explanation here -// =============> write your new code here +// =============> write your new code here : +/* +function capitalise(str) { + let cap = `${str[0].toUpperCase()}${str.slice(1)}`; + return cap; +} +console.log(capitalise("str")); +*/ \ No newline at end of file From 7f96fdf6f24027d50ea415339a87920c2d167a2a Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Thu, 2 Oct 2025 11:20:56 +0100 Subject: [PATCH 02/19] predict,explain and update the code --- Sprint-2/1-key-errors/1.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..44d38f62d 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,10 +1,12 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here : The variable decimalNumber has been declared. // Try playing computer with the example to work out what is going on +// The old code : +/* function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -13,8 +15,22 @@ function convertToPercentage(decimalNumber) { } console.log(decimalNumber); +*/ -// =============> write your explanation here - +// =============> write your explanation here : +/* +-The variable decimalNumber has been declared within the function as a parameter then declared again using const. +- The variable decimalNumber should be defined first. +*/ // Finally, correct the code to fix the problem // =============> write your new code here + +//The correct code : +const decimalNumber = 0.5; + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +console.log(convertToPercentage(decimalNumber)); From 4e3088e3c24b552199057b23a5f1996fe38f3851 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 5 Oct 2025 15:18:05 +0100 Subject: [PATCH 03/19] Predict and explain the error then fix the code --- Sprint-2/1-key-errors/2.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..723cd05ba 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,22 @@ - // 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 +// =============> write your prediction of the error here : There will be a syntax error because the parameter name cannot be a number. -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 +// =============> explain this error message here : /*The error message shows that there is an unexpected number in the function parameter name. Function parameters must be valid identifiers (like variable names) and cannot be numeric literals.*/ // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(3)); From ce8b66d8cffee60882fcfb9c220801019c8c179e Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 5 Oct 2025 15:24:52 +0100 Subject: [PATCH 04/19] Editing code by adding return --- Sprint-2/2-mandatory-debug/0.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..9a03a3f60 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here : There is no return statement for the parameters. function multiply(a, b) { console.log(a * b); @@ -8,7 +8,12 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here : /* The function multiply does not return any value, it only logs the result to the console. It returns undefined. To fix this, we need to add a return statement in the multiply function. */ // 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 0a2d8005395b00db48a3f4c4353ff3b28b2e5d1f Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 5 Oct 2025 15:31:27 +0100 Subject: [PATCH 05/19] fix the code with some explanations --- Sprint-2/2-mandatory-debug/1.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..70fe5501e 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here : A syntax error for the semicolon after return. function sum(a, b) { return; @@ -8,6 +8,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here : The semicolon after return at end of the statement. So there will be no value returned from 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)}`); From eb2af74d1d9d1be159198e95fef30631b8b0141b Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 5 Oct 2025 15:47:48 +0100 Subject: [PATCH 06/19] Declare a parameter to getLastDigit --- Sprint-2/2-mandatory-debug/2.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..c099ca927 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,7 @@ // Predict and explain first... -// Predict the output of the following code: -// =============> Write your prediction here +// Predict the output of the following code +// =============> Write your prediction here : The code did not set any parameter/s for the function getLastDigit. So it will always take the value of num which is 103 as a default value. const num = 103; @@ -14,11 +14,26 @@ 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 +// =============> write the output here : The output should be as the return line (103."103".3). +/* 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 +// =============> write your explanation here : Since .toString() converts the number to a string. The slice(-1) method extracts the last character from that string.. + // 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 +// >>>> because the pre declared value of num with const. So the function always returns the last digit of 103 which is 3. +// >>>> getLastDigit should take a parameter to work properly. From 7a1579c78225e935a48150e33a2668f33619a177 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 5 Oct 2025 15:52:55 +0100 Subject: [PATCH 07/19] update BMI code --- 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 17b1cbde1..454a8a016 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 bmi.toFixed(1); + // return the BMI of someone based off their weight and height +} +console.log(calculateBMI(75, 1.74)); From 61620bc6af0566c07fdcf726ffe68a43beb23b67 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 5 Oct 2025 15:56:55 +0100 Subject: [PATCH 08/19] upper snake case --- Sprint-2/3-mandatory-implement/2-cases.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..1b2d9c70c 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,9 @@ // 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 toUpperSnake(name) { + const upperCase = name.toUpperCase(); // Convert the string to uppercase + return upperCase.replaceAll(" ", "_"); // Replace all spaces with underscores +} +console.log(snakeItUp("alaa tagi is my name")); From 5711722d2fc163bd059484165037a82e16159307 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 5 Oct 2025 16:02:07 +0100 Subject: [PATCH 09/19] pence To Pound --- 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..9112b5bb0 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(penceToPound) { + const penceStringWithoutTrailingP = penceToPound.substring(0, penceToPound.length - 1); // Remove the P at the end. + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // Make sure there 3 digits at least. + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); // get the pounds part( - the last two digits). + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); // get the pence part( they should be 2 digits). + return `£${pounds}.${pence}`; // The final format. +} +console.log(toPounds("3990p")); +console.log(toPounds("1500p")); +console.log(toPounds("150p")); +console.log(toPounds("100p")); +console.log(toPounds("70p")); +console.log(toPounds("50p")); +console.log(toPounds("0p")); \ No newline at end of file From 544cb8362e26ead956fed9ca02e83abd23ce3c99 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 5 Oct 2025 16:39:08 +0100 Subject: [PATCH 10/19] Update time-format.js --- Sprint-2/4-mandatory-interpret/time-format.js | 10 +++++----- 1 file changed, 5 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..6286c36a8 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,18 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> write your answer here : three times in Line 11 with return. // 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 +// =============> write your answer here : 0, because totalHours is 0. // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here : "00", because num is converted to string and padded to 2 characters with "0". // 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 +// =============> write your answer here : 1, because remainingSeconds is 1 on python tutor. // 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 +// =============> write your answer here : "00", because num is converted to string and padded to 2 characters with "0". That's mean "1" becomes "01". From 8c774bc0c0bc1fcf3794adc9e11f098e18f4ad00 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 5 Oct 2025 16:40:04 +0100 Subject: [PATCH 11/19] Update format-time.js --- Sprint-2/5-stretch-extend/format-time.js | 95 +++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..2bea7a763 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,6 +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. +// The main function: function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); if (hours > 12) { @@ -10,16 +11,106 @@ function formatAs12HourClock(time) { return `${time} am`; } +// Tests: const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` -); +); // It's output is correct const currentOutput2 = formatAs12HourClock("23:00"); const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +); // It's output is correct + +const currentOutput3 = formatAs12HourClock("12:00"); +const targetOutput3 = "12:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); // It's output is incorrect because it returns "12:00 am" instead of "12:00 pm" + +const currentOutput4 = formatAs12HourClock("00:00"); +const targetOutput4 = "12:00 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); // It's output is incorrect because it returns "00:00 am" instead of "12:00 am" + +const currentOutput5 = formatAs12HourClock("15:30"); +const targetOutput5 = "3:30 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); // It's output is incorrect because it returns "15:00 pm" instead of "3:30 pm" + +const currentOutput6 = formatAs12HourClock("11:45"); +const targetOutput6 = "11:45 am"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); // It's output is correct + +//Fixing the function: +function formatAs12HourClockFixed(time) { + const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + let period = "am"; + + if (hours === 0) { + return `12:${minutes} am`; + } + if (hours === 12) { + period = "pm"; + } + if (hours > 12) { + return `${hours - 12}:${minutes} pm`; + } + return `${hours}:${minutes} ${period}`; +} + +// Re-testing : +const currentOutput1 = formatAs12HourClockFixed("08:00"); +const targetOutput1 = "8:00 am"; +console.assert( + currentOutput1 === targetOutput1, + `current output: ${currentOutput1}, target output: ${targetOutput1}` +); // It's output is correct + +const currentOutput2 = formatAs12HourClockFixed("23:00"); +const targetOutput2 = "11:00 pm"; +console.assert( + currentOutput2 === targetOutput2, + `current output: ${currentOutput2}, target output: ${targetOutput2}` +); // It's output is correct + +const currentOutput3 = formatAs12HourClockFixed("12:00"); +const targetOutput3 = "12:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); // It's output is correct + +const currentOutput4 = formatAs12HourClockFixed("00:00"); +const targetOutput4 = "12:00 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); // It's output is correct + +const currentOutput5 = formatAs12HourClockFixed("15:30"); +const targetOutput5 = "3:30 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); // It's output is correct + +const currentOutput6 = formatAs12HourClockFixed("11:45"); +const targetOutput6 = "11:45 am"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); // It's output is correct From 472c56d52aa6872f8267d4533216b7178b64ad64 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Mon, 13 Oct 2025 19:16:52 +0100 Subject: [PATCH 12/19] fix the code as requested --- Sprint-2/1-key-errors/0.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 5453fa61e..c5ba1e922 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,20 +1,23 @@ // Predict and explain first... -// =============> write your prediction here : There will be an error. +// =============> write your prediction here : +// The function capitalise is expected to take a string input and return the string with the first letter capitalised. When called with the input "hello", it should return "Hello". However, since the function body is empty, it will likely result in an error or return undefined. // 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; +// function capitalise(str) { +//let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +//} -// =============> write your explanation here : Because str has been used(declared) twice. As an input(parameter) to the function(capitalise) and with let. We should used a different variable. +// =============> write your explanation here : +// The function capitalise is called with the string "hello" as an argument. The function takes the first character of the string, converts it to uppercase, and concatenates it with the rest of the string starting from the second character. The result is "Hello", which is then returned and logged to the console. There is no error in this code; it works as intended. // =============> write your new code here : -/* + function capitalise(str) { - let cap = `${str[0].toUpperCase()}${str.slice(1)}`; - return cap; + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; } -console.log(capitalise("str")); -*/ \ No newline at end of file + +console.log(capitalise("hello")); \ No newline at end of file From 24c915a6ae67f21dea2db4d4bd16f48a22a1e531 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Mon, 13 Oct 2025 19:20:49 +0100 Subject: [PATCH 13/19] get the code more simplified --- Sprint-2/1-key-errors/1.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index 44d38f62d..f3b3e7861 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -29,8 +29,8 @@ console.log(decimalNumber); const decimalNumber = 0.5; function convertToPercentage(decimalNumber) { - const percentage = `${decimalNumber * 100}%`; - return percentage; + return `${decimalNumber * 100}%`; } + console.log(convertToPercentage(decimalNumber)); From 812128cc073aa700fd1bb36a7400a31251b0bb59 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Mon, 13 Oct 2025 19:27:34 +0100 Subject: [PATCH 14/19] write addition explanation for the prediction part --- Sprint-2/2-mandatory-debug/0.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index 9a03a3f60..ef6e18966 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,12 +1,14 @@ // Predict and explain first... -// =============> write your prediction here : There is no return statement for the parameters. +// =============> write your prediction here : This function doesn’t have a return statement. +// That means: even though it takes parameters, it won’t give any value back when called and the result will be undefined. -function multiply(a, b) { - console.log(a * b); -} -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +//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 multiply does not return any value, it only logs the result to the console. It returns undefined. To fix this, we need to add a return statement in the multiply function. */ From 38656001002507a80e121282440bd163167f0451 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Mon, 13 Oct 2025 19:33:37 +0100 Subject: [PATCH 15/19] rewrite bmi code --- Sprint-2/3-mandatory-implement/1-bmi.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 454a8a016..d70095f0f 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,7 +16,7 @@ function calculateBMI(weight, height) { const bmi = weight / (height * height); - return bmi.toFixed(1); - // return the BMI of someone based off their weight and height + return Math.round(bmi * 10) / 10; } + console.log(calculateBMI(75, 1.74)); From bfafdeffa8389b70c482040f4dab1f8f603e4f33 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Mon, 13 Oct 2025 19:37:34 +0100 Subject: [PATCH 16/19] recall the right function --- Sprint-2/3-mandatory-implement/2-cases.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 1b2d9c70c..5cbb3715d 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -19,4 +19,4 @@ function toUpperSnake(name) { const upperCase = name.toUpperCase(); // Convert the string to uppercase return upperCase.replaceAll(" ", "_"); // Replace all spaces with underscores } -console.log(snakeItUp("alaa tagi is my name")); +console.log(toUpperSnake("alaa tagi is my name")); From 0671c48bb81f5cb49599447411147aa62c57a2f3 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Mon, 13 Oct 2025 19:43:36 +0100 Subject: [PATCH 17/19] rewrite the correct answer for Q(e) --- Sprint-2/4-mandatory-interpret/time-format.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 6286c36a8..038d4aecc 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -31,4 +31,5 @@ function formatTimeDisplay(seconds) { // =============> write your answer here : 1, because remainingSeconds is 1 on python tutor. // 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 : "00", because num is converted to string and padded to 2 characters with "0". That's mean "1" becomes "01". +// =============> write your answer here : +// "01", because the number 1 is converted to a string ("1").Then padStart(2, "0") makes sure it has 2 characters by adding "0" at the start, so "1" becomes "01". From 3b782df2bd211d71a7a032f8cdafd2dc8b6ab417 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Mon, 13 Oct 2025 19:59:59 +0100 Subject: [PATCH 18/19] adding another tests for the code --- Sprint-2/5-stretch-extend/format-time.js | 151 ++++++++++++++--------- 1 file changed, 90 insertions(+), 61 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 2bea7a763..00aba3fce 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,56 +3,54 @@ // 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. // The main function: -function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; - } - return `${time} am`; -} +//function formatAs12HourClock(time) { +// const hours = Number(time.slice(0, 2)); +//if (hours > 12) { +// return `${hours - 12}:00 pm`; +//} +//return `${time} am`; +//} // Tests: -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); // It's output is correct +// const currentOutput = formatAs12HourClock("08:00"); +// const targetOutput = "08:00 am"; +// 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}` -); // It's output is correct +// const currentOutput2 = formatAs12HourClock("23:00"); +// const targetOutput2 = "11:00 pm"; +// console.assert( +// currentOutput2 === targetOutput2, +// `current output: ${currentOutput2}, target output: ${targetOutput2}` +// ); -const currentOutput3 = formatAs12HourClock("12:00"); -const targetOutput3 = "12:00 pm"; -console.assert( - currentOutput3 === targetOutput3, - `current output: ${currentOutput3}, target output: ${targetOutput3}` -); // It's output is incorrect because it returns "12:00 am" instead of "12:00 pm" +// const currentOutput3 = formatAs12HourClock("12:00"); +// const targetOutput3 = "12:00 pm"; +// console.assert( +// currentOutput3 === targetOutput3, +// `current output: ${currentOutput3}, target output: ${targetOutput3}` +// ); +// const currentOutput4 = formatAs12HourClock("00:00"); +// const targetOutput4 = "12:00 am"; +// console.assert( +// currentOutput4 === targetOutput4, +// `current output: ${currentOutput4}, target output: ${targetOutput4}` +// ); +// const currentOutput5 = formatAs12HourClock("15:30"); +// const targetOutput5 = "3:30 pm"; +// console.assert( +// currentOutput5 === targetOutput5, +// `current output: ${currentOutput5}, target output: ${targetOutput5}` +// ); -const currentOutput4 = formatAs12HourClock("00:00"); -const targetOutput4 = "12:00 am"; -console.assert( - currentOutput4 === targetOutput4, - `current output: ${currentOutput4}, target output: ${targetOutput4}` -); // It's output is incorrect because it returns "00:00 am" instead of "12:00 am" - -const currentOutput5 = formatAs12HourClock("15:30"); -const targetOutput5 = "3:30 pm"; -console.assert( - currentOutput5 === targetOutput5, - `current output: ${currentOutput5}, target output: ${targetOutput5}` -); // It's output is incorrect because it returns "15:00 pm" instead of "3:30 pm" - -const currentOutput6 = formatAs12HourClock("11:45"); -const targetOutput6 = "11:45 am"; -console.assert( - currentOutput6 === targetOutput6, - `current output: ${currentOutput6}, target output: ${targetOutput6}` -); // It's output is correct +// const currentOutput6 = formatAs12HourClock("11:45"); +// const targetOutput6 = "11:45 am"; +// console.assert( +// currentOutput6 === targetOutput6, +// `current output: ${currentOutput6}, target output: ${targetOutput6}` +// ); //Fixing the function: function formatAs12HourClockFixed(time) { @@ -78,39 +76,70 @@ const targetOutput1 = "8:00 am"; console.assert( currentOutput1 === targetOutput1, `current output: ${currentOutput1}, target output: ${targetOutput1}` -); // It's output is correct +); -const currentOutput2 = formatAs12HourClockFixed("23:00"); -const targetOutput2 = "11:00 pm"; +const currentOutput2 = formatAs12HourClockFixed("7:45"); +const targetOutput2 = "7:45 am"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` -); // It's output is correct +); -const currentOutput3 = formatAs12HourClockFixed("12:00"); -const targetOutput3 = "12:00 pm"; +const currentOutput3 = formatAs12HourClockFixed("23:00"); +const targetOutput3 = "11:00 pm"; console.assert( currentOutput3 === targetOutput3, `current output: ${currentOutput3}, target output: ${targetOutput3}` -); // It's output is correct +); -const currentOutput4 = formatAs12HourClockFixed("00:00"); -const targetOutput4 = "12:00 am"; +const currentOutput4 = formatAs12HourClockFixed("12:00"); +const targetOutput4 = "12:00 pm"; console.assert( currentOutput4 === targetOutput4, `current output: ${currentOutput4}, target output: ${targetOutput4}` -); // It's output is correct +); -const currentOutput5 = formatAs12HourClockFixed("15:30"); -const targetOutput5 = "3:30 pm"; +const currentOutput5 = formatAs12HourClockFixed("00:00"); +const targetOutput5 = "12:00 am"; console.assert( currentOutput5 === targetOutput5, `current output: ${currentOutput5}, target output: ${targetOutput5}` -); // It's output is correct +); -const currentOutput6 = formatAs12HourClockFixed("11:45"); -const targetOutput6 = "11:45 am"; +const currentOutput6 = formatAs12HourClockFixed("15:30"); +const targetOutput6 = "3:30 pm"; console.assert( currentOutput6 === targetOutput6, `current output: ${currentOutput6}, target output: ${targetOutput6}` -); // It's output is correct +); + +const currentOutput7 = formatAs12HourClockFixed("11:45"); +const targetOutput7 = "11:45 am"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}` +); + +const currentOutput8 = formatAs12HourClockFixed("25:30"); +const targetOutput8 = "Invalid time"; +console.assert( + currentOutput8 === targetOutput8, + `current output: ${currentOutput8}, target output: ${targetOutput8}` +); + +const currentOutput9 = formatAs12HourClockFixed("1724"); +const targetOutput9 = "Invalid time"; +console.assert( + currentOutput9 === targetOutput9, + `current output: ${currentOutput9}, target output: ${targetOutput9}` +); +console.log (formatAs12HourClockFixed("08:00")); + +console.log (formatAs12HourClockFixed("7:45")); +console.log (formatAs12HourClockFixed("23:00")); +console.log (formatAs12HourClockFixed("12:00")); +console.log (formatAs12HourClockFixed("00:00")); +console.log (formatAs12HourClockFixed("15:30")); +console.log (formatAs12HourClockFixed("11:45")); +console.log (formatAs12HourClockFixed("25:30")); +console.log (formatAs12HourClockFixed("1724688")); \ No newline at end of file From d0593998b85d3481ec2fd69bdcd65fe1b8317449 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Fri, 24 Oct 2025 18:51:11 +0100 Subject: [PATCH 19/19] Edit the code --- Sprint-2/5-stretch-extend/format-time.js | 132 +++++++++-------------- 1 file changed, 51 insertions(+), 81 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 00aba3fce..5f8b9992c 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -1,145 +1,115 @@ -// This is the latest solution to the problem from the prep. -// 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. - -// The main function: -//function formatAs12HourClock(time) { -// const hours = Number(time.slice(0, 2)); -//if (hours > 12) { -// return `${hours - 12}:00 pm`; -//} -//return `${time} am`; -//} - -// Tests: -// const currentOutput = formatAs12HourClock("08:00"); -// const targetOutput = "08:00 am"; -// 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 currentOutput3 = formatAs12HourClock("12:00"); -// const targetOutput3 = "12:00 pm"; -// console.assert( -// currentOutput3 === targetOutput3, -// `current output: ${currentOutput3}, target output: ${targetOutput3}` -// ); -// const currentOutput4 = formatAs12HourClock("00:00"); -// const targetOutput4 = "12:00 am"; -// console.assert( -// currentOutput4 === targetOutput4, -// `current output: ${currentOutput4}, target output: ${targetOutput4}` -// ); -// const currentOutput5 = formatAs12HourClock("15:30"); -// const targetOutput5 = "3:30 pm"; -// console.assert( -// currentOutput5 === targetOutput5, -// `current output: ${currentOutput5}, target output: ${targetOutput5}` -// ); - -// const currentOutput6 = formatAs12HourClock("11:45"); -// const targetOutput6 = "11:45 am"; -// console.assert( -// currentOutput6 === targetOutput6, -// `current output: ${currentOutput6}, target output: ${targetOutput6}` -// ); - -//Fixing the function: -function formatAs12HourClockFixed(time) { - const hours = Number(time.slice(0, 2)); - const minutes = time.slice(3, 5); +// Fixed function +function formatAs12HourClock(time) { + // Validate input format + if (typeof time !== "string" || !time.includes(":")) { + return "Invalid time"; + } + + const [hourStr, minuteStr] = time.split(":"); + const hours = Number(hourStr); + const minutes = Number(minuteStr); let period = "am"; + // Validate range + if ( + isNaN(hours) || isNaN(minutes) || + hours < 0 || hours > 23 || + minutes < 0 || minutes > 59 + ) { + return "Invalid time"; + } + + // Midnight if (hours === 0) { - return `12:${minutes} am`; + return `12:${minuteStr.padStart(2, "0")} am`; } + + // Noon if (hours === 12) { period = "pm"; + return `12:${minuteStr.padStart(2, "0")} ${period}`; } + + // PM hours if (hours > 12) { - return `${hours - 12}:${minutes} pm`; + return `${hours - 12}:${minuteStr.padStart(2, "0")} pm`; } - return `${hours}:${minutes} ${period}`; + + // AM hours + return `${hours}:${minuteStr.padStart(2, "0")} ${period}`; } -// Re-testing : -const currentOutput1 = formatAs12HourClockFixed("08:00"); +// ✅ Re-tests +const currentOutput1 = formatAs12HourClock("08:00"); const targetOutput1 = "8:00 am"; console.assert( currentOutput1 === targetOutput1, `current output: ${currentOutput1}, target output: ${targetOutput1}` ); -const currentOutput2 = formatAs12HourClockFixed("7:45"); +const currentOutput2 = formatAs12HourClock("7:45"); const targetOutput2 = "7:45 am"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); -const currentOutput3 = formatAs12HourClockFixed("23:00"); +const currentOutput3 = formatAs12HourClock("23:00"); const targetOutput3 = "11:00 pm"; console.assert( currentOutput3 === targetOutput3, `current output: ${currentOutput3}, target output: ${targetOutput3}` ); -const currentOutput4 = formatAs12HourClockFixed("12:00"); +const currentOutput4 = formatAs12HourClock("12:00"); const targetOutput4 = "12:00 pm"; console.assert( currentOutput4 === targetOutput4, `current output: ${currentOutput4}, target output: ${targetOutput4}` ); -const currentOutput5 = formatAs12HourClockFixed("00:00"); +const currentOutput5 = formatAs12HourClock("00:00"); const targetOutput5 = "12:00 am"; console.assert( currentOutput5 === targetOutput5, `current output: ${currentOutput5}, target output: ${targetOutput5}` ); -const currentOutput6 = formatAs12HourClockFixed("15:30"); +const currentOutput6 = formatAs12HourClock("15:30"); const targetOutput6 = "3:30 pm"; console.assert( currentOutput6 === targetOutput6, `current output: ${currentOutput6}, target output: ${targetOutput6}` ); -const currentOutput7 = formatAs12HourClockFixed("11:45"); +const currentOutput7 = formatAs12HourClock("11:45"); const targetOutput7 = "11:45 am"; console.assert( currentOutput7 === targetOutput7, `current output: ${currentOutput7}, target output: ${targetOutput7}` ); -const currentOutput8 = formatAs12HourClockFixed("25:30"); +const currentOutput8 = formatAs12HourClock("25:30"); const targetOutput8 = "Invalid time"; console.assert( currentOutput8 === targetOutput8, `current output: ${currentOutput8}, target output: ${targetOutput8}` ); -const currentOutput9 = formatAs12HourClockFixed("1724"); +const currentOutput9 = formatAs12HourClock("1724688"); const targetOutput9 = "Invalid time"; console.assert( currentOutput9 === targetOutput9, `current output: ${currentOutput9}, target output: ${targetOutput9}` ); -console.log (formatAs12HourClockFixed("08:00")); - -console.log (formatAs12HourClockFixed("7:45")); -console.log (formatAs12HourClockFixed("23:00")); -console.log (formatAs12HourClockFixed("12:00")); -console.log (formatAs12HourClockFixed("00:00")); -console.log (formatAs12HourClockFixed("15:30")); -console.log (formatAs12HourClockFixed("11:45")); -console.log (formatAs12HourClockFixed("25:30")); -console.log (formatAs12HourClockFixed("1724688")); \ No newline at end of file + +// ✅ Display output +console.log(formatAs12HourClock("08:00")); +console.log(formatAs12HourClock("7:45")); +console.log(formatAs12HourClock("23:00")); +console.log(formatAs12HourClock("12:00")); +console.log(formatAs12HourClock("00:00")); +console.log(formatAs12HourClock("15:30")); +console.log(formatAs12HourClock("11:45")); +console.log(formatAs12HourClock("25:30")); +console.log(formatAs12HourClock("1724688"));