From 4ebe3541bd66942b4ecf697e75d87e0ee6b64e8c Mon Sep 17 00:00:00 2001 From: Xinxi Date: Sat, 13 Dec 2025 18:30:08 +0100 Subject: [PATCH 1/5] 1-key-errors completed --- Sprint-2/1-key-errors/0.js | 7 +++++++ Sprint-2/1-key-errors/1.js | 14 +++++++++++++- Sprint-2/1-key-errors/2.js | 11 ++++++++--- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..7aa761d84 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,7 @@ // Predict and explain first... // =============> write your prediction here +There is a syntax error. Str is a parameter, then it has declared again as let str =. +Identifier 'str' has already been declared. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +12,9 @@ function capitalise(str) { } // =============> write your explanation here +// // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..b19891412 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,6 +2,7 @@ // Why will an error occur when this program runs? // =============> write your prediction here +//syntax error, decimalNumber has been declared as the parameter, then it to be declared again as const decimalNumber. // Try playing computer with the example to work out what is going on @@ -11,10 +12,21 @@ function convertToPercentage(decimalNumber) { return percentage; } - console.log(decimalNumber); + + // =============> write your explanation here +//Since the decimalNumber is a parameter, it's not neccesary to declare it again as const and assign it a value. +//Only need to assign the specific decimalNumber in console.log with the function name and delete the redeclaration of const decimalNumber = 0.5. // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(1)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..dcefdc1be 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,22 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +The parameter 3 is an error. + function square(3) { return num * num; } // =============> write the error message here - +function square(3) // =============> explain this error message here - +The parameter should 2 is a problem. It should be num, otherwise there is no declaration of num. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(3)); From 9ba26e5d9880c276419135bc553c7f931b679b87 Mon Sep 17 00:00:00 2001 From: Xinxi Date: Sat, 13 Dec 2025 19:13:34 +0100 Subject: [PATCH 2/5] 2-mandatory-debug completed --- Sprint-2/2-mandatory-debug/0.js | 12 +++++++++--- Sprint-2/2-mandatory-debug/1.js | 8 +++++++- Sprint-2/2-mandatory-debug/2.js | 16 +++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..ca0063046 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,20 @@ // Predict and explain first... // =============> write your prediction here - +//Probably the function is not defined. 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 doesn't return to any result. It's only to print the multiply of the parameters. // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..00ab9b066 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,6 @@ // Predict and explain first... // =============> write your prediction here - +//It's undefined. function sum(a, b) { return; a + b; @@ -9,5 +9,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +//It returns nothing, and the expression a + b is incomplete. // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..043760976 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,7 @@ // Predict the output of the following code: // =============> Write your prediction here - +The results are all 3. const num = 103; function getLastDigit() { @@ -15,10 +15,24 @@ 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 +Because num has been declared as a globle const variable which is 103, then the num value can't be changed. There is no parameter as well. // =============> write your explanation here +Since it's a const globle variable, the num's value is alway 103, that's why no matter what's the value type in console.log the result is always the same. // 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 2834f5d3119d4a6bcb6ded102b8e64e41ae39d85 Mon Sep 17 00:00:00 2001 From: Xinxi Date: Sun, 14 Dec 2025 09:27:50 +0100 Subject: [PATCH 3/5] 3-mandatory-implement completed --- Sprint-2/3-mandatory-implement/1-bmi.js | 5 ++++- Sprint-2/3-mandatory-implement/2-cases.js | 4 ++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 13 +++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..ce40770cd 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,7 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + const bmi = weight / (height * height); + return bmi.toFixed(1); +} +console.log(calculateBMI(70, 1.73)); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..9bc939cbb 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,7 @@ // 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(str) { + return str.replace(/([a-z])([A-Z])/g,'$1_$2').toUpperCase(); +} +console.log(toUpperSnakeCase("Lord Of The Rings")); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..92a7d94b0 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,16 @@ // 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); + console.log(`£${pounds}.${pence}`); +} +toPounds("399p"); +toPounds("99p"); +toPounds("3p"); +toPounds("1399p"); \ No newline at end of file From 9992c59395c9457796db5483affc2f27a2f5371c Mon Sep 17 00:00:00 2001 From: Xinxi Date: Sun, 14 Dec 2025 10:58:42 +0100 Subject: [PATCH 4/5] 4-mandatory-intepret completed --- Sprint-2/4-mandatory-interpret/time-format.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..7085f923a 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,6 +10,7 @@ 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 @@ -18,6 +19,7 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// 3 times. First, pad(totalHours); Second, pad(remainingMinutes); Third, pad(remainingSeconds). // Call formatTimeDisplay with an input of 61, now answer the following: @@ -26,9 +28,10 @@ function formatTimeDisplay(seconds) { // c) What is the return value of pad is called for the first time? // =============> write your answer here - + 0 // 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 - + 00, because after the function formatTimeDisplay(61) execute its argorithm, the hour value is 0, then function pad(num) converts all the num to 2 digit format. // 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, because after the function formatTimeDisplay(61) execute its argorithm, the hour value is 1, then function pad(num) converts all the num to 2 digit format, thus why it's 01. \ No newline at end of file From eaf2c872d9691fdb815096f676c0ad58499a440e Mon Sep 17 00:00:00 2001 From: Xinxi Date: Sun, 14 Dec 2025 11:56:43 +0100 Subject: [PATCH 5/5] 5-stretch-extend refactored --- Sprint-2/5-stretch-extend/format-time.js | 26 ++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..3182ac547 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,22 +3,32 @@ // 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 formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + let hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + let ampm = "am"; + + if (hours === 0) { + hours = 12; + ampm = "am"; + } + else if (hours >= 12) { + ampm = "pm"; + if (hours > 12) { + hours -= 12; + } } - return `${time} am`; + return `${hours}:${minutes} ${ampm}`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput = formatAs12HourClock("00:00"); +const targetOutput = "12:00 am"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +const currentOutput2 = formatAs12HourClock("24:00"); +const targetOutput2 = "12:00 pm"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}`