From 38c5f48d7ee9fd4f6ccc0e3c5185b7bf7346bca8 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Wed, 23 Sep 2026 17:13:40 +0100 Subject: [PATCH 01/17] Predict and explain errors. Fix by removing let so not to make new variable with same name --- Sprint-3/1-key-errors/0.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..0bb43e817 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// the function has a parameter str, but then tries to make a new +// variable also called str. so maybe cause error because str already exists // 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) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +//} // =============> write your explanation here +// SyntaxError: Identifier 'str' has already been declared +// Prediction was correct, str already exists // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} From b862d6d123c40b7547dcfb4cc395ffff4d54c930 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Wed, 23 Sep 2026 18:08:26 +0100 Subject: [PATCH 02/17] Predict and explain errors. Fix by removing line and making variable outside function --- Sprint-3/1-key-errors/1.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..5a0da345d 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -2,19 +2,41 @@ // Why will an error occur when this program runs? // =============> write your prediction here +//decimalNumber already exists as a parameter, so const decimalNumber is making +//a new variable with the same name. There is also a problem where decimalNumber is being +//assigned 0.5, which won't cause errors, but means that no matter what is passed to the function +//decimalNumber will always be 0.5 // 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 percentage = `${decimalNumber * 100}%`; - return percentage; -} +// return percentage; +//} -console.log(decimalNumber); +//convertToPercentage(0.6) +//console.log(decimalNumber); // =============> write your explanation here +//SyntaxError: Identifier 'decimalNumber' has already been declared +//prediction correct, decimalNumber already exists so causes error. Removing the line +//shows another error +//console.log(decimalNumber); +// ^ +//ReferenceError: decimalNumber is not defined +//This is because of scope, decimalNumber is defined in the function, so it only has scope +//in the function // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +let decimalNumber = 0.6; +console.log(decimalNumber); +console.log(convertToPercentage(decimalNumber)); From d311b628577f5586e4dadd22da3e8b22bea7c0e1 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Wed, 23 Sep 2026 18:10:56 +0100 Subject: [PATCH 03/17] Predict and explain error. Fix by giving adding variable name to parameter and giving default value --- Sprint-3/1-key-errors/2.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..fd9218524 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,26 @@ - // 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 +// In the function, 3 hasn't been assigned to num, so num is NaN and should return NaN. +//Also when defining a function, the parameter should have the variable name, not a value +//to pass in, unless you want to default value such as num = 3, which assigns 3 to num if +// no value is passed in -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 - +//syntaxError means there is a mistake in the way the code is written, so javascript can't +// interpret the code. in this case there is a number here when there shouldn't be. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num = 3) { + return num * num; +} From d9257c2ede324b6e6993cd55ce87f8a227df600b Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 24 Sep 2026 20:50:19 +0100 Subject: [PATCH 04/17] Create function to calculate BMI --- Sprint-3/3-mandatory-implement/1-bmi.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..6b39f4d69 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -16,4 +16,5 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + return (weight / height ** 2).toFixed(1); } From 8dcfe07ed771e5002b24e480c9ae18983f856567 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 24 Sep 2026 21:00:23 +0100 Subject: [PATCH 05/17] Create function to return upper snake case string --- Sprint-3/3-mandatory-implement/2-cases.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..fe92c4694 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,6 @@ // 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(inputString) { + return inputString.toUpperCase().replaceAll(" ", "_"); +} From 9ff0a0b0bdc739eecc4d4d15366664ab8c6795e1 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 24 Sep 2026 21:26:27 +0100 Subject: [PATCH 06/17] Take previous code and made into function toPounds --- Sprint-3/3-mandatory-implement/3-to-pounds.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..3f42bd0bb 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,28 @@ // 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) + .padEnd(2, "0"); + + console.log(`£${pounds}.${pence}`); +} + +toPounds("10p"); +toPounds("100p"); +toPounds("232134p"); +toPounds("238p"); +toPounds("5234p"); From ebffeae3d9b3723de778deb78e804d37a31d8db7 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Fri, 25 Sep 2026 16:55:06 +0100 Subject: [PATCH 07/17] Predict and explaind bug. Fix by changing function to return a * b --- Sprint-3/2-mandatory-debug/0.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..74e23e0a4 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,25 @@ // Predict and explain first... // =============> write your prediction here +//The multiply function doesn't return anything, only prints to console so the function call will have value undefined. +//So when line 12 is executed, first it will print "The result of multiplying 10 and 32 is undefined" +//Then on a new line it prints 320 -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 - +//It first printed 320, then printed "The result of multiplying 10 and 32 is undefined" on the next line +//I think this is because it goes through the line 12, executes the multiply function call first, then finishes the +//console.log function call. // 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 818cc3281997c16a43721a207942037300178be9 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Fri, 25 Sep 2026 17:02:46 +0100 Subject: [PATCH 08/17] Predict and explain bug. Fix by moving a + b before ; in return line so properly returned --- Sprint-3/2-mandatory-debug/1.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..ef7b55841 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,23 @@ // Predict and explain first... // =============> write your prediction here +//when the return line is executed, the function exits. a + b is a separate line because it +//is after the semicolon, so a + b is never reached and not returned. So line 11 will print +//The The sum 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)}`); // =============> write your explanation here +//It did as predicted, for the reason I expected // 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 b0260ef6996c4143c8e06e3c0df24445245fe2f3 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Fri, 25 Sep 2026 17:29:20 +0100 Subject: [PATCH 09/17] Predict and explain bug. Fix by removing num declaration and adding num parameter to function getLastDigit --- Sprint-3/2-mandatory-debug/2.js | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..42f75b5f6 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,24 +1,41 @@ // Predict and explain first... - // Predict the output of the following code: +//The last digit of 42 is 3 +//The last digit of 105 is 3 +//The last digit of 806 is 3 // =============> Write your prediction here +//num is defined outside of the function, so has scope in the function getLastDigit. +//the function doesn't take any arguments and uses num, so will always return 3, no matter what +//is passed to the function because it will use num, which is set to 103 -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 +// As I predicted, for the reason above // 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 From f54452f961be3831c039b36cbd804fd054acc146 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Mon, 28 Sep 2026 15:59:25 +0100 Subject: [PATCH 10/17] Answer questions --- Sprint-3/4-mandatory-interpret/time-format.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..6fd34cd30 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -22,17 +22,27 @@ function formatTimeDisplay(seconds) { // 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 when it 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. Last time it is called is pad(remainingSeconds). remainingSeconds = seconds % 60 +// with seconds = 61 this expression evaluates to 1, so when 1 is passed to pad, num is +// assigned 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 +// "01". Num is 1 which converted to the string "1" which is assigned to numString. +// The while loop runs while numString length less than 2, and adds "0" +// to the beginning string. So "1" becomes "01". the string length is now equal to two, +// so the while loop exits and numString is returned with value "01" From 38825cb653c643300d8cb8b062d52f54f60d5386 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Mon, 28 Sep 2026 22:22:47 +0100 Subject: [PATCH 11/17] Fix bugs, extend code to catch different inputs. Write tests --- Sprint-3/5-stretch-extend/format-time.js | 254 ++++++++++++++++++++++- 1 file changed, 250 insertions(+), 4 deletions(-) diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..74be42a23 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -1,13 +1,66 @@ // This is the latest solution to the problem from the prep. // Make sure to do the prep before you do the coursework // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. - +function pad(num){ + let stringNum = num.toString(); + if (stringNum.length < 2){ + stringNum = "0" + stringNum; + return stringNum; + } + else{ + return stringNum; + } +} function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); + if((time.indexOf(":") === -1 && time.indexOf(".") === -1) || (time.indexOf(":") !== -1 && time.indexOf(".") !== -1)){ + return "Not a valid time" // check if there is only either one : or . + } + + if(time.indexOf(":") === -1){ + if(time.indexOf(".") !== time.lastIndexOf(".")) + { + return "Not a valid time" //check there is only one . + } + } + if(time.indexOf(".") === -1){ + if(time.indexOf(":") !== time.lastIndexOf(":")) + { + return "Not a valid time" //check if there is only one : + } + + } + + let colonPeriodIndex = 0; + if(time.indexOf(":") === -1){ + colonPeriodIndex = time.indexOf(".") + } + else + { + colonPeriodIndex = time.indexOf(":") + } + + if(time.slice(0, colonPeriodIndex) === "" || time.slice(colonPeriodIndex+1) === ""){ + return "Not a valid time" //check if there is a number before and after time separator + } + const hours = Number(time.slice(0, colonPeriodIndex)); + const minutes = Number(time.slice(colonPeriodIndex+1)); + const stringMinutes = pad(minutes); + const stringHours = pad(hours); + + if (isNaN(hours) || isNaN(minutes) || hours >= 24 || hours < 0 || minutes >= 60 || minutes < 0){ + return "Not a valid time"; + } //check if numbers are valid + if (hours === 12){ + return `${stringHours}:${stringMinutes} pm`; + } + if (hours === 0){ + return `12:${stringMinutes} am`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${pad(hours - 12)}:${stringMinutes} pm`; } - return `${time} am`; + + return `${stringHours}:${stringMinutes} am`; } const currentOutput = formatAs12HourClock("08:00"); @@ -23,3 +76,196 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("23:46"); +const targetOutput3 = "11:46 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("12:00"); +const targetOutput4 = "12:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("00:00"); +const targetOutput5 = "12:00 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + +const currentOutput6 = formatAs12HourClock("24:00"); +const targetOutput6 = "Not a valid time"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); + +const currentOutput7 = formatAs12HourClock("47:00"); +const targetOutput7 = "Not a valid time"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}` +); + +const currentOutput8 = formatAs12HourClock("15:75"); +const targetOutput8 = "Not a valid time"; +console.assert( + currentOutput8 === targetOutput8, + `current output: ${currentOutput8}, target output: ${targetOutput8}` +); + +const currentOutput9 = formatAs12HourClock("18:60"); +const targetOutput9 = "Not a valid time"; +console.assert( + currentOutput9 === targetOutput9, + `current output: ${currentOutput9}, target output: ${targetOutput9}` +); + +const currentOutput10 = formatAs12HourClock("23:59"); +const targetOutput10 = "11:59 pm"; +console.assert( + currentOutput10 === targetOutput10, + `current output: ${currentOutput10}, target output: ${targetOutput10}` +); + +const currentOutput11 = formatAs12HourClock("00:01"); +const targetOutput11 = "12:01 am"; +console.assert( + currentOutput11 === targetOutput11, + `current output: ${currentOutput11}, target output: ${targetOutput11}` +); + +const currentOutput12 = formatAs12HourClock("12:01"); +const targetOutput12 = "12:01 pm"; +console.assert( + currentOutput12 === targetOutput12, + `current output: ${currentOutput12}, target output: ${targetOutput12}` +); + +const currentOutput13 = formatAs12HourClock("13:00"); +const targetOutput13 = "01:00 pm"; +console.assert( + currentOutput13 === targetOutput13, + `current output: ${currentOutput13}, target output: ${targetOutput13}` +); + +const currentOutput14 = formatAs12HourClock("hello"); +const targetOutput14 = "Not a valid time"; +console.assert( + currentOutput14 === targetOutput14, + `current output: ${currentOutput14}, target output: ${targetOutput14}` +); + +const currentOutput15 = formatAs12HourClock("15:ab"); +const targetOutput15 = "Not a valid time"; +console.assert( + currentOutput15 === targetOutput15, + `current output: ${currentOutput15}, target output: ${targetOutput15}` +); + + +const currentOutput16 = formatAs12HourClock("12"); +const targetOutput16 = "Not a valid time"; +console.assert( + currentOutput16 === targetOutput16, + `current output: ${currentOutput16}, target output: ${targetOutput16}` +); + +const currentOutput17 = formatAs12HourClock("12:30abc"); +const targetOutput17 = "Not a valid time"; +console.assert( + currentOutput17 === targetOutput17, + `current output: ${currentOutput17}, target output: ${targetOutput17}` +); + +const currentOutput18 = formatAs12HourClock("1.30"); +const targetOutput18 = "01:30 am"; +console.assert( + currentOutput18 === targetOutput18, + `current output: ${currentOutput18}, target output: ${targetOutput18}` +); + +const currentOutput19 = formatAs12HourClock("15.30"); +const targetOutput19 = "03:30 pm"; +console.assert( + currentOutput19 === targetOutput19, + `current output: ${currentOutput19}, target output: ${targetOutput19}` +); + +const currentOutput20 = formatAs12HourClock("01.30"); +const targetOutput20 = "01:30 am"; +console.assert( + currentOutput20 === targetOutput20, + `current output: ${currentOutput20}, target output: ${targetOutput20}` +); + +const currentOutput21 = formatAs12HourClock("12.30:45"); +const targetOutput21 = "Not a valid time"; +console.assert( + currentOutput21 === targetOutput21, + `current output: ${currentOutput21}, target output: ${targetOutput21}` +); + + +const currentOutput22 = formatAs12HourClock("12::30"); +const targetOutput22 = "Not a valid time"; +console.assert( + currentOutput22 === targetOutput22, + `current output: ${currentOutput22}, target output: ${targetOutput22}` +); + +const currentOutput23 = formatAs12HourClock("12..30"); +const targetOutput23 = "Not a valid time"; +console.assert( + currentOutput23 === targetOutput23, + `current output: ${currentOutput23}, target output: ${targetOutput23}` +); + +const currentOutput24 = formatAs12HourClock("12:"); +const targetOutput24 = "Not a valid time"; +console.assert( + currentOutput24 === targetOutput24, + `current output: ${currentOutput24}, target output: ${targetOutput24}` +); + +const currentOutput25 = formatAs12HourClock("12."); +const targetOutput25 = "Not a valid time"; +console.assert( + currentOutput25 === targetOutput25, + `current output: ${currentOutput25}, target output: ${targetOutput25}` +); + +const currentOutput26 = formatAs12HourClock(":30"); +const targetOutput26 = "Not a valid time"; +console.assert( + currentOutput26 === targetOutput26, + `current output: ${currentOutput26}, target output: ${targetOutput26}` +); + +const currentOutput27 = formatAs12HourClock("."); +const targetOutput27 = "Not a valid time"; +console.assert( + currentOutput27 === targetOutput27, + `current output: ${currentOutput27}, target output: ${targetOutput27}` +); + +const currentOutput28 = formatAs12HourClock("15:-30"); +const targetOutput28 = "Not a valid time"; +console.assert( + currentOutput28 === targetOutput28, + `current output: ${currentOutput28}, target output: ${targetOutput28}` +); + +const currentOutput29 = formatAs12HourClock("-15:30"); +const targetOutput29 = "Not a valid time"; +console.assert( + currentOutput29 === targetOutput29, + `current output: ${currentOutput29}, target output: ${targetOutput29}` +); + + From ab74a0167e30c958f7996efec2196dcd2d76edb0 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Mon, 28 Sep 2026 23:00:35 +0100 Subject: [PATCH 12/17] Fix formatting --- Sprint-3/5-stretch-extend/format-time.js | 137 ++++++++++++----------- 1 file changed, 70 insertions(+), 67 deletions(-) diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 74be42a23..39e2c10ec 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -1,65 +1,72 @@ // This is the latest solution to the problem from the prep. // Make sure to do the prep before you do the coursework // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. -function pad(num){ +function pad(num) { let stringNum = num.toString(); - if (stringNum.length < 2){ - stringNum = "0" + stringNum; - return stringNum; - } - else{ + if (stringNum.length < 2) { + stringNum = "0" + stringNum; + return stringNum; + } else { return stringNum; } } function formatAs12HourClock(time) { - if((time.indexOf(":") === -1 && time.indexOf(".") === -1) || (time.indexOf(":") !== -1 && time.indexOf(".") !== -1)){ - return "Not a valid time" // check if there is only either one : or . + if ( + (time.indexOf(":") === -1 && time.indexOf(".") === -1) || + (time.indexOf(":") !== -1 && time.indexOf(".") !== -1) + ) { + return "Not a valid time"; // check if there is only either one : or . } - if(time.indexOf(":") === -1){ - if(time.indexOf(".") !== time.lastIndexOf(".")) - { - return "Not a valid time" //check there is only one . + if (time.indexOf(":") === -1) { + if (time.indexOf(".") !== time.lastIndexOf(".")) { + return "Not a valid time"; //check there is only one . } } - if(time.indexOf(".") === -1){ - if(time.indexOf(":") !== time.lastIndexOf(":")) - { - return "Not a valid time" //check if there is only one : + if (time.indexOf(".") === -1) { + if (time.indexOf(":") !== time.lastIndexOf(":")) { + return "Not a valid time"; //check if there is only one : } - } - + let colonPeriodIndex = 0; - if(time.indexOf(":") === -1){ - colonPeriodIndex = time.indexOf(".") - } - else - { - colonPeriodIndex = time.indexOf(":") + if (time.indexOf(":") === -1) { + colonPeriodIndex = time.indexOf("."); + } else { + colonPeriodIndex = time.indexOf(":"); } - if(time.slice(0, colonPeriodIndex) === "" || time.slice(colonPeriodIndex+1) === ""){ - return "Not a valid time" //check if there is a number before and after time separator + if ( + time.slice(0, colonPeriodIndex) === "" || + time.slice(colonPeriodIndex + 1) === "" + ) { + return "Not a valid time"; //check if there is a number before and after time separator } const hours = Number(time.slice(0, colonPeriodIndex)); - const minutes = Number(time.slice(colonPeriodIndex+1)); + const minutes = Number(time.slice(colonPeriodIndex + 1)); const stringMinutes = pad(minutes); const stringHours = pad(hours); - - if (isNaN(hours) || isNaN(minutes) || hours >= 24 || hours < 0 || minutes >= 60 || minutes < 0){ - return "Not a valid time"; - } //check if numbers are valid - if (hours === 12){ + + if ( + isNaN(hours) || + isNaN(minutes) || + hours >= 24 || + hours < 0 || + minutes >= 60 || + minutes < 0 + ) { + return "Not a valid time"; + } //check if numbers are valid + if (hours === 12) { return `${stringHours}:${stringMinutes} pm`; } - if (hours === 0){ + if (hours === 0) { return `12:${stringMinutes} am`; } if (hours > 12) { return `${pad(hours - 12)}:${stringMinutes} pm`; } - + return `${stringHours}:${stringMinutes} am`; } @@ -67,205 +74,201 @@ const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + `current output: ${currentOutput}, target output: ${targetOutput}`, ); const currentOutput2 = formatAs12HourClock("23:00"); const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + `current output: ${currentOutput2}, target output: ${targetOutput2}`, ); const currentOutput3 = formatAs12HourClock("23:46"); const targetOutput3 = "11:46 pm"; console.assert( currentOutput3 === targetOutput3, - `current output: ${currentOutput3}, target output: ${targetOutput3}` + `current output: ${currentOutput3}, target output: ${targetOutput3}`, ); const currentOutput4 = formatAs12HourClock("12:00"); const targetOutput4 = "12:00 pm"; console.assert( currentOutput4 === targetOutput4, - `current output: ${currentOutput4}, target output: ${targetOutput4}` + `current output: ${currentOutput4}, target output: ${targetOutput4}`, ); const currentOutput5 = formatAs12HourClock("00:00"); const targetOutput5 = "12:00 am"; console.assert( currentOutput5 === targetOutput5, - `current output: ${currentOutput5}, target output: ${targetOutput5}` + `current output: ${currentOutput5}, target output: ${targetOutput5}`, ); const currentOutput6 = formatAs12HourClock("24:00"); const targetOutput6 = "Not a valid time"; console.assert( currentOutput6 === targetOutput6, - `current output: ${currentOutput6}, target output: ${targetOutput6}` + `current output: ${currentOutput6}, target output: ${targetOutput6}`, ); const currentOutput7 = formatAs12HourClock("47:00"); const targetOutput7 = "Not a valid time"; console.assert( currentOutput7 === targetOutput7, - `current output: ${currentOutput7}, target output: ${targetOutput7}` + `current output: ${currentOutput7}, target output: ${targetOutput7}`, ); const currentOutput8 = formatAs12HourClock("15:75"); const targetOutput8 = "Not a valid time"; console.assert( currentOutput8 === targetOutput8, - `current output: ${currentOutput8}, target output: ${targetOutput8}` + `current output: ${currentOutput8}, target output: ${targetOutput8}`, ); const currentOutput9 = formatAs12HourClock("18:60"); const targetOutput9 = "Not a valid time"; console.assert( currentOutput9 === targetOutput9, - `current output: ${currentOutput9}, target output: ${targetOutput9}` + `current output: ${currentOutput9}, target output: ${targetOutput9}`, ); const currentOutput10 = formatAs12HourClock("23:59"); const targetOutput10 = "11:59 pm"; console.assert( currentOutput10 === targetOutput10, - `current output: ${currentOutput10}, target output: ${targetOutput10}` + `current output: ${currentOutput10}, target output: ${targetOutput10}`, ); const currentOutput11 = formatAs12HourClock("00:01"); const targetOutput11 = "12:01 am"; console.assert( currentOutput11 === targetOutput11, - `current output: ${currentOutput11}, target output: ${targetOutput11}` + `current output: ${currentOutput11}, target output: ${targetOutput11}`, ); const currentOutput12 = formatAs12HourClock("12:01"); const targetOutput12 = "12:01 pm"; console.assert( currentOutput12 === targetOutput12, - `current output: ${currentOutput12}, target output: ${targetOutput12}` + `current output: ${currentOutput12}, target output: ${targetOutput12}`, ); const currentOutput13 = formatAs12HourClock("13:00"); const targetOutput13 = "01:00 pm"; console.assert( currentOutput13 === targetOutput13, - `current output: ${currentOutput13}, target output: ${targetOutput13}` + `current output: ${currentOutput13}, target output: ${targetOutput13}`, ); const currentOutput14 = formatAs12HourClock("hello"); const targetOutput14 = "Not a valid time"; console.assert( currentOutput14 === targetOutput14, - `current output: ${currentOutput14}, target output: ${targetOutput14}` + `current output: ${currentOutput14}, target output: ${targetOutput14}`, ); const currentOutput15 = formatAs12HourClock("15:ab"); const targetOutput15 = "Not a valid time"; console.assert( currentOutput15 === targetOutput15, - `current output: ${currentOutput15}, target output: ${targetOutput15}` + `current output: ${currentOutput15}, target output: ${targetOutput15}`, ); - const currentOutput16 = formatAs12HourClock("12"); const targetOutput16 = "Not a valid time"; console.assert( currentOutput16 === targetOutput16, - `current output: ${currentOutput16}, target output: ${targetOutput16}` + `current output: ${currentOutput16}, target output: ${targetOutput16}`, ); const currentOutput17 = formatAs12HourClock("12:30abc"); const targetOutput17 = "Not a valid time"; console.assert( currentOutput17 === targetOutput17, - `current output: ${currentOutput17}, target output: ${targetOutput17}` + `current output: ${currentOutput17}, target output: ${targetOutput17}`, ); const currentOutput18 = formatAs12HourClock("1.30"); const targetOutput18 = "01:30 am"; console.assert( currentOutput18 === targetOutput18, - `current output: ${currentOutput18}, target output: ${targetOutput18}` + `current output: ${currentOutput18}, target output: ${targetOutput18}`, ); const currentOutput19 = formatAs12HourClock("15.30"); const targetOutput19 = "03:30 pm"; console.assert( currentOutput19 === targetOutput19, - `current output: ${currentOutput19}, target output: ${targetOutput19}` + `current output: ${currentOutput19}, target output: ${targetOutput19}`, ); const currentOutput20 = formatAs12HourClock("01.30"); const targetOutput20 = "01:30 am"; console.assert( currentOutput20 === targetOutput20, - `current output: ${currentOutput20}, target output: ${targetOutput20}` + `current output: ${currentOutput20}, target output: ${targetOutput20}`, ); const currentOutput21 = formatAs12HourClock("12.30:45"); const targetOutput21 = "Not a valid time"; console.assert( currentOutput21 === targetOutput21, - `current output: ${currentOutput21}, target output: ${targetOutput21}` + `current output: ${currentOutput21}, target output: ${targetOutput21}`, ); - const currentOutput22 = formatAs12HourClock("12::30"); const targetOutput22 = "Not a valid time"; console.assert( currentOutput22 === targetOutput22, - `current output: ${currentOutput22}, target output: ${targetOutput22}` + `current output: ${currentOutput22}, target output: ${targetOutput22}`, ); const currentOutput23 = formatAs12HourClock("12..30"); const targetOutput23 = "Not a valid time"; console.assert( currentOutput23 === targetOutput23, - `current output: ${currentOutput23}, target output: ${targetOutput23}` + `current output: ${currentOutput23}, target output: ${targetOutput23}`, ); const currentOutput24 = formatAs12HourClock("12:"); const targetOutput24 = "Not a valid time"; console.assert( currentOutput24 === targetOutput24, - `current output: ${currentOutput24}, target output: ${targetOutput24}` + `current output: ${currentOutput24}, target output: ${targetOutput24}`, ); const currentOutput25 = formatAs12HourClock("12."); const targetOutput25 = "Not a valid time"; console.assert( currentOutput25 === targetOutput25, - `current output: ${currentOutput25}, target output: ${targetOutput25}` + `current output: ${currentOutput25}, target output: ${targetOutput25}`, ); const currentOutput26 = formatAs12HourClock(":30"); const targetOutput26 = "Not a valid time"; console.assert( currentOutput26 === targetOutput26, - `current output: ${currentOutput26}, target output: ${targetOutput26}` + `current output: ${currentOutput26}, target output: ${targetOutput26}`, ); const currentOutput27 = formatAs12HourClock("."); const targetOutput27 = "Not a valid time"; console.assert( currentOutput27 === targetOutput27, - `current output: ${currentOutput27}, target output: ${targetOutput27}` + `current output: ${currentOutput27}, target output: ${targetOutput27}`, ); const currentOutput28 = formatAs12HourClock("15:-30"); const targetOutput28 = "Not a valid time"; console.assert( currentOutput28 === targetOutput28, - `current output: ${currentOutput28}, target output: ${targetOutput28}` + `current output: ${currentOutput28}, target output: ${targetOutput28}`, ); const currentOutput29 = formatAs12HourClock("-15:30"); const targetOutput29 = "Not a valid time"; console.assert( currentOutput29 === targetOutput29, - `current output: ${currentOutput29}, target output: ${targetOutput29}` + `current output: ${currentOutput29}, target output: ${targetOutput29}`, ); - - From d27c7d8d7c89afcd8516a837de3aa4328c53dabb Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 1 Oct 2026 14:43:24 +0100 Subject: [PATCH 13/17] Add variable capitaliseStr instead of changing original string --- Sprint-3/1-key-errors/0.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 0bb43e817..1327a4414 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -17,6 +17,6 @@ // =============> write your new code here function capitalise(str) { - str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + const capitaliseStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitaliseStr; } From 508e4667d009b5df08c11984940c613c2d5dddd7 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 1 Oct 2026 14:46:21 +0100 Subject: [PATCH 14/17] Remove default parameter from square function --- Sprint-3/1-key-errors/2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index fd9218524..1438d2a51 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -21,6 +21,6 @@ // =============> write your new code here -function square(num = 3) { +function square(num) { return num * num; } From 92cdff7569a0b0221fdc573076c084d89dfbda7a Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 1 Oct 2026 16:23:43 +0100 Subject: [PATCH 15/17] Made fix to catch cases of empty strings, non-string inputs, check for integers --- Sprint-3/5-stretch-extend/format-time.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 39e2c10ec..436cc0a46 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -10,7 +10,10 @@ function pad(num) { return stringNum; } } -function formatAs12HourClock(time) { +function formatAs12HourClock(time = "") { + if (typeof time != "string") { + return "Not a valid format"; //check if time is a string + } if ( (time.indexOf(":") === -1 && time.indexOf(".") === -1) || (time.indexOf(":") !== -1 && time.indexOf(".") !== -1) @@ -53,7 +56,9 @@ function formatAs12HourClock(time) { hours >= 24 || hours < 0 || minutes >= 60 || - minutes < 0 + minutes < 0 || + hours % 1 != 0 || + minutes % 1 != 0 ) { return "Not a valid time"; } //check if numbers are valid From ad83dc00d3fcee371a894ef3b2f1c89d5f939b2a Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Fri, 2 Oct 2026 17:56:57 +0100 Subject: [PATCH 16/17] Fix toPounds function to return string template --- Sprint-3/3-mandatory-implement/3-to-pounds.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 3f42bd0bb..d6f1144bd 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -21,7 +21,7 @@ function toPounds(penceString) { .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); - console.log(`£${pounds}.${pence}`); + return(`£${pounds}.${pence}`); } toPounds("10p"); From d64375201874f761bb6236a5132f1a435bbd024e Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Fri, 2 Oct 2026 18:16:06 +0100 Subject: [PATCH 17/17] Add validation check for whitespace in input string --- Sprint-3/5-stretch-extend/format-time.js | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 436cc0a46..5cf4f51e2 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -14,6 +14,11 @@ function formatAs12HourClock(time = "") { if (typeof time != "string") { return "Not a valid format"; //check if time is a string } + + if (/\s/.test(time)) { + return "Not a valid time"; //check for whitespace in string + } + if ( (time.indexOf(":") === -1 && time.indexOf(".") === -1) || (time.indexOf(":") !== -1 && time.indexOf(".") !== -1) @@ -277,3 +282,24 @@ console.assert( currentOutput29 === targetOutput29, `current output: ${currentOutput29}, target output: ${targetOutput29}`, ); + +const currentOutput30 = formatAs12HourClock(" :30"); +const targetOutput30 = "Not a valid time"; +console.assert( + currentOutput30 === targetOutput30, + `current output: ${currentOutput30}, target output: ${targetOutput30}`, +); + +const currentOutput31 = formatAs12HourClock("12:30 "); +const targetOutput31 = "Not a valid time"; +console.assert( + currentOutput31 === targetOutput31, + `current output: ${currentOutput31}, target output: ${targetOutput31}`, +); + +const currentOutput32 = formatAs12HourClock("12:30\n"); +const targetOutput32 = "Not a valid time"; +console.assert( + currentOutput32 === targetOutput32, + `current output: ${currentOutput32}, target output: ${targetOutput32}`, +);