From f7295fe97ad73c6a6a025f76bb0cef198ee31d7d Mon Sep 17 00:00:00 2001 From: Qhama Gwele Date: Fri, 25 Sep 2026 15:45:57 +0200 Subject: [PATCH 1/6] Complete Sprint-3 errors exercise (0.js, 1.js, 2.js) --- Sprint-3/1-key-errors/0.js | 21 ++++++++++++++++----- Sprint-3/1-key-errors/1.js | 25 +++++++++++++++++++------ Sprint-3/1-key-errors/2.js | 35 +++++++++++++++++++++++------------ 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..1778b1da9 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,24 @@ // Predict and explain first... -// =============> write your prediction here +// ==============> write your prediction here +// I predict this will throw a SyntaxError, because the parameter 'str' is +// being redeclared with 'let' inside the function. // 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; + let result = `${str[0].toUpperCase()}${str.slice(1)}`; + return result; } -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("hello")); + +// ==============> write your explanation here +// The parameter 'str' already existed in the function's scope. The original +// code tried to declare another variable with 'let str', which caused a +// naming collision — JavaScript doesn't allow redeclaring a variable with +// 'let' in the same scope. This threw a SyntaxError before the function +// could even run. Renaming the new variable to 'result' fixes the collision. + +// ==============> write your new code here +// (the fixed function above already satisfies this) \ No newline at end of file diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..a6d1f25c2 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,33 @@ // Predict and explain first... - // Why will an error occur when this program runs? -// =============> write your prediction here +// ==============> write your prediction here +// I predict this will throw a SyntaxError, because 'decimalNumber' is being +// redeclared with 'const' inside the function, even though it's already the +// function's parameter. // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(0.5)); -// =============> write your explanation here +// ==============> write your explanation here +// 'decimalNumber' was already declared as the function's parameter. The +// original code tried to declare another variable with the same name using +// 'const decimalNumber = 0.5;', which caused a naming collision — JavaScript +// doesn't allow redeclaring a variable in the same scope. This threw a +// SyntaxError before the function could run. +// +// There was also a second issue: the original 'console.log(decimalNumber);' +// was outside the function, so 'decimalNumber' didn't exist there (it's +// scoped only inside 'convertToPercentage'), and the function was never +// actually called. The fix removes the duplicate declaration and calls +// convertToPercentage(0.5) directly, logging its return value instead. // Finally, correct the code to fix the problem -// =============> write your new code here +// ==============> write your new code here +// (the fixed function above already satisfies this) \ No newline at end of file diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..e7a20c9fe 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,31 @@ - // 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 +// this function should square any number but instead we're getting an error +// ==============> write your prediction of the error here +// I predict this will throw a SyntaxError, because '3' is used as the +// parameter name in 'function square(3)', and a number can't be used as a +// parameter name — parameter names must be valid identifiers (like variable +// names). -function square(3) { - return num * num; +function square(num) { + return num * num; } -// =============> write the error message here - -// =============> explain this error message here +console.log(square(3)); -// Finally, correct the code to fix the problem - -// =============> write your new code here +// ==============> write the error message here +// SyntaxError: Unexpected number +// ==============> explain this error message here +// Function parameters must be valid identifiers — the same rules as variable +// names (they must start with a letter, '$', or '_', never a digit). The +// original code used '3' as the parameter name, which isn't a valid +// identifier, so JavaScript couldn't even parse the function definition. +// This error happens before any code runs. There was also a second bug: the +// function body referred to 'num', which didn't match the (invalid) +// parameter name at all — fixing the parameter name to 'num' resolves both +// issues at once. +// Finally, correct the code to fix the problem +// ==============> write your new code here +// (the fixed function above already satisfies this) \ No newline at end of file From a25a45590d2a3782472f98300e7c0272a1ac25ab Mon Sep 17 00:00:00 2001 From: Qhama Gwele Date: Fri, 25 Sep 2026 15:50:44 +0200 Subject: [PATCH 2/6] Complete Sprint-3 debug exercises (0.js, 1.js, 2.js) --- Sprint-3/2-mandatory-debug/0.js | 22 +++++++++++++++++----- Sprint-3/2-mandatory-debug/1.js | 24 +++++++++++++++++++----- Sprint-3/2-mandatory-debug/2.js | 22 ++++++++++++++-------- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..3830879d1 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,26 @@ // Predict and explain first... - -// =============> write your prediction here +// ==============> write your prediction here +// I predict this will run without an error, but print the wrong result at +// the end. The 'multiply' function uses console.log internally instead of +// returning a value, so when it's used inside the template literal, it will +// show as 'undefined' instead of the actual multiplication result. function multiply(a, b) { - console.log(a * b); + return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// ==============> write your explanation here +// The original 'multiply' function printed the result with console.log +// instead of returning it. A function that doesn't explicitly return a value +// returns 'undefined' by default. When the outer console.log tried to use +// multiply(10, 32) inside the template literal, it inserted 'undefined' +// instead of the actual number, because it was using the function's return +// value, not what it printed internally. Changing console.log to return +// inside the function fixes this, since now the function actually hands +// back the calculated value. // Finally, correct the code to fix the problem -// =============> write your new code here +// ==============> write your new code here +// (the fixed function above already satisfies this) \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..bc49435b3 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,27 @@ // Predict and explain first... -// =============> write your prediction here +// ==============> write your prediction here +// I predict this will run without an error, but the result will show as +// 'undefined' instead of the actual sum. The 'return' statement is on its +// own line, separate from 'a + b;' on the next line — JavaScript will treat +// these as two separate statements, so the function returns immediately +// with nothing, and 'a + b' never actually gets returned. function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// ==============> write your explanation here +// JavaScript has a feature called Automatic Semicolon Insertion (ASI). When +// 'return' appears on its own line with nothing after it, JavaScript +// automatically treats it as 'return;' — ending the function right there and +// returning 'undefined'. The following line, 'a + b;', becomes dead code +// that never runs, because the function has already exited. This is why the +// original code always produced 'undefined' instead of the sum. The fix is +// to put the expression on the same line as 'return', so JavaScript knows +// it's part of the return statement: 'return a + b;'. + // Finally, correct the code to fix the problem -// =============> write your new code here +// ==============> write your new code here +// (the fixed function above already satisfies this) \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..53d98167e 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,7 +1,11 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// ==============> write your prediction here +// I predict every call to getLastDigit will return the same result: "3", +// no matter what number is passed in. This is because the function doesn't +// take any parameters — it always uses the outer 'num' variable (103) +// instead of the value passed into the function call. const num = 103; @@ -14,11 +18,13 @@ 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 -// 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 +// ==============> 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 -// This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +// Explain why the output is the way it is +// ==============> write your explanation here +// The function getLastDigit() is defined with no parameters, so the values +// 42, 105, and 806 passed into each call are simply ignored — they go +// nowhere. Instead, the \ No newline at end of file From f1b09e31fd3790130183f9c69c95c0dcd5e7171e Mon Sep 17 00:00:00 2001 From: Qhama Gwele Date: Fri, 25 Sep 2026 15:55:48 +0200 Subject: [PATCH 3/6] Complete Sprint-3 implement exercises (1-bmi.js, 2-cases.js, 3-to-pounds.js) --- Sprint-3/3-mandatory-implement/1-bmi.js | 7 ++++++- Sprint-3/3-mandatory-implement/2-cases.js | 7 +++++++ Sprint-3/3-mandatory-implement/3-to-pounds.js | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..cadcb2ffe 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,10 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height + const bmi = weight / (height * height); + return bmi.toFixed(1); } + +console.log(calculateBMI(70, 1.73)); +console.log(calculateBMI(60, 1.6)); +console.log(calculateBMI(90, 1.85)); \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..33489b2ad 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/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 convertToUpperSnakeCase(str) { + return str.toUpperCase().split(' ').join('_'); +} + +console.log(convertToUpperSnakeCase("hello there")); +console.log(convertToUpperSnakeCase("lord of the rings")); \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..0a8f33627 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,11 @@ // 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(kg) { + return kg * 2.20462; +} + +console.log(toPounds(70)); +console.log(toPounds(60)); +console.log(toPounds(90)); \ No newline at end of file From 28c76384bfed7a70340943fc63b58f521b4c997b Mon Sep 17 00:00:00 2001 From: Qhama Gwele Date: Fri, 25 Sep 2026 15:58:50 +0200 Subject: [PATCH 4/6] Complete Sprint-3 interpret exercise (time-format.js) --- Sprint-3/4-mandatory-interpret/time-format.js | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..26c21b5e1 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -11,28 +11,41 @@ function formatTimeDisplay(seconds) { const totalMinutes = (seconds - remainingSeconds) / 60; const remainingMinutes = totalMinutes % 60; const totalHours = (totalMinutes - remainingMinutes) / 60; - return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } -// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit +// 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 +// ==============> write your answer here +// pad will be called 3 times — once for totalHours, once for remainingMinutes, +// and once for remainingSeconds, since all three appear inside the template +// literal on the return line. // 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 - -// c) What is the return value of pad when it is called for the first time? -// =============> write your answer here - -// 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 - -// 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 +// ==============> write your answer here +// num = 0 (this is totalHours, since (61-1)/60 = 1 minute total, and +// (1-1)/60 = 0 hours, and pad(totalHours) is the first call in the template +// literal) + +// c) What is the return value of pad it is called for the first time? +// ==============> write your answer here +// "00" — pad(0) converts 0 to the string "0", which has length 1, so a +// leading "0" is added, making it "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 +// num = 1 (this is remainingSeconds — 61 % 60 = 1 — and pad(remainingSeconds) +// is the last of the three pad() calls in the template literal) + +// 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 +// "01" — pad(1) converts 1 to the string "1", which has length 1, so a +// leading "0" is added, making it "01" + +console.log(formatTimeDisplay(61)); // "00:01:01" \ No newline at end of file From b74b98b569d179ef8efd1195a1b58efb9badcb37 Mon Sep 17 00:00:00 2001 From: Qhama Gwele Date: Fri, 25 Sep 2026 16:00:34 +0200 Subject: [PATCH 5/6] Complete Sprint-3 stretch-extend exercise (format-time.js) --- Sprint-3/5-stretch-extend/format-time.js | 51 +++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..a18c7f5db 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -4,12 +4,21 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3); + + if (hours === 0) { + return `12:${minutes} am`; + } + if (hours === 12) { + return `12:${minutes} pm`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}:${minutes} pm`; } return `${time} am`; } +// Original tests const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( @@ -23,3 +32,43 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +// Edge case: midnight +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +// Edge case: noon +const currentOutput4 = formatAs12HourClock("12:00"); +const targetOutput4 = "12:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +// Edge case: PM time with non-zero minutes (checks minutes aren't lost) +const currentOutput5 = formatAs12HourClock("13:45"); +const targetOutput5 = "1:45 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + +// Edge case: one minute before midnight +const currentOutput6 = formatAs12HourClock("23:59"); +const targetOutput6 = "11:59 pm"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); + +// Edge case: noon with non-zero minutes +const currentOutput7 = formatAs12HourClock("12:15"); +const targetOutput7 = "12:15 pm"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}` +); \ No newline at end of file From 42903a4ca81f487bf325247720091222c2b2bd89 Mon Sep 17 00:00:00 2001 From: Qhama Gwele Date: Fri, 25 Sep 2026 19:43:48 +0200 Subject: [PATCH 6/6] Fix formatAs12HourClock logic in Sprint 3 --- Sprint-3/5-stretch-extend/format-time.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index a18c7f5db..bbff38367 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -8,14 +8,13 @@ function formatAs12HourClock(time) { if (hours === 0) { return `12:${minutes} am`; - } - if (hours === 12) { + } else if (hours === 12) { return `12:${minutes} pm`; - } - if (hours > 12) { + } else if (hours > 12) { return `${hours - 12}:${minutes} pm`; + } else { + return `${hours}:${minutes} am`; } - return `${time} am`; } // Original tests