From 9b03bec68f00888dea9679dc58cedfa1ec7790d6 Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 15:40:58 +0000 Subject: [PATCH 01/12] 0.js committed --- Sprint-2/1-key-errors/0.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..5da9eeb2ee 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,41 @@ // Predict and explain first... // =============> write your prediction here +/** Prediction: + * The code will throw an error because the parameter str and the variable str inside the function are both declared in the same scope. + * +*/ + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +/** Original function: + * function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +*/ // =============> write your explanation here +/** Explanation: + * The error occurs because: + * + * The function parameter str is already declared in the function scope + * Inside the function, we're trying to redeclare str using let which creates a new variable in the same scope + * JavaScript doesn't allow redeclaring a variable with let in the same scope + * The error message would be something like: + * SyntaxError: Identifier 'str' has already been declared +*/ + // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +// Test the function +console.log(capitalise("hello")); // "Hello" +console.log(capitalise("world")); // "World" + From cb65503a24c47ea1a6e21e5821fb0618b367c0c1 Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 15:43:15 +0000 Subject: [PATCH 02/12] .gitignore committed on Sprint-2 --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index bde36e5302..3f87afb450 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules .DS_Store .vscode -**/.DS_Store \ No newline at end of file +*.swp +**/.DS_Store From 2a91f7286f8425b026ca5485f7a51fc872fca0a1 Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 15:47:44 +0000 Subject: [PATCH 03/12] 1.js committed --- Sprint-2/1-key-errors/1.js | 41 +++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..d799dbb503 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,47 @@ // Predict and explain first... // Why will an error occur when this program runs? + // =============> write your prediction here +/** + * Prediction: + * An error will occur because the function parameter decimalNumber is being redeclared inside the function body using const. This creates a naming conflict - you can't have a parameter and a local variable with the same name. + * Additionally, the console.log(decimalNumber) at the end will cause a ReferenceError because decimalNumber is not defined in the global scope, it's only defined within the function's scope. + */ // Try playing computer with the example to work out what is going on +/** Original function: + * + * function convertToPercentage(decimalNumber) { + * const decimalNumber = 0.5; + * const percentage = `${decimalNumber * 100}%`; + * + * return percentage; + * } + * + * console.log(decimalNumber); + */ + +// =============> write your explanation here +/** + * Explanation + * The code has two main problems: + * Redeclaration error: The function parameter decimalNumber is being redeclared with const decimalNumber = 0.5 inside the function. In JavaScript, it not possible to have a variable with the same name as a parameter in the same scope. + * Scope error: The console.log(decimalNumber) at the end is trying to access a variable that only exists inside the function's scope. Variables declared inside functions are not accessible from the outside. + * Logic error: Even if the scope issues were fixed, the function always returns "50%" regardless of the input because it overwrites the parameter with 0.5. + */ + +// Finally, correct the code to fix the problem +// =============> write your new code here + function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; - return percentage; } -console.log(decimalNumber); - -// =============> write your explanation here +// Example usage: +const decimalNumber = 0.5; +console.log(convertToPercentage(decimalNumber)); // Output: "50%" +console.log(convertToPercentage(0.80)); // Output: "80%" -// Finally, correct the code to fix the problem -// =============> write your new code here From 4542624ed8fa5948d5aa4617bb2248c60909e2bc Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 15:50:42 +0000 Subject: [PATCH 04/12] 2.js committed --- Sprint-2/1-key-errors/2.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..c4d6bafc44 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,41 @@ // 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 -function square(3) { - return num * num; -} +/** + * Prediction: + * The function parameter is incorrectly defined as a numeric literal 3 instead of a parameter name. This will cause a syntax error because JavaScript function parameters must be valid identifiers (variable names), not numbers. + */ + +/** Original function: + * + * function square(3) { + * return num * num; + * } + */ + // =============> write the error message here +/** + * Error message: + * Uncaught SyntaxError: Unexpected number + */ // =============> explain this error message here +/** Explanation: + * In JavaScript, when defining a function, the parameters must be valid variable names (like num, x, value, etc.). Using a literal number like 3 as a parameter is invalid syntax because: + * Parameters act as placeholders for values that will be passed when the function is called + * Numbers can not be used as variable names in JavaScript + * The parser expects a valid identifier in the parameter declaration, not a numeric literal + */ // Finally, correct the code to fix the problem - // =============> write your new code here +function square(num) { + return num * num; +} + +console.log(square(3)); From 985cde482a5e8becbe0cd0a987b397aa67103413 Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 17:21:53 +0000 Subject: [PATCH 05/12] 0.js committed --- Sprint-2/2-mandatory-debug/0.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..d10987b6cd 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,33 @@ // Predict and explain first... // =============> write your prediction here +/** + * It display "320" and "The result of multiplying 10 and 32 is undefined" + */ +/** 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 +/** Explanation: + * + * The following function takes two arguments a and b as input and display the multiplication result. + * But instead of using the function "console.log" it shoudl use the function "return (a * b)". + * That's the reason it displays "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 da968b77616a7bc7a953747c3fbc315b55a6b5b9 Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 17:24:05 +0000 Subject: [PATCH 06/12] 1.js committed --- Sprint-2/2-mandatory-debug/1.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..a42169a3e5 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 +/** + * It returns an "undefined" + */ -function sum(a, b) { +/** function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +*/ // =============> write your explanation here + /** Explanation: + * The arguments "a + b" should be on the same line as the return so the sum returns that value instead of "undefined" + */ + // 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)}`); + From 5b7317764ab52a09286fee613d18b75ed81ba4dc Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 17:26:36 +0000 Subject: [PATCH 07/12] 2.js committed --- Sprint-2/2-mandatory-debug/2.js | 62 +++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..0049e86c9e 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,22 +3,70 @@ // Predict the output of the following code: // =============> Write your prediction here -const num = 103; +/** + * The last digit of 42 input is '3' + * The last digit of 105 input is '3' + * The last digit of 806 input is '3' + */ -function getLastDigit() { - return num.toString().slice(-1); -} +/** + * Original function: + * + * const num = 103; + * + * 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 input is '3' + * The last digit of 105 input is '3' + * The last digit of 806 input is '3' + */ + // Explain why the output is the way it is // =============> write your explanation here + +/** + * Explanation: + * + * The function getLastDigit() is not working properly because: + * It's ignoring the parameter: The function is defined to take a parameter, but when calling the function it's using the global variable num (which is set to 103) instead of the parameter passed to it. + * There is no parameters in function definition. + * The function is defined as function getLastDigit() without any parameters, so when we call getLastDigit(42), the 42 is ignored. + * Fixed value: The function always returns the last digit of 103 (which is "3"), regardless of what number is passed to it. + * That's why all three console logs show "3" - they're all getting the last digit of 103, not the numbers 42, 105, and 806 passed. + */ + // Finally, correct the code to fix the problem // =============> write your new code here +const num = 103; + +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)}`); + +function getLastDigit(number) { + return number.toString().slice(-1); +} + // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem + +/** + * The key fix was adding a parameter number to the function definition and using that parameter inside the function instead of the global num variable. Now each call to the function getLastDigit() works with the specific number passed to it. + */ + From 3cfeecd3d08e5ec112b0a9076ba0bb4d9280fedc Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 17:30:04 +0000 Subject: [PATCH 08/12] 1-bmi.js committed --- Sprint-2/3-mandatory-implement/1-bmi.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..7e892632f2 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,22 @@ // Then when we call this function with the weight and height // It should return their Body Mass Index to 1 decimal place +/** + * Original function: + * function calculateBMI(weight, height) { + * return the BMI of someone based off their weight and height + *} + */ + function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + // Calculate BMI: weight divided by height squared + const bmi = weight / (height * height); + + // Return the result rounded to 1 decimal place using the built-in Math library + return Math.round(bmi * 10) / 10; +} + +// Test the function +console.log(calculateBMI(70, 1.73)); // Should output: 23.4 +console.log(calculateBMI(80, 1.80)); // Test with another example and returns 24.7 + From e27fa7b78152d3786a06d9bb6fad15b8126a3e71 Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 17:31:51 +0000 Subject: [PATCH 09/12] 2-cases.js committed --- Sprint-2/3-mandatory-implement/2-cases.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..0f1664dea1 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,22 @@ // 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 convertToUpperSnakeCase(inputString) { + // Split the string into words using spaces as separators + const words = inputString.split(' '); + + // Convert each word to uppercase using map() function + const upperWords = words.map(word => word.toUpperCase()); + + // Join the uppercase words with underscores + const upperSnakeString = upperWords.join('_'); + + return upperSnakeString; +} + +// Test the function +console.log(convertToUpperSnakeCase("hello there")); // HELLO_THERE +console.log(convertToUpperSnakeCase("lord of the rings")); // LORD_OF_THE_RINGS +console.log(convertToUpperSnakeCase("UPPER SNAKE CASE")); // UPPER_SNAKE_CASE + From c1b8638a5fa4e77d541741dcbe65d056ee7b075e Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 17:34:02 +0000 Subject: [PATCH 10/12] 3-to-pounds.js committed --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..9932d177b9 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,41 @@ // 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 + + +// REUSABLE FUNCTION SOLUTION: + +function toPounds(penceString) { + // Remove the trailing 'p' character + const penceStringWithoutTrailingPound = penceString.substring( + 0, + penceString.length - 1 + ); + + // Pad with leading zeros to ensure at least 3 characters + const paddedPenceNumberString = penceStringWithoutTrailingPound.padStart(3, "0"); + + // Extract pounds (all except last 2 digits) + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + // Extract pence (last 2 digits) and ensure it's 2 digits + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + // Return the formatted string + return `£${pounds}.${pence}`; +} + +// TEST THE FUNCTION: + +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("99p")); // £0.99 +console.log(toPounds("5p")); // £0.05 +console.log(toPounds("1000p")); // £10.00 +console.log(toPounds("12345p")); // £123.45 +console.log(toPounds("0p")); // £0.00 + From 90cb4233ecb9869494d8bb2389532346c670379f Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 17:41:01 +0000 Subject: [PATCH 11/12] time-format.js committed --- Sprint-2/4-mandatory-interpret/time-format.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8c..02692ae141 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -19,16 +19,54 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +/** + * formatTimeDisplay calls pad three times inside the template literal: once for hours, once for minutes, once for seconds. + */ + // 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 +/** + * Value of num when pad is called first time: + * pad(totalHours) -> num = 0 + */ + // c) What is the return value of pad is called for the first time? // =============> write your answer here +/** + * Return value of pad first time: + * "0".padStart(2, "0") = "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 of num when pad is called last time: + * Last pad call is pad(remainingSeconds) -> remainingSeconds = 1, so num = 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 + +/** + * Return value of last pad call: + * "1".padStart(2, "0") = "01" + */ + +/** + * To recap all responses: + * + * (a) 3 + * (b) 0 + * (c) "00" + * (d) 1 — because remainingSeconds is calculated first and holds 1, then after pad(totalHours) and pad(remainingMinutes) have finished, the final call is with that remainingSeconds variable, which is still 1. + * (e) "01" — because 1 is padded with a leading zero making a two-character string "01". + */ + +// Calling the function formatTimeDisplay passing the 12,000 seconds as argument +console.log(formatTimeDisplay(12000)) //return 03:20:00 + From d512feb791e9084e1b9a495d97c61f16881e05df Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Tue, 3 Mar 2026 17:50:11 +0000 Subject: [PATCH 12/12] format-time.js committed --- Sprint-2/5-stretch-extend/format-time.js | 111 +++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..2385fb3a79 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -23,3 +23,114 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +// Test suite for formatAs12HourClock function + +function runTests() { + const testCases = [ + // Edge cases and boundaries + { input: "00:00", expected: "12:00 am", description: "Midnight" }, + { input: "12:00", expected: "12:00 pm", description: "Noon" }, + { input: "00:01", expected: "12:01 am", description: "Just after midnight" }, + { input: "11:59", expected: "11:59 am", description: "Just before noon" }, + { input: "12:01", expected: "12:01 pm", description: "Just after noon" }, + { input: "23:59", expected: "11:59 pm", description: "Just before midnight" }, + + // AM hours + { input: "01:00", expected: "01:00 am", description: "1 AM" }, + { input: "02:30", expected: "02:30 am", description: "2:30 AM" }, + { input: "09:15", expected: "09:15 am", description: "9:15 AM" }, + { input: "11:45", expected: "11:45 am", description: "11:45 AM" }, + + // PM hours + { input: "13:00", expected: "01:00 pm", description: "1 PM" }, + { input: "14:30", expected: "02:30 pm", description: "2:30 PM" }, + { input: "17:45", expected: "05:45 pm", description: "5:45 PM" }, + { input: "22:15", expected: "10:15 pm", description: "10:15 PM" }, + + // Invalid inputs + { input: "24:00", expected: "12:00 am", description: "24:00 (should be midnight)" }, + { input: "25:00", expected: null, description: "Invalid hour > 24" }, + { input: "12:60", expected: null, description: "Invalid minutes > 59" }, + { input: "9:00", expected: null, description: "Missing leading zero" }, + { input: "09-00", expected: null, description: "Wrong separator" }, + { input: "abc", expected: null, description: "Non-numeric input" }, + { input: "", expected: null, description: "Empty string" }, + { input: "12", expected: null, description: "Incomplete time" }, + { input: "12345", expected: null, description: "Too many digits" }, + ]; + + console.log("Running tests for formatAs12HourClock...\n"); + + let passed = 0; + let failed = 0; + + testCases.forEach((testCase, index) => { + try { + const result = formatAs12HourClock(testCase.input); + const passed_test = result === testCase.expected; + + if (passed_test) { + console.log(`Test ${index + 1}: ${testCase.description} - PASSED`); + passed++; + } else { + console.log(`Test ${index + 1}: ${testCase.description} - FAILED`); + console.log(`Input: "${testCase.input}"`); + console.log(`Expected: "${testCase.expected}"`); + console.log(`Got: "${result}"\n`); + failed++; + } + } catch (error) { + console.log(`Test ${index + 1}: ${testCase.description} - ERROR`); + console.log(`Input: "${testCase.input}"`); + console.log(`Error: ${error.message}\n`); + failed++; + } + }); + + console.log(`\nTests completed: ${passed} passed, ${failed} failed`); +} + +// Run the tests +runTests(); + +// Fixed Code: + +function formatAs12HourClock(time) { + // Input validation + if (typeof time !== 'string' || !time) { + return null; + } + + // Check format: HH:MM with leading zeros + const timeRegex = /^([0-1][0-9]|2[0-3]):([0-5][0-9])$/; + if (!timeRegex.test(time)) { + return null; + } + + const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + + // Handle midnight (00:00) + if (hours === 0) { + return `12:${minutes} am`; + } + + // Handle noon (12:00) and other PM hours + if (hours === 12) { + return `12:${minutes} pm`; + } + + if (hours > 12) { + const pmHours = hours - 12; + // Add leading zero for hours 1-9 + const formattedHours = pmHours < 10 ? `0${pmHours}` : `${pmHours}`; + return `${formattedHours}:${minutes} pm`; + } + + // AM hours (1-11) + // Keep leading zero for hours 1-9 + const formattedHours = hours < 10 ? `0${hours}` : `${hours}`; + return `${formattedHours}:${minutes} am`; +} +