From 07744d9b6dfd53662d75fe93f10f09afea7d02ce Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 09:54:51 +0000 Subject: [PATCH 01/11] strError --- Sprint-2/1-key-errors/0.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..ecc406045 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -4,10 +4,19 @@ // 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 +// An error occured => SyntaxError: Identifier 'str' has already been declared +//because in javascript we cant declare a variable more than once within scope . + // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; + } + + console.log(capitalise("cool")) \ No newline at end of file From 02b303147112374cab9a08b7ea7ad1b71cbfa7ca Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 09:56:36 +0000 Subject: [PATCH 02/11] decimalNumber --- Sprint-2/1-key-errors/1.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..075e38cb3 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -4,7 +4,7 @@ // =============> write your prediction here // Try playing computer with the example to work out what is going on - +/* function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -12,9 +12,22 @@ function convertToPercentage(decimalNumber) { return percentage; } -console.log(decimalNumber); +console.log(decimalNumber); */ // =============> write your explanation here +//The function convertToPercentage(decimalNumber) accepts a parameter decimalNumber +//Inside the function, ywe have decimalNumber redeclared using const decimalNumber = 0.5;, which creates a conflict +// Finally, correct the code to fix the problem +//we should remove the redeclaration of decimalNumber inside the function. Instead, use the parameter directly.and then +//pass a decimal number as input => 0.5 in the function call // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +const result = convertToPercentage(0.5); +console.log(result); // Output => "50%" \ No newline at end of file From d1ff58c903d358875ee10b1bfc7d8a73df04dbb6 Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 09:59:19 +0000 Subject: [PATCH 03/11] decimalNumber --- Sprint-2/1-key-errors/0.js | 3 ++- Sprint-2/1-key-errors/1.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index ecc406045..2dc3fb7ff 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,6 +1,7 @@ // Predict and explain first... // =============> write your prediction here - +//The variable str has been declared twice the first one as the function +// and the second time inside the function => let str = `${str[0].toUpperCase()}${str.slice(1)}`; // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index 075e38cb3..1fdd4293b 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,7 +2,8 @@ // Why will an error occur when this program runs? // =============> write your prediction here - +//// The variable decimalNmber has been declared twice once as a parameter for convertToPercentage +// function and the second time inside the function // Try playing computer with the example to work out what is going on /* function convertToPercentage(decimalNumber) { From 4e01724ea2e4b72a057012e90b2151f56c6ff69a Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 10:02:38 +0000 Subject: [PATCH 04/11] squareError --- Sprint-2/1-key-errors/2.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..fb220e8d2 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,25 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// The number three was passed straight to the function witch is why it's throwing an error -function square(3) { +/* function square(3) { return num * num; } - + */ // =============> write the error message here +// Error message => SyntaxError: Unexpected number // =============> explain this error message here +//The function was expecting a parameter and got a number instead and a number +// can not be a valid parameter name +//We pass 3 as an argument when calling square(3), and the function returns 9 (since 3 squared is 9). // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(3)) From a6f8889e080a32e2dfcf4d6d4d6113357f9748e5 Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 10:05:58 +0000 Subject: [PATCH 05/11] multiplyFunction --- Sprint-2/2-mandatory-debug/0.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..8b6369549 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 +//In this function console.log() is used instead of return -function multiply(a, b) { +/* 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 - +//By using console.log() this multiply function dose not return anything and it just printing the output +//of => a * b to the console + undefined => since console.log does not return // Finally, correct the code to fix the problem +// 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)}`); \ No newline at end of file From 09786705a6b9a978b3bbeae814ff7a73d91f50e3 Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 10:08:53 +0000 Subject: [PATCH 06/11] debuggingSum --- Sprint-2/2-mandatory-debug/1.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..4dae5316b 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,24 @@ // Predict and explain first... // =============> write your prediction here +//Here we have return; and => a + b ; in seprate lines closed with semicolon. -function sum(a, b) { +/* function sum(a, b) { return; a + b; } -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); */ // =============> write your explanation here +//AS we know that return end and stops the execution of the function witch mean that => a + b +//will not be executed and line 8 is useless since the function stops at line 7. + // 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)}`); \ No newline at end of file From 10837b1a5e5b5065a624cbca08a118dc6511e9a9 Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 10:13:06 +0000 Subject: [PATCH 07/11] getLastDigit --- Sprint-2/2-mandatory-debug/2.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..50cdb500b 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,9 +1,12 @@ // Predict and explain first... +// The constant variable num is set to = 103 ,and num should be passed as a parameter to getLastDigit function // Predict the output of the following code: // =============> Write your prediction here +/* The last digit will be the same for all values that will be passed to it +since it coming from the constant.*/ -const num = 103; +/* const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -12,13 +15,33 @@ 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 +//The output is the same because it coming from the execution of => num.toString().slice(-1); +//=> 3 over and over again + // 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 +// To correct the code we needed to get rid of => const num = 103 +//and pass num as a parameter to the function and now it giving the proper output +/* The last digit of 42 is 2 +The last digit of 105 is 5 +The last digit of 806 is 6 */ \ No newline at end of file From 07055e2348821d1062897c3e756b1516d0a57cca Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 10:14:58 +0000 Subject: [PATCH 08/11] calculateBmi --- Sprint-2/3-mandatory-implement/1-bmi.js | 8 +++++++- 1 file changed, 7 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..dc6bd3636 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,11 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + let squared = height * height; + let bmi = weight/squared ; + let bmiRounded = Math.round(bmi * 10) / 10; + return bmiRounded ; // return the BMI of someone based off their weight and height -} \ No newline at end of file +} + +console.log(calculateBMI(70,1.73)) \ No newline at end of file From e0a8f9c03f9909096b6ca824c3dfd887be618db5 Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 10:16:05 +0000 Subject: [PATCH 09/11] upperSnakeCase --- Sprint-2/3-mandatory-implement/2-cases.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..922b1af73 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,10 @@ // You will need to come up with an appropriate name for the function // Use the MDN string documentation to help you find a solution // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase + +function toUpperSnakeCase(str) { + return str.replace(/\s+/g, '_').toUpperCase(); + } + + console.log(toUpperSnakeCase("hello there")); + console.log(toUpperSnakeCase("lord of the rings")); \ No newline at end of file From 130149aee272ed7de41373cd1ee2759b3e67251a Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 10:16:54 +0000 Subject: [PATCH 10/11] toPounds --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 22 +++++++++++++++++++ 1 file changed, 22 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..8ea7409ec 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,25 @@ // You will need to declare a function called toPounds with an appropriately named parameter. // You should call this function a number of times to check it works for different inputs + +function toPounds(penceString){ + + const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 + ); + + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + return`£${pounds}.${pence}`; + + } + console.log(toPounds("399p")) \ No newline at end of file From 1c65aaba8871b09c7c6e816ea92a842f6418a00b Mon Sep 17 00:00:00 2001 From: mohammed-alzaki Date: Fri, 14 Mar 2025 10:19:41 +0000 Subject: [PATCH 11/11] time-format --- Sprint-2/4-mandatory-interpret/time-format.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..42a572c00 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,6 +10,7 @@ function formatTimeDisplay(seconds) { 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 @@ -18,17 +19,26 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +/* The pad function is called three times inside the formatTimeDisplay function: +Once for totalHours => to pad the hour value. +Once for remainingMinutes => to pad the minutes value. +Once for remainingSeconds =>to pad the seconds value. */ // 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 last time pad is called is for remainingSeconds, which is calculated as remainingSeconds = 61 % 60 = 1 when formatTimeDisplay(61) is called. // 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 +//The last call to pad is for remainingSeconds, which is 1. The padStart(2, "0") will pad this number to make it two digits. +//And return value =>"01" \ No newline at end of file