From 482253a6458eab0d51790309455ee9f6e037e8f2 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Wed, 6 May 2026 01:55:13 +0100 Subject: [PATCH 1/4] key errors done --- Sprint-2/1-key-errors/0.js | 11 +++++++++++ Sprint-2/1-key-errors/1.js | 25 +++++++++++++++++++------ Sprint-2/1-key-errors/2.js | 17 ++++++++++------- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..3a83911bf 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,8 @@ // Predict and explain first... // =============> write your prediction here +// the str variable declaration inside of the function shadows the function parameter +// if an empty string is passed to the function, trying to access element at index 0, and calling +// toUpperCase() on it will cause an error, as that value would be undefined. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +13,12 @@ function capitalise(str) { } // =============> write your explanation here +// Error message is: +//SyntaxError: Identifier 'str' has already been declared +// it means that JavaScript has already declared a variable name 'str' within the function's scope. +// so when you try to declare it in the body of the function, there's a naming conflict. + // =============> write your new code here +function capitalise2(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..10daf2114 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,32 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// First, it's trying to print decimalNumber, an undeclared value. +// Second, the decimalNumber variable name is already taken when it tries to declare it in the first +// line of the function body. // 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 +// when Javascript reads the function definition, and sees a parameter, it creates a variable with that name +// scoped to the body of the function. Then when it reads the first line within the body, the 'decimalNumber' +// name is already taken. // 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)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..222cae53f 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,23 @@ - // 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 +// the function parameter cannot be a number, or start with a number, follows same rules as variable names +// num is undefined. -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 +// function parameter is a number, which is not a valid type for parameters // Finally, correct the code to fix the problem -// =============> write your new code here - - +function square(num) { + return num * num; +} From fc9b43bc46ea5d5b2434215cc2bab8cf8a7699aa Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Wed, 6 May 2026 02:23:36 +0100 Subject: [PATCH 2/4] mandatory-debug done --- Sprint-2/2-mandatory-debug/0.js | 22 ++++++++++++++++++---- Sprint-2/2-mandatory-debug/1.js | 20 +++++++++++++++----- Sprint-2/2-mandatory-debug/2.js | 29 +++++++++++++++++++++++------ 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..207becc8e 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,28 @@ // Predict and explain first... // =============> write your prediction here +// logging the result of the function will always be undefined, since it doesn't return anything. +// if the inputs are two numbers, should console log the right number, e.g. (2, 2) -> 4 (2.5, 2.5) -> 6.25 +// otherwise, it might produce incorrect results -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(2, "asdf")}`); // =============> write your explanation here +// Javascript tries to cast non-number values to numbers before multiplying. So, "3" to 3 for example. +// if the value can't be converted to a number, it might cause unpredictable or incorrect outputs. +// e.g. Boolean true is converted to numeric 1, so true * 2 becomes 1 * 2 = 2. // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + if (Number.isFinite(a) && Number.isFinite(b)) { + return a * b; + } + throw new TypeError("Arguments must be finite numbers"); +} + +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..85fa796d9 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,23 @@ // Predict and explain first... // =============> write your prediction here +// the output of console log should be 'The sub of 10 and 32 is undefined' -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 +// since the return statement is the first thing in the function body, and it returns nothing. +// the return value of the function is undefined. +// so in the console log, it will coerce undefined to string, and print that. + // 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)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..23953feb3 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,40 @@ // Predict the output of the following code: // =============> Write your prediction here +// The output will always be 3 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 function does not take an argument, so supplying one is no use. +// Since there is no num variable inside the function, it looks for it in the nearest enclosing scope. +// It finds num = 103, so it will always operate on that variable + // 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 +// ??? it returns the last digit From b84788f68096fd2225a9860e8d5e9ab14d5f6ba9 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Wed, 6 May 2026 02:36:44 +0100 Subject: [PATCH 3/4] implement done --- Sprint-2/3-mandatory-implement/1-bmi.js | 4 ++-- Sprint-2/3-mandatory-implement/2-cases.js | 6 +++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 24 +++++++++++++++++++ 3 files changed, 32 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..2e49ce69e 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,5 @@ // 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 + return (weight / height ** 2).toFixed(1); +} diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..fe70e7f22 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(inputStr) { + return inputStr.toUpperCase().replaceAll(" ", "_"); +} + +console.log(toUpperSnakeCase("hello there")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..4e79339a8 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,27 @@ // 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 + ); + + return `£${pounds}.${pence}`; +} + +console.log(toPounds("399p")); +console.log(toPounds("9p")); +console.log(toPounds("20p")); +console.log(toPounds("1220p")); From f1bd8110d9c83e7cbe6f4c1dbcc6ad1e2d9ffc01 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Wed, 6 May 2026 02:55:42 +0100 Subject: [PATCH 4/4] interpret done --- Sprint-2/4-mandatory-interpret/time-format.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 15d579805..8c92ff587 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,24 +11,29 @@ 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 // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// 3 // 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 +// value assigned is 1, because pad is called with remainingSeconds as the argument. +// remainingSeconds is the remainder of 61 divided by 60, which is 1. // e) What is the return value of pad when it is called for the last time in this program? Explain your answer -// =============> write your answer here +// return value is "01". First, the pad function converts input num to string so the return type is string. +// Second, the function pads the start of the string with up to two "0" characters, and in this case +// since num is a single character, it only pads one "0" at the start.