From 68bac7f07363b35497bea87505a86fe473d3f9eb Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 11:36:58 +0100 Subject: [PATCH 01/12] Sprint2/1-key-errors --- Sprint-2/1-key-errors/0.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..6806da501 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,21 @@ // Predict and explain first... // =============> write your prediction here +// PREDICTION: There will be a SyntaxError because 'str' is declared twice - once as parameter, once as variable // 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 +// 1. Cannot redeclare 'str' with 'let' inside function - it's already the parameter name +// 2. 'explain' is not defined - should be a string literal +console.log(capitalise("hello")); // Test the function // =============> write your new code here +function capitalise(str) { +let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`; +return capitalised; +} \ No newline at end of file From 59f3b6388f7a4f94f0301b509b1b9203097e1695 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 11:54:33 +0100 Subject: [PATCH 02/12] Format with prettier --- Sprint-2/1-key-errors/0.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 6806da501..499f95ec5 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -16,6 +16,6 @@ console.log(capitalise("hello")); // Test the function // =============> write your new code here function capitalise(str) { -let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`; -return capitalised; -} \ No newline at end of file + let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalised; +} From 81fb70c7182401532ce99d259221215b99c7f0a1 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 12:26:00 +0100 Subject: [PATCH 03/12] Sprint2/01-key-errors --- Sprint-2/1-key-errors/1.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..46408f9da 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,27 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// PREDICTION: There will be a SyntaxError because 'decimalNumber' is declared twice - once as parameter, once as variable // 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 - +// Cannot redeclare 'decimalNumber' with 'const' inside function - it's already the parameter name // 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)); \ No newline at end of file From 9d22332a6e80d45c3ee6ec8531bf334f0f1b937e Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 12:31:12 +0100 Subject: [PATCH 04/12] Sprint2/2 --- Sprint-2/1-key-errors/2.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..94b63153e 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,23 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// PREDICTION: There will be a SyntaxError because '3' is not a valid parameter name -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// The error message indicates that the function parameter is not valid because '3' is a number literal, not a variable name. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} - +console.log(square(5)); // Test the function From 299b451f310d2c11ef239e750ce3a9a31a58f249 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 13:17:11 +0100 Subject: [PATCH 05/12] Sprint2/2/0.js --- Sprint-2/2-mandatory-debug/0.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..79d72e1f6 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// PREDICTION: The function will log the result of the multiplication, but the template literal will not include the result because multiply() does not return a 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 +// EXPLANATION: The multiply function logs the product of a and b, but it does not return any value. When we try to use multiply(10, 32) inside the template literal, it evaluates to undefined because there is no return statement in the function. Therefore, the output will be "The result of multiplying 10 and 32 is undefined". // 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 4e6985ad70343ebb3644f63e5bd36d0063135052 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 13:33:23 +0100 Subject: [PATCH 06/12] Sprint2/2/1.js --- Sprint-2/2-mandatory-debug/1.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..77e4b49a9 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,21 @@ // Predict and explain first... // =============> write your prediction here +// PREDICTION: The function will not return the sum because the return statement is placed before the actual addition operation. -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 return statement is placed before the addition operation, so the function will return undefined instead of the sum. + // 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 51a54b4a8f094775f756347cdb6ebeb138227b9a Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 13:40:50 +0100 Subject: [PATCH 07/12] Sprint2/2/2.js --- Sprint-2/2-mandatory-debug/2.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..18a5e619a 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,38 @@ // Predict the output of the following code: // =============> Write your prediction here +// PREDICTION: The function will log the last digit of the number passed to it, but it will not work as intended because the num variable is defined outside the function and is not being passed as an argument. -const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// const num = 103; -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)}`); +// 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)}`); // 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 +// The output is not as expected because the getLastDigit function is not using the argument passed to it. Instead, it is using the num variable defined outside the function, which is not being updated with the new values. // =============> write your explanation here +// The getLastDigit function should take a number as an argument and return the last digit of that number. However, it is currently using the num variable defined outside the function, which is not being updated with the new values passed to the function. // 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 getLastDigit function should take a number as an argument and return the last digit of that number. However, it is currently using the num variable defined outside the function, which is not being updated with the new values passed to the function. \ No newline at end of file From 9dc919f9b41112a1e86027a47382e58dfd2e1820 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 14:00:22 +0100 Subject: [PATCH 08/12] 1-bmi.js --- Sprint-2/3-mandatory-implement/1-bmi.js | 5 ++++- 1 file changed, 4 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..7225aa056 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,7 @@ 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(calculateBMI(70, 1.73)); // should return 23.4 \ No newline at end of file From d80ed874c3ff466119e06da980680dc71c45200f Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 14:14:03 +0100 Subject: [PATCH 09/12] 2-cases.js --- 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..017ed4efd 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) { + // convert the string to UPPER_SNAKE_CASE + return str.toUpperCase().replaceAll(" ", "_"); +} +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); \ No newline at end of file From ee22da6e5cff536b1e6be50b873f15a2d42fcccf Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 14:30:55 +0100 Subject: [PATCH 10/12] 3-to-pounds --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 37 +++++++++++++++++++ 1 file changed, 37 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..6bcce28bd 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,40 @@ // 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 +// const penceString = "399p"; + +// 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"); + + +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")); + +// This program takes a string representing a price in pence +// The program then builds up a string representing the price in pounds + +// You need to do a step-by-step breakdown of each line in this program +// Try and describe the purpose / rationale behind each step + +// To begin, we can start with +// 1. const penceString = "399p": initialises a string variable with the value "399p" \ No newline at end of file From 9a48031e10f0e3178368b4e139dfd108eaf4efe5 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 11 Oct 2025 14:59:18 +0100 Subject: [PATCH 11/12] time-format.js --- 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..07b6b748d 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 +// pad will be called 3 times, once for each of totalHours, remainingMinutes, and remainingSeconds. // Call formatTimeDisplay with an input of 61, now answer the following: +console.log(formatTimeDisplay(61)); // 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, which is the value of totalHours. // 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", which is the string representation of totalHours padded to 2 digits. // 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 is 1, which is the value of remainingSeconds. This is because 61 seconds has 1 remaining second after accounting for the full minute. // 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 of pad when called for the last time is "01", which is the string representation of remainingSeconds padded to 2 digits. From ed8ad94265e77c0c9338e9519450f1276240df45 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Tue, 21 Oct 2025 17:05:20 +0100 Subject: [PATCH 12/12] Address comment on 3-to-pounds.js --- 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 6bcce28bd..a0d9cfb4d 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -32,6 +32,16 @@ function toPounds (penceString) { return `£${pounds}.${pence}`; } console.log(toPounds("399p")); +console.log(toPounds("5p")) +console.log(toPounds("007p")); // leading zeroes handled -> £0.07 +// Edge cases and inputs this function does not handle well (demonstration) +// 1) Missing trailing 'p' – the current implementation will drop the last character, +// so "399" is treated like "39" and becomes £0.39 (not what we want) +console.log(toPounds("399")); + +// 2) Different trailing character – the function does not validate the trailing 'p', +// it simply removes the last character. "399P" still returns £3.99 +console.log(toPounds("399P")); // This program takes a string representing a price in pence // The program then builds up a string representing the price in pounds