From 26fd354594aa86ffba6d747c0c662e1992d3310e Mon Sep 17 00:00:00 2001 From: asaniDev Date: Tue, 14 Oct 2025 00:23:08 +0100 Subject: [PATCH 01/10] changed the name of the string in the function --- Sprint-2/1-key-errors/0.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..d781212bd 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,23 @@ // Predict and explain first... // =============> write your prediction here +//we should have an error showing that str already exists so we cannot declare it again 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; -} +//function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +//} + +//console.log(capitalise("cat")); // =============> write your explanation here +//str has been passed down as a parameter so we cannot declare it again. we would need to declare a different variable name. // =============> write your new code here +function capitalise(str) { + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; +} + +console.log(capitalise("cat")); From 5ab6322f2f182e62d95b8e084f24263950904809 Mon Sep 17 00:00:00 2001 From: asaniDev Date: Tue, 14 Oct 2025 00:42:55 +0100 Subject: [PATCH 02/10] removed line 10 and passed function to console --- Sprint-2/1-key-errors/1.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..23e2adc75 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,21 @@ // Why will an error occur when this program runs? // =============> write your prediction here +//decimalNumber does not exist outside the scope of the convertToPercentage funtion so the log will throw an error saying it has not been declared. // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; + //const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(0.5)); // =============> write your explanation here - +//decimalNumber is passed as a parameter when the function is called, line 10 tries to declare it again and assign it a new fixed value. we would need to remove that line so we can use the value passed in with the function call. +//decimalNumber only exists inside the function scope so console.log cannot access it an error will be thrown. we need to instead pass the function to console.log. // Finally, correct the code to fix the problem // =============> write your new code here From 0c7cc6f534f6b9659cdc8692b2a9ca2ac75375fc Mon Sep 17 00:00:00 2001 From: asaniDev Date: Tue, 14 Oct 2025 21:45:58 +0100 Subject: [PATCH 03/10] corrected the error with the function --- Sprint-2/1-key-errors/2.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..d793aa81e 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,23 @@ - // 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 +//it will throw an error for use of a primitive value instead of a variable. -function square(3) { - return num * num; -} +//function square(3) { +// return num * num; +//} -// =============> write the error message here +// =============> write the error message here, SyntaxError: Unexpected number // =============> explain this error message here - +//this error message is becasue we cannot pass a direct value to a function when creating it but rather when we call it. Instead we need to give it a parameter that can then hold the value that we want to pass to the function. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} - +console.log(square(3)); From cc216e17bebb1f4bbc760a6c44c64d1b718541cb Mon Sep 17 00:00:00 2001 From: asaniDev Date: Tue, 14 Oct 2025 22:02:03 +0100 Subject: [PATCH 04/10] added a return to the function --- Sprint-2/2-mandatory-debug/0.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..eb7a99587 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,21 @@ // Predict and explain first... // =============> write your prediction here +//line 7 will log the result of a * b, line 10 will throw an error since the function does not return anything so it will say The result of multiplying 10 and 32 is 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 +//since the function does not return a value we get undefined passed back to the console. // 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 90013dcd67549cce10bb5bf03842f58ad94b0e3f Mon Sep 17 00:00:00 2001 From: asaniDev Date: Tue, 14 Oct 2025 22:14:44 +0100 Subject: [PATCH 05/10] moved the return command to the line below it --- Sprint-2/2-mandatory-debug/1.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..cacd9c69a 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,20 @@ // Predict and explain first... // =============> write your prediction here +// the console will show The sum of 10 and 32 is undefined, this is because the return command is before the calculation so the return has no value to bring back. -function sum(a, b) { - return; - a + b; -} +//function sum(a, b) { +// return; +// a + b; +//} -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +//console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +//the console will show The sum of 10 and 32 is undefined, this is because the return command is before the calculation so the return has no value to bring back. // 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 f63f7cfcae2acf75994b43ab9d7e857dc2171ac3 Mon Sep 17 00:00:00 2001 From: asaniDev Date: Tue, 14 Oct 2025 22:53:32 +0100 Subject: [PATCH 06/10] created a function that returns the bmi --- Sprint-2/3-mandatory-implement/1-bmi.js | 13 +++++++++++-- 1 file changed, 11 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..21e126009 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,14 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + // return the BMI of someone based off their weight and height + const bmi = weight / height ** 2; + return bmi.toFixed(1); +} + +console.log(`Your BMI is ${calculateBMI(70, 1.73)}`); + +//const actualOutput = calculateBMI(70, 1.73); +//const targetOutput = "23.4"; + +//console.assert(actualOutput === targetOutput, `That is not the correct BMI`); From a02b0c5ee28875c10e08092931238409214ba77c Mon Sep 17 00:00:00 2001 From: asaniDev Date: Tue, 14 Oct 2025 23:06:15 +0100 Subject: [PATCH 07/10] created a function that converts a given string to snake case --- Sprint-2/3-mandatory-implement/2-cases.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..be2c48771 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,9 @@ // 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 convertToSnakeCase(inputStr) { + const newStr = inputStr.toUpperCase().replaceAll(" ", "_"); + return newStr; +} + +console.log(convertToSnakeCase("hello there")); From e0973162482356b438a581f4f986803ef7987470 Mon Sep 17 00:00:00 2001 From: asaniDev Date: Thu, 16 Oct 2025 14:44:44 +0100 Subject: [PATCH 08/10] created a toPounds function to return a pounds and pence notation --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..c1d3009d7 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,48 @@ // 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"); + + return `£${pounds}.${pence}`; +} + +console.log(toPounds("399p")); + +const actualOutput = toPounds("4685p"); +const expectedOutput = "£46.85"; + +console.assert( + actualOutput === expectedOutput, + `expected to get ${expectedOutput}, but got ${actualOutput}` +); + +const actualOutput1 = toPounds("123456p"); +const expectedOutput1 = "£1234.56"; + +console.assert( + actualOutput1 === expectedOutput1, + `expected to get ${expectedOutput1}, but got ${actualOutput1}` +); + +const actualOutput2 = toPounds("4p"); +const expectedOutput2 = "£0.04"; + +console.assert( + actualOutput2 === expectedOutput2, + `expected to get ${expectedOutput2}, but got ${actualOutput2}` +); From 742c228a82e4fca6000fdaee1b471a57597b9718 Mon Sep 17 00:00:00 2001 From: asaniDev Date: Thu, 16 Oct 2025 15:15:19 +0100 Subject: [PATCH 09/10] answered all the questions in the exercise --- Sprint-2/4-mandatory-interpret/time-format.js | 12 +++++++----- 1 file changed, 7 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..db27f6476 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,24 +11,26 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(61)); + // 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 it will be called 3 times // Call formatTimeDisplay with an input of 61, now answer the following: // b) What is the value assigned to num when pad is called for the first time? -// =============> write your answer here +// =============> write your answer here num = 0 on the first call // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here return value = "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 +// =============> write your answer here num = 1 when called for the last time because remainingSeconds has a value of 1 from 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 +// =============> write your answer here return value will be "01" since num has 1 digit pad adds a 0 to the start to make it 2 digits. From 0d5ad45ee9c0643b416a62d1cf1bf385b41ab13e Mon Sep 17 00:00:00 2001 From: asaniDev Date: Fri, 17 Oct 2025 02:05:59 +0100 Subject: [PATCH 10/10] wrote tests for any edge cases and fixed resulting bugs --- Sprint-2/5-stretch-extend/format-time.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..251f6d4f7 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,8 +4,14 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3); + console.log(minutes); + if (hours == 12) { + return `${hours}:${minutes} pm`; + } + if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}:${minutes} pm`; } return `${time} am`; } @@ -23,3 +29,17 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("23:30"); +const targetOutput3 = "11:30 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("12:01"); +const targetOutput4 = "12:01 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +);