From 43a1dfdb2f4750a1b65e554b6fb92715bd8b5baa Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Mon, 26 Jan 2026 21:44:45 +0000 Subject: [PATCH 01/11] fix: resolve naming conflict and add scope documentation --- Sprint-2/1-key-errors/0.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..96e479534 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -4,10 +4,25 @@ // 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) { +// const capiLetter = `${str[0].toUpperCase()}${str.slice(1)}`; +// return capiLetter; +// } // =============> write your explanation here + + // The original code failed for two reasons: first the function was never invoke, + // there was a variable naming conflict with the function scope. + // Since str was already defined as a parameter, can not be used to re-declare + // a variable inside the function. To fix this, I moved the processed directly and + // returned the result directly + // =============> write your new code here + + +function capitalise(str) { + + return `${str[0].toUpperCase()}${str.slice(1)}`; +} + +console.log(capitalise("hello")); \ No newline at end of file From 8fb443a99eacf58a969e502a70c3ad0423096a6d Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Mon, 26 Jan 2026 22:20:25 +0000 Subject: [PATCH 02/11] fix: fixed the code that was causing fix: resolve scope error and stop ignoring parameters with hardcoded values --- Sprint-2/1-key-errors/1.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..50b20e3d2 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,41 @@ // Predict and explain first... +const { t } = require("tar"); + // Why will an error occur when this program runs? // =============> 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}%`; +// 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 +// This function will throw a SyntaxError because decimalNumber +// is being redeclared inside the same scope as the parameter. +// Additionally, the function ignores the input parameter +// because the value is hardcoded inside the body. +// Finally, the console.log will throw a ReferenceError +// because it tries to access a local variable from the global scope. +// To fix this, we must remove the internal declaration to use the +// parameter dynamically and invoke the function correctly. + // Finally, correct the code to fix the problem // =============> write your new code here + + + +function convertToPercentage(num) { + + const percentage = `${num * 100}%`; + return percentage; +} + +console.log(convertToPercentage(0.80)); \ No newline at end of file From 7655d2ad81ac5a28f42fa47f2ce08afc720b8bed Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Mon, 26 Jan 2026 22:34:10 +0000 Subject: [PATCH 03/11] fix: replace numeric literal with identifier in function parameter --- Sprint-2/1-key-errors/2.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..ce9e23650 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -5,12 +5,19 @@ // =============> write your prediction of the error here -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } + // =============> write the error message here + // It will throw a syntaxError because a literal Number(3) cannot be used + // as a function parameter. furthermore, the return statement will + // cause an referenceError because it attempts to use a var num that + // has no been defined. To fix this, we must replace the number in + // the function with the identifier num. + // =============> explain this error message here // Finally, correct the code to fix the problem @@ -18,3 +25,10 @@ function square(3) { // =============> write your new code here + +function square(num) { + return num * num; +} + + +console.log(square(9)); \ No newline at end of file From 627e70db054464fd3738416805cb9ec88efee508 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Mon, 26 Jan 2026 23:11:35 +0000 Subject: [PATCH 04/11] fix: change console.log to return statement to avoid undefined result --- Sprint-2/2-mandatory-debug/0.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..08f48e531 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -2,13 +2,22 @@ // =============> write your prediction here -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)}`); // =============> write your explanation here +//will display the first console.log and make the operation 10 * 32 +// but the second console.log will throw undefined because +// the function does not have a return statement. + // 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 e1133a2f2ed8243a294fcab61dec4cfeedd0cbc7 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Mon, 26 Jan 2026 23:19:32 +0000 Subject: [PATCH 05/11] fix: move operation to the return statement to avoid unreachable and undefined result --- Sprint-2/2-mandatory-debug/1.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..b095d1bdc 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 -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 + + // This function will return undefined because the return statement executes first. + // since the operation a + b is after the return statement, it will never be executed. + + // 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 + 32 is ${sum(10, 32)}`); \ No newline at end of file From 4739b941be5231673ba8b9aec6f4aec4d2ac6534 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Mon, 26 Jan 2026 23:41:43 +0000 Subject: [PATCH 06/11] fix: add parameter to function to use passed arguments instead of a global constant --- Sprint-2/2-mandatory-debug/2.js | 43 +++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..88f3b3e16 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,22 +3,49 @@ // Predict the output of the following code: // =============> Write your prediction here -const num = 103; + // The output will always be 3 for all cases. Even though + // arguments are passed to the function, it lacks parameter + // to receive them. Therefore, the function defaults to using + // the global scope constant num = 103 every time it runs. -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 output is: + // 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 getLastDigit is not working properly because + // it is hardcoded to use the global constant num. Since + // the function definition does not include any parameters, + // it ignores the arguments (42, 105, 806) passed during the + // call. As a result, it always calculates the last digit of 103, which is 3 + // Finally, correct the code to fix the problem // =============> write your new code here +const num = 103; //this now is a ghost variable and is not used in the function + +function getLastDigit(value) { + return value.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 +// Explain why getLastDigit is not working properly - correct the problem \ No newline at end of file From ee55dfe8a1508689fdb36f8943106d8f24fc8131 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Fri, 30 Jan 2026 21:38:44 +0000 Subject: [PATCH 07/11] feat: add a new math operation to round a number and get 1 decimal place. --- Sprint-2/3-mandatory-implement/1-bmi.js | 18 ++++++++++++++++-- 1 file changed, 16 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..b7297a227 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,19 @@ // 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 the BMI of someone based off their weight and height + const squareHeight = height * height; + const operation = weight / squareHeight; + console.log(operation); + + const bmi = Math.round(operation * 10) / 10; + + return bmi; +} + +console.log(calculateBMI(70, 1.73)); + + +// to get 23.4 we use the Math.round(operation * 10) / 10; to move the decimal +// one place to the right, round it to the nearest whole number, then move it +// back by dividing by 10. \ No newline at end of file From 854cdb4fed60924b0c2d816942e98ce9fa3560b2 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Sat, 31 Jan 2026 16:17:29 +0000 Subject: [PATCH 08/11] feat: add new methods function to convert an string input as upperCase and remove the spaces and replace with underScores. I implement toUpperCase() and replace() --- Sprint-2/3-mandatory-implement/2-cases.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..c9289de6c 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -12,5 +12,14 @@ // Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" // You will need to come up with an appropriate name for the function -// Use the MDN string documentation to help you find a solution +// Use the MDN string documentation to help you find a solutio // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase + + +function toUpperSnakeCase(str) { + + return str.toUpperCase().replaceAll(" ", "_"); +} + +// console.log(toUpperSnakeCase("Hello there")); // "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS" \ No newline at end of file From 6e1b61c75c85b708e64263b9e1d8b13266786933 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Sun, 1 Feb 2026 21:28:45 +0000 Subject: [PATCH 09/11] refactor: reusable function code to treat string to convert to only number and set the display result as Money --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 60 +++++++++++++++++++ 1 file changed, 60 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..2e77565f7 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,63 @@ // 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 + + +// =============================== Original Code ======================== +// 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"); + +// console.log(`£${pounds}.${pence}`); + + + +// =============================== Refactoring ======================== + +// to make robust function we can use some maths methods to reduce the looping and +// return only the numbers, like this: /[^\d]/g, with that we can remove all +// characters that are no number, then we can convert the number into decimals by +// dividing by 100. + +//padStart method can be used to ensure that strings have a minimum length, no used in numbers +//The toFixed() method formats a number using fixed-point notation. + +// In programming, we call this a Regular Expression (Regex). Think of it as a custom filter for text. + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions + +// REGEX BREAKDOWN + +// Delimiters (/ /): The "brackets" that mark the start and end of the Regex. +// Non-digit (\D): Matches any character that is NOT a number (0-9). +// Negated Set ([^\d]): The long version of \D (Anything that is NOT a digit). +// Global Flag (g): Tells the code to clean the entire string, not just the first letter. +// Empty String (''): Replaces the "bad" characters with nothing (deleting them). +// Example: "£1.50p" -> "150" +// const onlyNumbers = str.replace(/\D/g, ''); + +// =============================== Reusable Code ======================== + + +function toPounds(str){ + + const onlyNumbers = str.replace(/[^\d]/g, ''); + const decimals = (onlyNumbers/100).toFixed(2); + + return `£${decimals}`; +} + +console.log(toPounds("hfhbgjk1p")); \ No newline at end of file From 298e008a981ec499955489067ffe49551e80b3b5 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Sun, 1 Feb 2026 21:47:42 +0000 Subject: [PATCH 10/11] refactor: added two way to test the function to convert str to pounds --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 2e77565f7..ec6cafb74 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -61,6 +61,21 @@ function toPounds(str){ const decimals = (onlyNumbers/100).toFixed(2); return `£${decimals}`; +}; + + + +// =============================== 1st way to test ======================== + +const examples = ["79p", "hfhbgjk1p", "150p", "£2.50", "11000"]; +for (const example of examples) { + console.log(toPounds(example)); } -console.log(toPounds("hfhbgjk1p")); \ No newline at end of file + +// =============================== 2nd way to test ======================== + +// const examples2 = ["79p", "hfhbgjk1p", "150p", "£2.50", "12000"]; +// for (const example of examples2) { +// console.log(`Original: ${example} -> Formatted: ${toPounds(example)}`); +// }; \ No newline at end of file From 558ea3740fe6bc4ff99c3381e85248a24c367bfc Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Sun, 1 Feb 2026 22:55:47 +0000 Subject: [PATCH 11/11] docs: explanation how is iterating both functions when they are invoke, one is to separate hours, minutes and seconds and the pad() is formatting the time displayed --- Sprint-2/4-mandatory-interpret/time-format.js | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..0e7fb5217 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -3,14 +3,21 @@ function pad(num) { } function formatTimeDisplay(seconds) { - const remainingSeconds = seconds % 60; + const remainingSeconds = seconds % 60; + //get the remaining seconds after dividing by 61/60 = 1 remainder 1 const totalMinutes = (seconds - remainingSeconds) / 60; + //get seconds = 1 - 60; result is 60/60 = total minutes 1 const remainingMinutes = totalMinutes % 60; + //the remainingMinutes = 1 & 60 = 1; because the totalMinutes is less than + // 60 the number will always the number from left side of the modulus operator const totalHours = (totalMinutes - remainingMinutes) / 60; return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } + +console.log(formatTimeDisplay(61)); // Should print "00:01:01" + // 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 +25,24 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// pad function will be called 3 times when formatTimeDisplay is called. -// Call formatTimeDisplay with an input of 61, now answer the following: +//==== 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 +// =============> 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 it is called for the first time is "00". +// because num is 0, and when we convert it to a string and pad it to 2 +// characters with leading zeros, it becomes "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 value assigned to num when pad is called for the last time in this program is 1 // 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".