From ab3d1163753b5244976684a02f8413bc39002f06 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Sun, 1 Mar 2026 06:41:59 +0000 Subject: [PATCH 1/7] Sprint 2: complete coursework tasks --- Sprint-2/1-key-errors/0.js | 18 ++++++++- Sprint-2/1-key-errors/1.js | 24 +++++++++++- Sprint-2/1-key-errors/2.js | 24 ++++++++++++ Sprint-2/2-mandatory-debug/0.js | 27 +++++++++++++- Sprint-2/2-mandatory-debug/1.js | 14 ++++++- Sprint-2/2-mandatory-debug/2.js | 37 +++++++++++++++++++ Sprint-2/3-mandatory-implement/1-bmi.js | 8 +++- Sprint-2/3-mandatory-implement/2-cases.js | 10 +++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 19 ++++++++++ Sprint-2/4-mandatory-interpret/time-format.js | 31 ++++++++++------ Sprint-2/5-stretch-extend/format-time.js | 24 +++++++++++- 11 files changed, 217 insertions(+), 19 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..a9416f8005 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,9 @@ -// Predict and explain first... +// Predict and explain first... +//Syntax error // =============> write your prediction here +// We pass str as a parameter to the function. +//The parameter becomes a local variable inside the function scope. +//Therefore, we cannot declare another variable with the same name inside the same scope. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +14,16 @@ function capitalise(str) { } // =============> write your explanation here +// The parameter str is already a local variable inside the function. +// Previously, redeclaring it using let caused an error because variables cannot be declared twice in the same scope. +// Instead of redeclaring it, I modified the existing str variable and returned it. +// This avoids the redeclaration error and the code runs correctly. // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} +let str = capitalise("Arun"); +console.log(str); + + diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..02419d0727 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,6 +1,12 @@ // Predict and explain first... - +// syntax error identifier decimalNumber be already declare +// there is no function call and then you can't use the local variable decimalNumber out of the function // Why will an error occur when this program runs? +//decimalNumber is already declared as a parameter, so declaring it again inside the function causes an error. + +//There is no function call. + +//You cannot use the local variable decimalNumber outside the function scope. // =============> write your prediction here // Try playing computer with the example to work out what is going on @@ -11,10 +17,24 @@ function convertToPercentage(decimalNumber) { return percentage; } - console.log(decimalNumber); // =============> write your explanation here +//The error occurred because a local variable was redeclared inside the function using a variable keyword. Since the parameter already acts as a local variable, redeclaring it caused an error. + +//To fix this, I removed the variable keyword and modified the existing parameter instead. + +//I also removed the unused variable from console.log because it was outside the function scope. +//Finally, I called the function directly inside console.log using convertToPercentage() so the function executes properly and returns the correct value. // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +const decimalNumber = convertToPercentage(0.5) ; +console.log(decimalNumber); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..8878cfae7a 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,44 @@ // Predict and explain first BEFORE you run any code... +// The code has a syntax error because a number is used instead of a parameter name in the function definition. +// Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError. +// If the function is not called, it will not execute. // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// The code has a syntax error because a number is used instead of a parameter name in the function definition. +// Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError. +// If the function is not called, it will not execute. + function square(3) { return num * num; } + + // =============> write the error message here +// syntax error :unexpected number. // =============> explain this error message here +// The error occurs because a number is used in the function definition instead of a parameter name. A function definition must contain a parameter (a variable name), not a value. + +// Inside the function, num is used but it was never declared or passed as a parameter, which causes a ReferenceError because num is not defined in the scope. + +// Also, if the function is not called, it will not execute. // Finally, correct the code to fix the problem +// I corrected the function by properly defining num as a parameter instead of using a number in the function definition. This resolved the syntax error. + +// Inside the function, num is now defined, so the ReferenceError is fixed. +// I then called the function with an argument and stored the returned value in a variable named num. Although the same variable name is used, the function parameter and the outer variable exist in different scopes, so there is no conflict. // =============> write your new code here +function square(num) { + return num * num; +} +let num = square(3); +console.log(num); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..b3c702a94c 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,39 @@ // Predict and explain first... // =============> write your prediction here +// The function multiplies the numbers and prints the answer. + +// But it does not return the answer. + +// Because there is no return, the function automatically gives back undefined. + +// So when we use the function inside the second console.log, the value is undefined. function multiply(a, b) { console.log(a * b); + } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// console.log and return are not the same. + +// console.log only prints the value to the console. + +// It does not send the value back to the function call. + +// Because there was no return statement, the function automatically returned undefined. -// Finally, correct the code to fix the problem +// To fix the issue, I replaced console.log with return. + +// By returning a * b, the function now sends the calculated value back to where it was called. + +// As a result, the correct value is displayed instead of undefined. // =============> 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 37cedfbcfd..273fa2033c 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,9 @@ // Predict and explain first... // =============> write your prediction here - +// The function returns nothing because the return statement has no value. +// Once JavaScript reaches return, the function stops executing. +// Therefore, the line after it is dead code and never runs. +// Since the function does not return a value, it returns undefined, which is why the template string displays undefined. function sum(a, b) { return; a + b; @@ -9,5 +12,14 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The function now correctly returns the result of a + b. +// Since the addition is included in the return statement, the function sends back the calculated value. +// Therefore, when the function is called inside the template string, it displays the correct result, 42. // 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)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..6e910cbe79 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,6 +2,11 @@ // Predict the output of the following code: // =============> Write your prediction here +// The output will be 3 for every function call. +// This is because the function does not have a parameter, so it ignores the values passed in the function calls. +// Instead, it uses the global variable num, which is set to 103. +// The last digit of 103 is 3, so the function always returns 3. +// variable num inside function take the input from the global variable num in the outside of the function const num = 103; @@ -15,10 +20,42 @@ 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 +// The function getLastDigit does not have a parameter. +// Even though values like 42, 105, and 806 are passed when calling the function, they are ignored. + +// Inside the function, the variable num refers to the global variable num, which is set to 103. + +// Each time the function runs, it converts 103 to a string and extracts the last character using slice(-1). + +// The last digit of 103 is "3". + +// Therefore, every function call returns "3". // 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 +// In the previous version, the function was not working properly because it used a global variable instead of a parameter. +// Since the function did not define a parameter, it ignored the values passed during the function calls and always used the same global value. + +// In the corrected version, the global variable was removed and a parameter num was added to the function definition. +// Now, the function receives the argument passed during each function call and correctly returns the last digit of that number. + +// Therefore, the output becomes: + +// The last digit of 42 is 2 +// The last digit of 105 is 5 +// The last digit of 806 is 6 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..e89ed084e9 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,9 @@ // 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 + let bmi = weight / (height * height); + bmi = bmi.toFixed(1); + return Number(bmi); + +} + console.log(calculateBMI(62 , 1.8)); \ 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 5b0ef77ad9..c513adc944 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,13 @@ // 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 upperSnakeCase(str){ + str = str.toUpperCase() + + return str.replaceAll(" " , "_"); +; + +} +console.log(upperSnakeCase("hello there")); +console.log(upperSnakeCase("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 6265a1a703..734364a277 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,22 @@ // 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")); + console.log(toPounds("10p")); + console.log(toPounds("5p")); \ No newline at end of file diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8c..cff5d8e63f 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -8,27 +8,36 @@ function formatTimeDisplay(seconds) { const remainingMinutes = totalMinutes % 60; const totalHours = (totalMinutes - remainingMinutes) / 60; - return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; + return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } -// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit +console.log(formatTimeDisplay(61)) + +// You will need to play computer with this example - use the Python Visualizer 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 - -// Call formatTimeDisplay with an input of 61, now answer the following: +// ans: 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 - -// c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here : 0 +// c) What is the return value of pad is called for the first time? +// =============> write your answer here 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 : 1 When formatTimeDisplay(61) is called, the value 61 is passed as the argument to seconds. +// Then remainingSeconds is calculated using seconds % 60, which gives 1. +// The last call to pad is pad(remainingSeconds). +// Since remainingSeconds is 1, the value assigned to num in the last call is 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 : 01 When formatTimeDisplay(61) is called, the value 1 is passed to pad during the last call because remainingSeconds equals 1. +// Inside the pad function: +// The number 1 is converted to a string. +// padStart(2, "0") ensures the string has at least two characters. +// Since "1" has only one character, a "0" is added to the beginning. +// So "1" becomes "01". +// This ensures the time format follows the 00:00:00 structure. \ No newline at end of file diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..34d02fad82 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -5,7 +5,8 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); if (hours > 12) { - return `${hours - 12}:00 pm`; + time = hours - 12; + return `${time.toString().padStart(2 , "0")}:00 pm` } return `${time} am`; } @@ -23,3 +24,24 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); +const currentOutput3 = formatAs12HourClock("19:00"); +const targetOutput3 = "07:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("21:00"); +const targetOutput4 = "09:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + + + +// The previous version only worked properly for two-digit hours. To fix this, I made sure the hour is always displayed in two-digit format. + +// First, I completed the mathematical calculation and stored the result in a variable. Then, I converted it to a string and used padStart(2, "0") to add a leading zero when needed. + +// Now, it works correctly for all times greater than 12 and always shows two digits. \ No newline at end of file From 76d05887a91576039d16912800f1bb3a63e77aa3 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Thu, 5 Mar 2026 10:58:33 +0000 Subject: [PATCH 2/7] Avoid mutating function parameter --- Sprint-2/1-key-errors/0.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index a9416f8005..81cfbdb5e7 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -20,8 +20,8 @@ function capitalise(str) { // This avoids the redeclaration error and the code runs correctly. // =============> write your new code here function capitalise(str) { - str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + const capitalised = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalised; } let str = capitalise("Arun"); console.log(str); From ff2c4c56ae0dbd7b03f831f16d8a59be0420a6bf Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Thu, 5 Mar 2026 11:17:41 +0000 Subject: [PATCH 3/7] Rename variable for clarity --- Sprint-2/1-key-errors/1.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index 02419d0727..c2ca82a071 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -36,5 +36,5 @@ function convertToPercentage(decimalNumber) { return percentage; } -const decimalNumber = convertToPercentage(0.5) ; -console.log(decimalNumber); \ No newline at end of file +const percentageValue = convertToPercentage(0.5) ; +console.log(percentageValue); \ No newline at end of file From f98c27d3a377556ae0a48a7f41146a667f610f05 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Thu, 5 Mar 2026 11:23:14 +0000 Subject: [PATCH 4/7] Improve variable naming and use const --- Sprint-2/1-key-errors/2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index 8878cfae7a..df085ed819 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -40,5 +40,5 @@ function square(3) { function square(num) { return num * num; } -let num = square(3); -console.log(num); \ No newline at end of file +const squaredValue = square(3); +console.log(squaredValue); \ No newline at end of file From 52a1b76d6c101263280966aa093113cf3b55ce04 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Thu, 5 Mar 2026 11:25:36 +0000 Subject: [PATCH 5/7] Remove unnecessary parentheses in multiply function --- Sprint-2/2-mandatory-debug/0.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b3c702a94c..7b1ad8458e 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -32,7 +32,7 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // As a result, the correct value is displayed instead of undefined. // =============> write your new code here function multiply(a, b) { - return (a * b); + return a * b; } From 5cf8a5aaf53fc1361a1bd8d6a45824ecebc19904 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Thu, 5 Mar 2026 11:44:25 +0000 Subject: [PATCH 6/7] Refactor upperSnakeCase using method chaining --- Sprint-2/3-mandatory-implement/2-cases.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index c513adc944..9cfd953f44 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -16,9 +16,9 @@ // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase function upperSnakeCase(str){ - str = str.toUpperCase() + str.toUpperCase() - return str.replaceAll(" " , "_"); + return str.toUpperCase().replaceAll(" " , "_"); ; } From 0422ff03ef620e30326342ffd696e0e9bf215272 Mon Sep 17 00:00:00 2001 From: Arunkumar Akilan Date: Thu, 5 Mar 2026 11:52:33 +0000 Subject: [PATCH 7/7] Avoid mutating time parameter and store converted hours in a new variable --- Sprint-2/5-stretch-extend/format-time.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 34d02fad82..ca0ad3d73a 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -5,8 +5,8 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); if (hours > 12) { - time = hours - 12; - return `${time.toString().padStart(2 , "0")}:00 pm` + const convertedHours= hours - 12; + return `${convertedHours.toString().padStart(2 , "0")}:00 pm` } return `${time} am`; }