From 64fd84374c1006a199ca8c2ca3f309ff4afabaa9 Mon Sep 17 00:00:00 2001 From: MehrozMunir Date: Thu, 26 Feb 2026 13:06:04 +0000 Subject: [PATCH 1/2] sprint 2 completed --fixed --- Sprint-2/1-key-errors/0.js | 12 ++- Sprint-2/1-key-errors/1.js | 15 ++-- Sprint-2/1-key-errors/2.js | 16 +++- Sprint-2/2-mandatory-debug/0.js | 14 +++- Sprint-2/2-mandatory-debug/1.js | 9 ++- Sprint-2/2-mandatory-debug/2.js | 27 +++++-- Sprint-2/3-mandatory-implement/1-bmi.js | 6 +- Sprint-2/3-mandatory-implement/2-cases.js | 11 +++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 24 ++++++ Sprint-2/4-mandatory-interpret/time-format.js | 20 ++--- Sprint-2/5-stretch-extend/format-time.js | 80 ++++++++++++++++--- 11 files changed, 195 insertions(+), 39 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..21034a52ea 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// I am not able to predict any error, everything seems fine to me. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +9,11 @@ function capitalise(str) { return str; } -// =============> write your explanation here -// =============> write your new code here +// =============> Yeah, str name for the variable is already being used as the parameter of the function, +// that's why we can't declare it agian as being done in line 8. +// =============> the correct code would be as follows: + +/*function capitalise(str) { + let capitaliseStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitaliseStr; +}*/ diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..400dc4beac 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,8 +1,7 @@ // Predict and explain first... - // Why will an error occur when this program runs? -// =============> write your prediction here - +// =============> There would be a syntax error because the decimalNumber is declared again as const +//and logically there is no need for initializing decimalNumber again with 0.5 value as in that case the method would return 50% always // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -14,7 +13,13 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// =============> Yes, the error is because the identifier decimalNumber has already been declared. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> the correct code would be as follows:- + +/*function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +}*/ diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..091f6ff667 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,30 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> In the parameters of function we can write the name of the variable but not the actual value. +//we write actual value when we call the function. function square(3) { return num * num; } // =============> write the error message here +/* +function square(3) { + ^ + +SyntaxError: Unexpected number +*/ -// =============> explain this error message here +// =============> Yeah, the error is because that when writing the function it does not expect number value instead of the parameter name // Finally, correct the code to fix the problem // =============> write your new code here +/* +function square(num) { + return num * num; +} +*/ diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..48faaed883 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,8 @@ // Predict and explain first... // =============> write your prediction here +//I think that the problem is our function multiply does not return any value but write in console +// However, we are calling the function inside another console.log and expecting the function to return some value. function multiply(a, b) { console.log(a * b); @@ -8,7 +10,17 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> Yeah, when we run the code we get the following output which shows that second console is not able to get the value from the function as the function does not return any value. +/* + 320 +The result of multiplying 10 and 32 is undefined +*/ // 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)}`); +*/ diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..488e0bdfd1 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 - +// In the below function, after return there is a semi colon which syntactically would just return nothing and any line of code below that would not run function sum(a, b) { return; a + b; @@ -9,5 +9,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +//The function is returning undefined + // Finally, correct the code to fix the problem // =============> write your new code here +/* +function sum(a, b) { + return a + b; +} +*/ diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..3d745069c6 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,11 +1,13 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here - +/* The last digit of 42 is 2 + The last digit of 105 is 5 + The last digit of 806 is 6 +*/ const num = 103; -function getLastDigit() { +function getLastDigit(num) { return num.toString().slice(-1); } @@ -15,10 +17,23 @@ 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 +// =============> I just realized now that the function getLastDigit is just always just returning the last digit of cons num + // 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 +*/ diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..b0e4309d08 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,7 @@ // 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 (weight / (height * height)).toFixed(1); +} + +console.log(calculateBMI(90, 1.83)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..67117e78e2 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,14 @@ // 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 + +/* +1.Split the String by comma to get the separate words +2.Join the separate words by _ to convert the string into snake case +3. uppercase all the letters in the string using toUpperCase Method in Javascript +*/ +function upperSnakeCase(myString) { + return myString.split(" ").join("_").toUpperCase(); +} + +console.log(upperSnakeCase("My name is Mehroz")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..be52539086 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 + +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("400p in pounds is :" + toPounds("400p")); +console.log("420p in pounds is :" + toPounds("420p")); +console.log("0p in pounds is :" + toPounds("0p")); +console.log("1p in pounds is :" + toPounds("1p")); +console.log("248p in pounds is :" + toPounds("248p")); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8c..b8168c6d04 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -3,10 +3,12 @@ function pad(num) { } function formatTimeDisplay(seconds) { - const remainingSeconds = seconds % 60; - const totalMinutes = (seconds - remainingSeconds) / 60; - const remainingMinutes = totalMinutes % 60; - const totalHours = (totalMinutes - remainingMinutes) / 60; + const remainingSeconds = seconds % 60; // 1 + const totalMinutes = (seconds - remainingSeconds) / 60; //1 + const remainingMinutes = totalMinutes % 60; // 1 + const totalHours = (totalMinutes - remainingMinutes) / 60; //0 + + console.log(totalHours + " " + remainingMinutes + " " + remainingSeconds); return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } @@ -17,18 +19,18 @@ function formatTimeDisplay(seconds) { // Questions // 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 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 +// =============> Value assigned to num is '1' i.e. the value fo remaining seconds. As when in the program we did seconds % 60 we got the remaining seconds as 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 +// =============> the value assigned is '01' because when the function pad is called on the value 1, the padStart method inside that function applies the pad length of 2 using 0. diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..0b4056d2fd 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,24 +2,84 @@ // 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. +/* +edge cases to be checked: +1. 00:00 +2. 24:00 +3. 12:00 +4. 12:01 +5. 00:01 +*/ + +function pad(num) { + return num.toString().padStart(2, "0"); +} + function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; - } - return `${time} am`; + const minutes = Number(time.slice(3, 5)); + + if (hours == 0 || hours == 24) { + return `12:${pad(minutes)} am`; + } else if (hours > 12) { + return `${pad(hours - 12)}:00 pm`; + } else if (hours == 12) { + return `${time} pm`; + } else return `${time} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +let currentOutput = formatAs12HourClock("08:00"); +let targetOutput = "08:00 am"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); + +currentOutput = formatAs12HourClock("23:00"); +targetOutput = "11:00 pm"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); + +currentOutput = formatAs12HourClock("13:00"); +targetOutput = "01:00 pm"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); + +currentOutput = formatAs12HourClock("00:00"); +targetOutput = "12:00 am"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); + +currentOutput = formatAs12HourClock("24:00"); +targetOutput = "12:00 am"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); + +currentOutput = formatAs12HourClock("12:00"); +targetOutput = "12:00 pm"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +currentOutput = formatAs12HourClock("12:01"); +targetOutput = "12:01 pm"; console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); + +currentOutput = formatAs12HourClock("00:01"); +targetOutput = "12:01 am"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` ); From f70662ae76e8f275aeb58d7762e7d536c611a47a Mon Sep 17 00:00:00 2001 From: MehrozMunir Date: Sun, 8 Mar 2026 11:05:40 +0000 Subject: [PATCH 2/2] commented out the correct code --- Sprint-2/1-key-errors/0.js | 8 ++++---- Sprint-2/1-key-errors/1.js | 8 ++++---- Sprint-2/1-key-errors/2.js | 7 +++---- Sprint-2/2-mandatory-debug/0.js | 7 +++---- Sprint-2/2-mandatory-debug/1.js | 8 ++++---- Sprint-2/2-mandatory-debug/2.js | 7 +++---- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 21034a52ea..6ddcce4267 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -3,17 +3,17 @@ // 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; -} +}*/ // =============> Yeah, str name for the variable is already being used as the parameter of the function, // that's why we can't declare it agian as being done in line 8. // =============> the correct code would be as follows: -/*function capitalise(str) { +function capitalise(str) { let capitaliseStr = `${str[0].toUpperCase()}${str.slice(1)}`; return capitaliseStr; -}*/ +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index 400dc4beac..8b6fa0f516 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -4,12 +4,12 @@ //and logically there is no need for initializing decimalNumber again with 0.5 value as in that case the method would return 50% always // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { +/*function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; -} +}*/ console.log(decimalNumber); @@ -18,8 +18,8 @@ console.log(decimalNumber); // Finally, correct the code to fix the problem // =============> the correct code would be as follows:- -/*function convertToPercentage(decimalNumber) { +function convertToPercentage(decimalNumber) { const percentage = `${decimalNumber * 100}%`; return percentage; -}*/ +} diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index 091f6ff667..c3795064ff 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -6,9 +6,9 @@ // =============> In the parameters of function we can write the name of the variable but not the actual value. //we write actual value when we call the function. -function square(3) { +/*function square(3) { return num * num; -} +}*/ // =============> write the error message here /* @@ -24,9 +24,8 @@ SyntaxError: Unexpected number // =============> write your new code here -/* + function square(num) { return num * num; } -*/ diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index 48faaed883..4aa865ce9f 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -4,12 +4,12 @@ //I think that the problem is our function multiply does not return any value but write in console // However, we are calling the function inside another console.log and expecting the function to return some value. -function multiply(a, b) { +/*function multiply(a, b) { console.log(a * b); } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); - +*/ // =============> Yeah, when we run the code we get the following output which shows that second console is not able to get the value from the function as the function does not return any value. /* 320 @@ -18,9 +18,8 @@ The result of multiplying 10 and 32 is undefined // 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)}`); -*/ diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 488e0bdfd1..428b6d8efd 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,10 +1,10 @@ // Predict and explain first... // =============> write your prediction here // In the below function, after return there is a semi colon which syntactically would just return nothing and any line of code below that would not run -function sum(a, b) { +/*function sum(a, b) { return; a + b; -} +}*/ console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); @@ -13,8 +13,8 @@ console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // Finally, correct the code to fix the problem // =============> write your new code here -/* + function sum(a, b) { return a + b; } -*/ + diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 3d745069c6..2c8a230472 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -7,13 +7,14 @@ */ const num = 103; -function getLastDigit(num) { +/*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)}`); +*/ // Now run the code and compare the output to your prediction // =============> write the output here @@ -27,7 +28,7 @@ The last digit of 806 is 3 // Finally, correct the code to fix the problem // =============> write your new code here -/* + function getLastDigit(num) { return num.toString().slice(-1); } @@ -35,5 +36,3 @@ function getLastDigit(num) { 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)}`); - -*/