From e3b89c32cc9ea4eed79af610701cf258b88be29f Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 25 Sep 2025 10:22:22 +0100 Subject: [PATCH 01/12] questions answered and code rewritten --- Sprint-2/1-key-errors/0.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..76b8a3d9a 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,29 @@ // Predict and explain first... // =============> write your prediction here +// the code will throw an error because the variable str is being declared twice within the same scope // 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; } -// =============> write your explanation here -// =============> write your new code here +*/ + +// =============> write your explanation +/* the code throws a SyntaxError because the variable 'str' is declared twice in the same scope using 'let'. In JavaScript, + you cannot declare a variable with the same name more than once in the same scope. The first declaration of 'str' is as a + function parameter, and the second declaration is within the function body. To fix this, you can either rename the second + 'str' variable or remove the 'let' keyword from the second declaration to reassign the value to the existing parameter variable.*/ + + // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +console.log(capitalise("hello")); // Output: "Hello" From 3d852fa16e996b005bfb09f65783cc094d90169b Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 25 Sep 2025 10:31:05 +0100 Subject: [PATCH 02/12] question answered and code rewritten --- Sprint-2/1-key-errors/1.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..f89eb25e3 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,9 +2,12 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// the code will throw an error because the variable str is being declared twice within the same scope // Try playing computer with the example to work out what is going on +/* + function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -14,7 +17,23 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); +*/ + // =============> write your explanation here +/* The error occurs because the variable 'decimalNumber' is declared twice within the same function scope using 'const'. +This is not allowed in JavaScript, as each variable must have a unique name within its scope. When the function is called, +the JavaScript engine encounters the second declaration of 'decimalNumber' and throws a SyntaxError indicating that the +identifier has already been declared.*/ // Finally, correct the code to fix the problem // =============> write your new code here + +let decimalNumber = 0.6; + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(decimalNumber)); From ce0868b2d06557801e70631c5c4b586db63ebf61 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 25 Sep 2025 10:36:13 +0100 Subject: [PATCH 03/12] questions answered and code fixed --- Sprint-2/1-key-errors/2.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..0cc11d7b0 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,37 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +/* I predict that the code will throw a SyntaxError because the parameter '3' is not a valid identifier. +Function parameters must be valid variable names.*/ + +/* function square(3) { return num * num; } +*/ + // =============> write the error message here +/* Process exited with code 1 +Uncaught SyntaxError /home/ads/Desktop/CYF-SEP/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/2.js:10 +function square(3) { + ^ + +SyntaxError: Unexpected number */ // =============> explain this error message here +/* The error message indicates a SyntaxError due to an "Unexpected number". This occurs because the function +parameter '3' is not a valid identifier. In JavaScript, function parameters must be valid variable names, and +using a number as a parameter name is not allowed. The JavaScript engine expects a valid identifier after the +opening parenthesis of the function declaration, but it encounters a number instead, leading to the syntax error.*/ // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(4)); // Output: 16 From f775ea1f400b983382daacb1c30fd307832a11b3 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 25 Sep 2025 10:38:56 +0100 Subject: [PATCH 04/12] question answered and error fixed. --- Sprint-2/2-mandatory-debug/0.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..05b512db9 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,26 @@ // Predict and explain first... // =============> write your prediction here +/* The function multiply does not return any value, it only logs the product of a and b to the console. +Therefore, when we try to log the result of multiply(10, 32), it will log 'undefined' because the +function does not return anything.*/ +/* 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 have a return statement, so it returns undefined by default. // 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 7a754782a565394c160cd8cfa52b2a3e316f8c0d Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 25 Sep 2025 10:40:52 +0100 Subject: [PATCH 05/12] error fixed and questions answered --- Sprint-2/2-mandatory-debug/1.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..d8a25d274 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,27 @@ // Predict and explain first... // =============> write your prediction here +// An error will occur because the function 'sum' has a return statement that is not returning any value. +/* function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +*/ // =============> write your explanation here +/* The error occurs because the 'sum' function has a return statement that does not return any value. In JavaScript, +when a function reaches a return statement without a value, it returns 'undefined' by default. Therefore, when we +call 'sum(10, 32)', it returns 'undefined', and the output will be "The sum of 10 and 32 is undefined". To fix this, +we need to ensure that the function returns the result of 'a + b'.*/ + // Finally, correct the code to fix the problem // =============> write your new code here + +function sum(a, b) { + return a + b; +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); \ No newline at end of file From 15b01b6bc1400de43e9a51ca043e486d6b9134e0 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 25 Sep 2025 11:15:05 +0100 Subject: [PATCH 06/12] code fixed and explained why the previous code was not working. --- Sprint-2/2-mandatory-debug/2.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..ab75db690 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,10 @@ // Predict the output of the following code: // =============> Write your prediction here +/* The output will be "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3" because +the function getLastDigit always returns the last digit of the constant number 'num', which is set to 103.*/ +/* const num = 103; function getLastDigit() { @@ -12,13 +15,34 @@ function getLastDigit() { console.log(`The last digit of 42 is ${getLastDigit(42)}`); console.log(`The last digit of 105 is ${getLastDigit(105)}`); console.log(`The last digit of 806 is ${getLastDigit(806)}`); +*/ // Now run the code and compare the output to your prediction // =============> write the output here +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 + // Explain why the output is the way it is // =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here +/* The output is the way it is because the function getLastDigit does not use its parameter. Instead, it always +returns the last digit of the constant variable 'num', which is set to 103. Therefore, regardless of the input +value provided when calling getLastDigit, it will always return '3', the last digit of 103. To fix this, we need +to modify the function to accept a parameter and return the last digit of that parameter.*/ // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem +// the return function is not using the parameter passed to it and is instead using a constant value + + +// Finally, correct the code to fix the problem +// =============> write your new code here + +function getLastDigit(number) { + return number.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)}`); + From 4fa6e0467f765a8f897ff6498123bd27e1f1f926 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 25 Sep 2025 11:49:20 +0100 Subject: [PATCH 07/12] function to calculate bmi has been implemented --- 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..16e89d7e5 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,13 @@ // Then when we call this function with the weight and height // It should return their Body Mass Index to 1 decimal place +let weight = 80; // in kg +let height = 1.73; // in metres + function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + let bmi = weight / (height * height); + return bmi; +} + +console.log(calculateBMI(weight, height).toFixed(1)); // should return 26.7 \ No newline at end of file From d8d8f8440ce9886b539fb0e2f6db39a82321d7ac Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 25 Sep 2025 11:58:38 +0100 Subject: [PATCH 08/12] function created for touppersnakecase --- Sprint-2/3-mandatory-implement/2-cases.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..dd0d8c8d5 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,14 @@ // 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 input1 = "hello there"; +const input2 = "lord of the rings"; + +function toUpperSnakeCase(str) { + // return the string in UPPER_SNAKE_CASE + return str.replace(/ /g, "_").toUpperCase(); // Replace spaces with underscores and convert to uppercase +} + +console.log(toUpperSnakeCase(input1)); // should return "HELLO_THERE" +console.log(toUpperSnakeCase(input2)); // should return "LORD_OF_THE_RINGS" \ No newline at end of file From 0752aa51c6ab8e3996b618af1bf68169f3227ba7 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 25 Sep 2025 12:57:53 +0100 Subject: [PATCH 09/12] created a reusable function called topounds --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 25 +++++++++++++++++++ 1 file changed, 25 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..8ee7fe495 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,28 @@ // 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 // to remove the trailing 'p' +); + +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 // all but the last two characters +); + +const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) // the last two characters + .padEnd(2, "0"); // in case there is only one character of pence + +return `£${pounds}.${pence}`; +} + +console.log(toPounds("399p")); // should log "£3.99" +console.log(toPounds("5p")); // should log "£0.05" +console.log(toPounds("89p")); // should log "£0.89" +console.log(toPounds("123456p")); // should log "£1234.56" \ No newline at end of file From faeb0ff0b093d609d5111ff25e1e58737fde49f5 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 25 Sep 2025 13:11:49 +0100 Subject: [PATCH 10/12] all questions answered and code tested. --- Sprint-2/4-mandatory-interpret/time-format.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..8017e1c60 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,24 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// pad is called 3 times because there are 3 values to be padded - hours, minutes and seconds // Call formatTimeDisplay with an input of 61, now answer the following: +console.log(formatTimeDisplay(61)); // Should return "00:01:01" // b) What is the value assigned to num when pad is called for the first time? // =============> write your answer here +// num is assigned the value of totalHours which is 0 because 61 seconds is less than a minute // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value is "00" because the padStart method adds a "0" to the start of the string "0" to make it 2 characters long // 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 +// num is assigned the value of remainingSeconds which is 1 because 61 seconds is 1 second more than a full minute (60 seconds) // 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 is "01" because the padStart method adds a "0" to the start of the string "1" to make it 2 characters long + From ebb51e5caedbc88ca7cff85acae5956fcc7be350 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Mon, 6 Oct 2025 11:54:10 +0100 Subject: [PATCH 11/12] indentation fix --- 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 8ee7fe495..6dab5bdc3 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -7,7 +7,7 @@ function toPounds(penceString) { -const penceStringWithoutTrailingP = penceString.substring( + const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 // to remove the trailing 'p' ); @@ -22,7 +22,7 @@ const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) // the last two characters .padEnd(2, "0"); // in case there is only one character of pence -return `£${pounds}.${pence}`; + return `£${pounds}.${pence}`; } console.log(toPounds("399p")); // should log "£3.99" From 345f9a23e176d10b9188707e3b077b5efec6c4ea Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Mon, 6 Oct 2025 12:00:27 +0100 Subject: [PATCH 12/12] adjusted to fix the decimal place within the function. --- 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 16e89d7e5..1323b92d7 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -20,7 +20,7 @@ let height = 1.73; // in metres function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height let bmi = weight / (height * height); - return bmi; + return bmi.toFixed(1); } -console.log(calculateBMI(weight, height).toFixed(1)); // should return 26.7 \ No newline at end of file +console.log(calculateBMI(weight, height)); // should return 26.7 \ No newline at end of file