From 47f18bb6650de15bbe55b70a903e6874c64e9319 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Thu, 18 Sep 2025 14:40:48 +0100 Subject: [PATCH 1/7] Complete 1-key-errors --- Sprint-2/1-key-errors/0.js | 17 +++++++++++------ Sprint-2/1-key-errors/1.js | 24 +++++++++++++++--------- Sprint-2/1-key-errors/2.js | 26 +++++++++++++++++--------- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..053adb770 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,18 @@ // Predict and explain first... -// =============> write your prediction here +// The function is supposed to convert the first character of a string to uppercase +// There will be a syntax error due to the redeclaration of the variable 'str' // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +capitalise("hello"); + +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } + +// The error occurred because 'str' was declared again with 'let', which is not allowed. function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + return `${str[0].toUpperCase()}${str.slice(1)}`; } - -// =============> write your explanation here -// =============> write your new code here diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..e0fc9b16a 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,26 @@ // Predict and explain first... +// The function is intended to take a decimal number and convert it to a percentage (e.g. 1.5 => 150%) // Why will an error occur when this program runs? -// =============> write your prediction here +// There will be a syntax error due to the redeclaration of the variable 'decimalNumber' // 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 +// Redeclaring the variable decimalNumber causes a syntax error +// The parameter decimalNumber cannot be logged to the console as it is a parameter and holds no value until the function is called // Finally, correct the code to fix the problem -// =============> write your new code here +function convertToPercentage(decimalNumber) { + return `${decimalNumber * 100}%`; +} + +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..89a921a56 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,28 @@ - // 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 +// A number is not a valid parameter (Syntax Error) -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } -// =============> write the error message here +// Failed to instrument Sprint-2/1-key-errors/2.js +// 7 | +// > 8 | function square(3) { +// | ^ SyntaxError: Unexpected token (8:16) +// 9 | return num * num; +// 10 | } +// 11 | +// at /Sprint-2/1-key-errors/2.js:8:16 -// =============> explain this error message here +// parameter names cannot start with a digit // Finally, correct the code to fix the problem -// =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(3)); From f682c6c07b128365378135e60081f686f3067006 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Thu, 18 Sep 2025 20:24:27 +0100 Subject: [PATCH 2/7] Complete 2-mandatory-debug --- Sprint-2/2-mandatory-debug/0.js | 19 ++++++++++------- Sprint-2/2-mandatory-debug/1.js | 19 ++++++++++------- Sprint-2/2-mandatory-debug/2.js | 37 +++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..194ee8d37 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,19 @@ // Predict and explain first... -// =============> write your prediction here +// The result of a * b will be printed to the console +// Since the function does not return any value, it will return undefined -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 +// Because the function is evaluated before the string is printed to the console, it will log the result of a * b to the console first, but because it does not return a value, the next console.log of the string will print '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)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..eaed15fd2 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,18 @@ // Predict and explain first... -// =============> write your prediction here +// The function is intended to take 2 arguments and return the sum +// it will return undefined +// function sum(a, b) { +// return; +// a + b; +// } + +// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +// it currently has a return statement that ends the function prematurely, causing it to return undefined +// Finally, correct the code to fix the problem 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 -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..4f1588e60 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,35 @@ // Predict and explain first... +// The function intends to take a number and return it's last digit +// Each call of the function will return the same result // Predict the output of the following code: -// =============> Write your prediction here +// 'The last digit of 42 is 3' +// 'The last digit of 105 is 3' +// 'The last digit of 806 is 3' -const num = 103; +// 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 +// Because the function was not correctly defined to accept an argument, It was using a fixed variable instead + // Finally, correct the code to fix the problem -// =============> write your new code here +function getLastDigit(num) { + return num.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 +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)}`); From 29f55b42f53c46cf39f8801ed80a0c6c981aefcd Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sat, 20 Sep 2025 16:58:14 +0100 Subject: [PATCH 3/7] Complete 3-mandatory-implement --- Sprint-2/3-mandatory-implement/1-bmi.js | 6 ++++-- Sprint-2/3-mandatory-implement/2-cases.js | 7 +++++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 20 +++++++++++++++++++ 3 files changed, 31 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..6f15614d0 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,7 @@ // 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); +} + +console.log(calculateBMI(70, 1.73)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..4a4c18f12 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(string) { + return string.replace(/\s+/g, "_").toUpperCase(); +} + +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..0ea37b25c 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,23 @@ // 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) { + penceString = penceString.endsWith("p") + ? penceString.slice(0, -1) + : penceString; + + const paddedString = penceString.padStart(3, "0"); + + const pounds = paddedString.slice(0, -2); + + const pence = paddedString.slice(paddedString.length - 2); + + return `£${pounds}.${pence}`; +} + +// Example calls +console.log(toPounds("399p")); +console.log(toPounds("5p")); +console.log(toPounds("123")); +console.log(toPounds("4567")); From 4361e74dcdac60069ead2e51c0588afd2f5ebdc8 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sat, 20 Sep 2025 17:06:14 +0100 Subject: [PATCH 4/7] Complete 4-mandatory-interpret --- Sprint-2/4-mandatory-interpret/time-format.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..321b454fa 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,21 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> 3 times // 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 +// =============> 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 +// =============> 1 +// The last call to pad is for remaining seconds, which is 1 (since 61 % 60 = 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 +// =============> "01" +// pad(1) returns "01" because padStart(2, "0") adds a leading zero From f7c0a35e24475dc533f2d3d14ae4eaa47d027a3a Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sat, 27 Sep 2025 13:08:09 +0100 Subject: [PATCH 5/7] Complete 5-stretch-extend --- Sprint-2/5-stretch-extend/format-time.js | 77 +++++++++++++++++++++++- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..52c68d525 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,8 +4,9 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const minutes = time.slice(3, 5); + if (hours >= 12) { + return `${hours - 12}:${minutes} pm`; } return `${time} am`; } @@ -23,3 +24,75 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +// Midnight +const outMidnight = formatAs12HourClock("00:00"); +const targetMidnight = "00:00 am"; +console.assert( + outMidnight === targetMidnight, + `current output: ${outMidnight}, target output: ${targetMidnight}` +); + +// Noon +const outNoon = formatAs12HourClock("12:00"); +const targetNoon = "12:00 pm"; +console.assert( + outNoon === targetNoon, + `current output: ${outNoon}, target output: ${targetNoon}` +); + +// 1 PM +const out13 = formatAs12HourClock("13:00"); +const target13 = "1:00 pm"; +console.assert( + out13 === target13, + `current output: ${out13}, target output: ${target13}` +); + +// 12:59 PM +const out1259 = formatAs12HourClock("12:59"); +const target1259 = "12:59 pm"; +console.assert( + out1259 === target1259, + `current output: ${out1259}, target output: ${target1259}` +); + +// 11:59 PM +const out2359 = formatAs12HourClock("23:59"); +const target2359 = "11:59 pm"; +console.assert( + out2359 === target2359, + `current output: ${out2359}, target output: ${target2359}` +); + +// 01:00 AM +const out1 = formatAs12HourClock("01:00"); +const target1 = "01:00 am"; +console.assert( + out1 === target1, + `current output: ${out1}, target output: ${target1}` +); + +// 10:30 AM +const out1030 = formatAs12HourClock("10:30"); +const target1030 = "10:30 am"; +console.assert( + out1030 === target1030, + `current output: ${out1030}, target output: ${target1030}` +); + +// 15:45 PM +const out1545 = formatAs12HourClock("15:45"); +const target1545 = "3:45 pm"; +console.assert( + out1545 === target1545, + `current output: ${out1545}, target output: ${target1545}` +); + +// Edge: single digit hour +const outSingle = formatAs12HourClock("7:00"); +const targetSingle = "7:00 am"; +console.assert( + outSingle === targetSingle, + `current output: ${outSingle}, target output: ${targetSingle}` +); From 23a25caa79e15fcdf0510ba798287ef2c5c4e10b Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Sat, 27 Sep 2025 13:31:47 +0100 Subject: [PATCH 6/7] fix: correct time formatting for 12 PM and update edge case test --- Sprint-2/5-stretch-extend/format-time.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 52c68d525..0c29d968c 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -6,7 +6,7 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); const minutes = time.slice(3, 5); if (hours >= 12) { - return `${hours - 12}:${minutes} pm`; + return `${hours !== 12 ? hours - 12 : hours}:${minutes} pm`; } return `${time} am`; } @@ -90,8 +90,8 @@ console.assert( ); // Edge: single digit hour -const outSingle = formatAs12HourClock("7:00"); -const targetSingle = "7:00 am"; +const outSingle = formatAs12HourClock("7:45"); +const targetSingle = "7:45 am"; console.assert( outSingle === targetSingle, `current output: ${outSingle}, target output: ${targetSingle}` From e39749fde7c1cc6a9a3a8b37f1124ac7e90db6ca Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Mon, 1 Dec 2025 16:50:06 +0000 Subject: [PATCH 7/7] fix: improve BMI calculation precision and return type --- Sprint-2/3-mandatory-implement/1-bmi.js | 3 ++- Sprint-2/5-stretch-extend/format-time.js | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 6f15614d0..b74339e02 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,7 +15,8 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - return (weight / height ** 2).toFixed(1); + const bmi = weight / height ** 2; + return Number(bmi.toFixed(1)); } console.log(calculateBMI(70, 1.73)); diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 0c29d968c..cfc7e80aa 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -5,7 +5,16 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); const minutes = time.slice(3, 5); - if (hours >= 12) { + + if (hours === 0) { + return `12:${minutes} am`; + } + + if (hours === 12) { + return `${hours}:${minutes} pm`; + } + + if (hours > 12) { return `${hours !== 12 ? hours - 12 : hours}:${minutes} pm`; } return `${time} am`; @@ -27,7 +36,7 @@ console.assert( // Midnight const outMidnight = formatAs12HourClock("00:00"); -const targetMidnight = "00:00 am"; +const targetMidnight = "12:00 am"; console.assert( outMidnight === targetMidnight, `current output: ${outMidnight}, target output: ${targetMidnight}`