From d191914e1c65b35f9272719ef573995bd49c6272 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Tue, 15 Jul 2025 15:50:38 +0100 Subject: [PATCH 01/11] key errors fixed --- Sprint-2/1-key-errors/0.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..5eae58a1e 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// The code will throw an error because the variable 'str' is being redeclared with 'let' inside the function. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +// the error shows that 'str' is already declared in the outer scope, and we cannot redeclare it with 'let' in the inner scope. -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 +// The code will throw a SyntaxError because the variable 'str' is being redeclared with 'let' inside the function, which is not allowed in JavaScript. +// The function capitalise is trying to declare 'str' again with 'let', which is already declared in the outer scope. This leads to a SyntaxError because 'let' does not allow redeclaration of the same variable in the same scope. // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} +console.log(capitalise("hello")); \ No newline at end of file From aed1bc112b27065f5754cba631681701fa1f2d28 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 16 Jul 2025 15:08:04 +0100 Subject: [PATCH 02/11] key-errors 1 --- Sprint-2/1-key-errors/1.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..8297d2bbb 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,30 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// The code will shows an error because the variable 'decimalNumber' is being redeclared with 'const' inside the function, which is not allowed in JavaScript. // 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 +// there is a SyntaxError because the variable 'decimalNumber' is being redeclared with 'const' inside the function. +// The function convertToPercentage is trying to declare 'decimalNumber' again with 'const', which is already declared in the outer scope. This leads to a SyntaxError because 'const' does not allow redeclaration of the same variable in the same scope. + // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + decimalNumber = 0.5; // Remove 'const' to avoid redeclaration + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} +console.log(convertToPercentage(0.5)); \ No newline at end of file From 4617f0c63a9c679a4bdeef3817b814217acb772b Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 16 Jul 2025 15:09:05 +0100 Subject: [PATCH 03/11] squaring error --- Sprint-2/1-key-errors/2.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..29e156ad0 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 code will show an errror because no varible has been declared + +// function square(3) { +// return num * num; +// } +// console.log(square(3)); -function square(3) { - return num * num; -} // =============> write the error message here +// Uncaught SyntaxError: Unexpected number // =============> explain this error message here +// The error occurs because the function parameter is incorrectly defined as a number (3) instead of a variable name. In JavaScript, function parameters must be variable names, not literal values. This leads to a SyntaxError because the JavaScript engine expects a valid identifier for the parameter. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(3)); From f95aff3fc6b65f294f444483b8b19c10b6901cdc Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 16 Jul 2025 15:09:35 +0100 Subject: [PATCH 04/11] multiplication --- Sprint-2/2-mandatory-debug/0.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..9bf7ceb43 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 +// The code will show an error because the function does not have a return value. -function multiply(a, b) { - console.log(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 - +// The code will throw an error because the function `multiply` does not return a value, but the console.log statement expects a value to be printed. +// The function `multiply` is defined to log the result of multiplying two numbers, but it does not return anything, leading to an undefined value being logged. // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + return a * b; // Change console.log to return the result +} +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); From 1cf00691c20a8a69a02f526ee2b3bb75f70e8db2 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 16 Jul 2025 15:10:03 +0100 Subject: [PATCH 05/11] addition fixed error --- Sprint-2/2-mandatory-debug/1.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..d7c926b6d 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,20 @@ // Predict and explain first... // =============> write your prediction here +// the code will throw an error because the operation a + b is outside the return syntax. -function sum(a, b) { - return; - 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 +// The code will throw an error because the return statement is not followed by any value. The function `sum` is expected to return the result of adding `a` and `b`, but since the return statement is immediately followed by a semicolon, it effectively returns `undefined`. This leads to an incorrect output when trying to log the result of the sum operation. + // Finally, correct the code to fix the problem // =============> write your new code here +function sum(a, b) { + return a + b; // Corrected to return the sum of a and b +} +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); \ No newline at end of file From 6c171836438e307a0c8bff9e8da876bb9c184948 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 16 Jul 2025 15:10:49 +0100 Subject: [PATCH 06/11] last digit error --- Sprint-2/2-mandatory-debug/2.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..8efdc46ff 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,35 @@ // Predict the output of the following code: // =============> Write your prediction here +// the code will show an error because we didn't set an an identifier in the function's parameter. -const num = 103; +// const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// function getLastDigit() { +// return num.toString().slice(-1); +// } -console.log(`The last digit of 42 is ${getLastDigit(42)}`); -console.log(`The last digit of 105 is ${getLastDigit(105)}`); -console.log(`The last digit of 806 is ${getLastDigit(806)}`); +// console.log(`The last digit of 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 for all three calls because the function `getLastDigit` uses a hardcoded variable `num` instead of the parameter passed to it. Therefore, it always returns the last digit of `103`, which is `3`, regardless of the input provided in the function call. // 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 +// The function `getLastDigit` was not using the parameter `num` correctly. \ No newline at end of file From a9f28b8a7f25cddfaf99861eefd0d7233757f1b8 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 16 Jul 2025 15:12:02 +0100 Subject: [PATCH 07/11] BMI program --- Sprint-2/3-mandatory-implement/1-bmi.js | 8 ++++++-- 1 file changed, 6 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..974d6f2a9 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,9 @@ // 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);// Calculate BMI by dividing weight by height squared + + // Return the BMI rounded to 1 decimal place + return Math.round(bmi * 10) / 10; +} +console.log(calculateBMI(70, 1.73)); \ No newline at end of file From 66a4e4a208cc97ad3138427275f5218024fb8e74 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 16 Jul 2025 15:12:24 +0100 Subject: [PATCH 08/11] 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..cec53a9d1 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 toUpperSnakeCase(input) // Function to convert a string to UPPER_SNAKE_CASE + { + return input.toUpperCase().replace(/\s+/g, "_");// Convert the string to uppercase and replace spaces with underscores + } +console.log(toUpperSnakeCase("hello there")); // Outputs: "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // Outputs: "LORD_OF_THE_RINGS" From 5a90beffe214cd8e2afbd82e9420431fada195c4 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 16 Jul 2025 15:12:52 +0100 Subject: [PATCH 09/11] pound converter --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 21 +++++++++++++++++++ 1 file changed, 21 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..261c56ac4 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,24 @@ // 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")); +console.log(toPounds("100p")); +console.log(toPounds("430p")); \ No newline at end of file From aec46fd19a957a32cd438931fe17a4f15cc47f91 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Wed, 16 Jul 2025 15:13:19 +0100 Subject: [PATCH 10/11] time-format --- Sprint-2/4-mandatory-interpret/time-format.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..169a52d81 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,23 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// 3 times, once for each of hours, minutes, and seconds - pad(totalHours), pad(remainingMinutes), and pad(remainingSeconds). // 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 value assigned to num when pad is called for the first time is 0. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value of pad when called for the first time is "00", because it converts the number 0 to a string and pads it with leading zeros to ensure it has a length of at least 2 characters. // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer // =============> write your answer here +// The value assigned to num when pad is called for the last time in this program is 1. This is because the last call to pad is for remainingSeconds, which is calculated as 61 % 60, resulting in 1. The function then pads this value to ensure it has a length of at least 2 characters, resulting in "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 +// The return value assigned to num when pad is called for the last time in this program is "01". This is because the function converts the number 1 to a string and pads it with leading zeros to ensure it has a length of at least 2 characters, resulting in "01". +// This is the final output for the seconds part of the time format, which is displayed as part of the formatted time string. From 940d3645ead4afe24c2590602367184e63ab0a05 Mon Sep 17 00:00:00 2001 From: Nahom Mesfin Date: Fri, 8 Aug 2025 17:05:20 +0100 Subject: [PATCH 11/11] removed a fixed value --- Sprint-2/1-key-errors/1.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index 8297d2bbb..0d7226ef8 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -22,10 +22,9 @@ // Finally, correct the code to fix the problem // =============> write your new code here -function convertToPercentage(decimalNumber) { - decimalNumber = 0.5; // Remove 'const' to avoid redeclaration +function convertToPercentage(decimalNumber) { // Remove 'const' to avoid redeclaration const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(convertToPercentage(0.5)); \ No newline at end of file +console.log(convertToPercentage(0.7)); \ No newline at end of file