From 0e93b520b21c73aa759c978e73371553f1508209 Mon Sep 17 00:00:00 2001 From: pathywang Date: Tue, 1 Jul 2025 17:47:28 +0100 Subject: [PATCH 01/12] error correction --- Sprint-2/1-key-errors/0.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..0dbc9cfa5 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,13 @@ -// Predict and explain first... -// =============> write your prediction here +/"hello Mhairi" === `hello ${mhairiName}`; +"${mhairiName} is 28" === `Mhairi is ${mhairiAge}`; -// call the function capitalise with a string input -// interpret the error message and figure out why an error is occurring +// My answer is: +const mhairiName = "Mhairi"; +const mhairiAge = 28; -function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; -} +const sentence1= `hello ${mhairiName}`; +console.log(sentence1); // ➜ Output: hello Mhairi -// =============> write your explanation here -// =============> write your new code here + +const sentence= `${mhairiName} is ${mhairiAge}`; +console.log(sentence); // ➜ Output: Mhairi is 28 From 202d64e7e2e2c4a8f187dadf292aab03f389594b Mon Sep 17 00:00:00 2001 From: pathywang Date: Tue, 1 Jul 2025 18:09:49 +0100 Subject: [PATCH 02/12] error correction --- Sprint-2/1-key-errors/1.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..b0e90042e 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -14,7 +14,14 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// =============>const decimalNumber =0.5 is wrong because we can not redeclare parameter using const inside function. +// what is more, decimalNumber is not defined in thed global scope so we can not use console.log(decimalNumber) + // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> +function convertToPercentage(decimalNumber) {const percentage = `${decimalNumber*100}%`;return percentage;} +const decimalNumber = 0.5 + +console.log(convertToPercentage(decimalNumber)); +console.log(decimalNumber); \ No newline at end of file From 5d023b1c8562b21ecde26ca53c6f29ace4571c4b Mon Sep 17 00:00:00 2001 From: pathywang Date: Tue, 1 Jul 2025 18:19:33 +0100 Subject: [PATCH 03/12] error correction --- Sprint-2/1-key-errors/2.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..54df9fc3e 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,22 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> wrong syntax the number 3 should not be in function function square(3) { return num * num; } -// =============> write the error message here +// =============> syntaxError: Unexpected number -// =============> explain this error message here +// =============> when defining a function, the parentheses must contain parameter names, not values. +// function square(3) is invalid because 3 is a value, not a parameter name. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> +function square(num){return num*num}; + +console.log (square(3)); From 43cff66cd54c7b4213a903659dec636a5ad091e3 Mon Sep 17 00:00:00 2001 From: pathywang Date: Tue, 1 Jul 2025 18:35:00 +0100 Subject: [PATCH 04/12] explain error --- Sprint-2/2-mandatory-debug/0.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..ee79ade10 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> console.log(a*b) is inside the function, so it does not return anything instead return undefined by default function multiply(a, b) { console.log(a * b); @@ -8,7 +8,16 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> When we call multiply(10, 32) inside the template string, it first runs console.log(320) inside the function. +// Then it tries to insert the function’s return value (which is undefined) into the string. +// So, we’ll see:csharp +// Copy +// Edit +// 320 +// The result of multiplying 10 and 32 is undefined // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> my new code: +function multiply(a,b) {return(a*b)}; + +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); From b52edc9e8dd5fb2bba645b5eca7243a987571c54 Mon Sep 17 00:00:00 2001 From: pathywang Date: Tue, 1 Jul 2025 18:44:35 +0100 Subject: [PATCH 05/12] spot error --- Sprint-2/2-mandatory-debug/1.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..e24bd5bbb 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> it should be{return a+b;}; rather than have semicolon between return and a+b function sum(a, b) { return; @@ -8,6 +8,17 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> The return; statement immediately exits the function and returns undefined. +// The line a + b; after return; never runs — it's unreachable code.So, when we call sum(10, 32), it returns undefined. +// Output we get: +// python +// Copy +// Edit +// The sum of 10 and 32 is undefined // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> my new code: +function sum (a,b) {return a+b;}; + +console.log (`The sum of 10 and 32 is ${sum(10, 32)}`); + +// the output: The sum of 10 and 32 is 42 From 9c517544a8ddc5533b020351583156dd64f8edd6 Mon Sep 17 00:00:00 2001 From: pathywang Date: Tue, 1 Jul 2025 18:58:49 +0100 Subject: [PATCH 06/12] error explaination --- Sprint-2/2-mandatory-debug/2.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..63a83c3b6 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,7 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> get rid of const num = 103 as it is defined in global scope. the output will be always 3. const num = 103; @@ -14,11 +14,27 @@ 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 +//num is always 103 inside the function (from the global variable). +//So, every call to getLastDigit() returns "3" (the last digit of 103). +//The arguments you pass (like 42, 105) are ignored because the function doesn't take parameters. + + // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> my new code: +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)}`); +// The output will be: The last digit of 42 is 2 +// The last digit of 105 is 5 +// The last digit of 806 is 6 // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem From 280323481dc0bf51f20c0d6e579ac03afec92c04 Mon Sep 17 00:00:00 2001 From: pathywang Date: Tue, 1 Jul 2025 22:56:04 +0100 Subject: [PATCH 07/12] complete code --- Sprint-2/3-mandatory-implement/1-bmi.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..4d6db9edb 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,7 @@ // Then when we call this function with the weight and height // 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 +function calculateBMI(weight, height) { const bmi= weight/(height*height); return parseFloat(bmi.toFixed(1));} + + +console.log(calculateBMI(58,1.50)); // output: 25.8 From 934f53b18252823060665aa76e3f96d1afca3e2c Mon Sep 17 00:00:00 2001 From: pathywang Date: Tue, 1 Jul 2025 23:09:47 +0100 Subject: [PATCH 08/12] complete simple code --- Sprint-2/3-mandatory-implement/2-cases.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..5b0d3f90d 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,16 @@ // 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 + +// The function to convert any string to UPPER_SNAKE_CASE: + +function toUpperSnakeCase(input) { + return input.trim().toUpperCase().replace(/\s+/g, "_");} + +console.log(toUpperSnakeCase("hello there")); // output:HELLO_THERE +console.log(toUpperSnakeCase("lord of the rings")); // output: LORD_OF_THE_RINGS + +//trim() removes extra spaces at the beginning and end. +//toUpperCase() makes everything uppercase. +//replace(/\s+/g, "_") replaces all spaces (even multiple) with underscores. + From 1ccd377ffe54011091b67a4241c381ac742e7a5e Mon Sep 17 00:00:00 2001 From: pathywang Date: Tue, 1 Jul 2025 23:20:35 +0100 Subject: [PATCH 09/12] function completion --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 24 +++++++++++++++++++ 1 file changed, 24 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..c71902ae2 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,27 @@ // 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 +// the code is: +function formatPenceToPounds(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(formatPenceToPounds("7p")); // £0.07 +console.log(formatPenceToPounds("67p")); // £0.67 +console.log(formatPenceToPounds("153p")); // £1.53 +console.log(formatPenceToPounds("3p")); // £0.03 \ No newline at end of file From 43f590e43f50bbb18c260093b0f786c0c8455c43 Mon Sep 17 00:00:00 2001 From: pathywang Date: Wed, 2 Jul 2025 11:17:19 +0100 Subject: [PATCH 10/12] question answered --- 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..0716cd876 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 +// =============> pad is called 3 times — once for hours, once for minutes, and once for seconds. + // 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 +// =============> pad is first called for total hourse which is 0 hours // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> the return value is "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 +// =============> pad is called for total seconds at last time which is 1 seconds. // 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 +// =============> the return value is "01".It pads "1" with a leading zero to ensure two digits. + + From 90208f2bec030aa94f39772c8b6f69fc3407aab7 Mon Sep 17 00:00:00 2001 From: pathywang Date: Wed, 2 Jul 2025 20:03:14 +0100 Subject: [PATCH 11/12] further explaination --- Sprint-2/4-mandatory-interpret/time-format.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 0716cd876..4c1177be3 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,21 +17,26 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> pad is called 3 times — once for hours, once for minutes, and once for seconds. +// =============> pad is called 3 times — once each for hours, for minutes andvfor seconds. // 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? -// =============> pad is first called for total hourse which is 0 hours +// =============> The first call is for totalHours, and when you call formatTimeDisplay(61), the value of totalHours is: +//totalHours = (61 - 1) / 60 = 60 / 60 = 1, remainingMinutes = 1 +//totalHours = totalMinutes - remainingMinutes = 1 - 1 = 0 +// So, num = 0 for the first pad call. // c) What is the return value of pad is called for the first time? -// =============> the return value is "00" +// =============> The return value is "00" because pad(0) results in "00" using .padStart(2, "0"). // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> pad is called for total seconds at last time which is 1 seconds. +// =============> The last call is for remaining seconds. With input 61, remainingSeconds = 61 % 60 = 1, so num = 1 in the last call. // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> the return value is "01".It pads "1" with a leading zero to ensure two digits. +// =============> The return value is "01" – pad(1) returns "01" because it adds a leading zero to make it 2 characters. + +// so final output should be "00:01:01" From c60427b1facbf96d0ab37ef5e68139a159358581 Mon Sep 17 00:00:00 2001 From: pathywang Date: Wed, 2 Jul 2025 20:29:10 +0100 Subject: [PATCH 12/12] fix bugs --- Sprint-2/5-stretch-extend/format-time.js | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..527a98d44 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -23,3 +23,41 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +// i put this function for test in console, chrome but it showed undefined then i put 12:00 and the output is:Uncaught SyntaxError: +// Unexpected token.so the function is not workable and has some bugs.Unfortunately, i can not work out by myself, i went to chatGpt which +// explained the detail to me. function format12HourClock(time) does not work right because it does not work for 12:00, 24:00 d not for +// minutes either. The right function should be: + +function formatAs12HourClock(time) { + const hours24 = Number(time.slice(0, 2)); + const minutes = time.slice(3); + + const period = hours24 >= 12 ? "pm" : "am"; + const hours12 = hours24 % 12 === 0 ? 12 : hours24 % 12; + + return `${String(hours12).padStart(2, "0")}:${minutes} ${period}`; +} + +const currentOutput = formatAs12HourClock("08:00"); +const targetOutput = "08:00 am"; +console.assert( + currentOutput === 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}` +); + +// const period = hours24 >= 12 ? "pm" : "am";This line uses a ternary operator — a concise way to write an if...else statement. +// If hours24 is greater than or equal to 12, then set period to "pm".Otherwise, set it to "am". + +// const hours12 = hours24 % 12 === 0 ? 12 : hours24 % 12;This is also a ternary operator (shortcut for if...else) +// used to convert 24-hour time to 12-hour time format, which means:"If hours24 % 12 is 0, then set hours12 to 12, +// otherwise set it to hours24 % 12." For me, i can understand now with chatgpt help. + +