From d7a7034af178f982562c79e4629cb6200b781100 Mon Sep 17 00:00:00 2001 From: abduhasen Date: Wed, 1 Jul 2026 12:09:11 +0100 Subject: [PATCH 1/2] I worked on interpreting code,fixing error, implementing of code, debugging of code --- Sprint-2/1-key-errors/0.js | 11 +++++-- Sprint-2/1-key-errors/1.js | 14 +++++++-- Sprint-2/1-key-errors/2.js | 18 +++++++----- Sprint-2/2-mandatory-debug/0.js | 14 +++++++-- Sprint-2/2-mandatory-debug/1.js | 11 +++++-- Sprint-2/2-mandatory-debug/2.js | 12 +++++--- Sprint-2/3-mandatory-implement/1-bmi.js | 9 ++++-- Sprint-2/3-mandatory-implement/2-cases.js | 5 ++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 19 ++++++++++++ Sprint-2/4-mandatory-interpret/time-format.js | 13 +++++---- Sprint-2/5-stretch-extend/format-time.js | 29 +++++++++++++++---- 11 files changed, 118 insertions(+), 37 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..70a3a6b746 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,6 +1,6 @@ // Predict and explain first... // =============> write your prediction here - +// My prediction is that the function will throw an error because the variable which also the parameter is redeclared again inside the function. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +9,10 @@ function capitalise(str) { return str; } -// =============> write your explanation here -// =============> write your new code here +// =============> write your explanation here: to explain, The parameter str already exists in that function's scope, +// so trying to `let str = ...` again is redeclaring the same name in the same scope, and JS won't allow it. +// =============> write your new code here: function capitalise(str) { +// let capitaliseFristLatter = `${str[0].toUpperCase()}${str.slice(1)}`; + +// return capitaliseFristLatter; +// } diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..f90333e453 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,7 +2,7 @@ // Why will an error occur when this program runs? // =============> write your prediction here - +// My prediction is that the variable `decimalNumber` is already declared and also the console.log can only log the variable decimalNumber. // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -14,7 +14,15 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// =============> write your explanation here: the variable was already declared in the parameter scope and it won't redeclare inside the function again +// and also the console.log was only working for the logging of the variable `decimalNumber` not the function. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here: const decimalNumber = 0.5; +// function convertToPercentage(decimalNumber) { +// const percentage = `${decimalNumber * 100}%`; + +// return percentage; +// } + +// console.log(convertToPercentage(decimalNumber)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..69f8b40fe9 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,22 @@ - // 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 +// =============> write your prediction of the error here: it will throw an error because in the parameter(placeholder) +// there is constant value which js does not recognize. function square(3) { - return num * num; + return num * num; } -// =============> write the error message here +// =============> write the error message here: the error message SyntaxError: Unexpected number -// =============> explain this error message here +// =============> explain this error message here: syntax error, meaning it fails before the function even gets defined. +// JS expects a parameter slot to contain a name (identifier) and gets confused +// when it sees a raw number sitting there instead. // Finally, correct the code to fix the problem -// =============> write your new code here - - +// =============> 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..850d40fbcb 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 +// =============> write your prediction here: My prediction is that in this function there is two console.log +// the result of this two log will print one with the value only 320 and +// the other one with the sentences of the log not the value. function multiply(a, b) { console.log(a * b); @@ -8,7 +10,13 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here: because of the log inside the function the outside function can't access the result so the log will print +// one with the value only and second console.log will print the sentences with explanation of the result and +// undefined value because the the function doesn't access the operation. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> 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..14c8de0457 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 +// =============> write your prediction here: the function have nothing to return so it will log the sentences with undefined value. function sum(a, b) { return; @@ -8,6 +8,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here: firstly the process start accepting that it is a function and takes +// the parameter to hold the place then it go through the function but by the 5th line +// the function return and the value will come out undefined // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> 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..02c74337e6 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 +// =============> Write your prediction here: the code is already declared a variable that can not be changed so the consol.log always be the same 3. const num = 103; @@ -14,11 +14,15 @@ 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 +// =============> write the output here: 3 // Explain why the output is the way it is -// =============> write your explanation here +// =============> write your explanation here: the code is already declared a variable that can not be changed in the +// first place so when ever the process try to access another console.log it will take the same value as before +// so the consol.log always be the same. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here: function getLastDigit(num) { +// return num.toString().slice(-1); +// } // 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..1655281f4d 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,10 @@ // 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 + const squaredHeight = height * height; + const originalBMI = (weight / squaredHeight).toString(); + let dotIndex = originalBMI.indexOf("."); + let bmiWithOneDecimal = originalBMI.slice(0, dotIndex + 2); + return bmiWithOneDecimal; +} +console.log(`The calculated BMI is ${calculateBMI(78.4, 1.71)}`); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..b0efa45fdf 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,8 @@ // 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 toCapitalizedSnakeCase(str) { + const toCapitalized = str.toUpperCase().replace(/ /g, "_"); + return toCapitalized; +} diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..89354d2208 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}`; +} diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..74588249b2 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -14,25 +14,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: 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:it is 0, because totalHours is 0. // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here:it is 00, because the function pad will add another 0 if it is less than 2 index. // 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:it is 1, because the remainingSeconds value will become 1 after it get the remainder of the seconds. // e) What is the return value of pad when it is called for the last time in this program? Explain your answer -// =============> write your answer here +// =============> write your answer here:it will be 01,because after going through the function pad it will gain 0 from the front because it is less +// than 2 index so the return will be 01. diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..19bfc2b789 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,22 +4,41 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}:${minutes} pm`; + } else if (hours == 12) { + return `${hours}:${minutes} pm`; + } else if (hours == 0) { + return `${12}:${minutes} am`; } return `${time} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput = formatAs12HourClock("08:19"); +const targetOutput = "08:19 am"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +const currentOutput2 = formatAs12HourClock("23:58"); +const targetOutput2 = "11:58 pm"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("00:27"); +const targetOutput3 = "12:27 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("12:00"); +const targetOutput4 = "12:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); From d38452c78ee3e1a74e538512b28931f9b084351a Mon Sep 17 00:00:00 2001 From: abduhasen Date: Fri, 3 Jul 2026 13:41:42 +0100 Subject: [PATCH 2/2] fixing code using mentor feed back --- Sprint-2/2-mandatory-debug/1.js | 3 ++- Sprint-2/3-mandatory-implement/2-cases.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 14c8de0457..23a6e1dbcc 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -10,7 +10,8 @@ console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here: firstly the process start accepting that it is a function and takes // the parameter to hold the place then it go through the function but by the 5th line -// the function return and the value will come out undefined +// the function return and the value will come out undefined,which means java script doesn't +// have step to continue so it is returning with out a value. // 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/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index b0efa45fdf..c3e965172e 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -15,7 +15,7 @@ // 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 toCapitalizedSnakeCase(str) { +function toUpperSnakeCase(str) { const toCapitalized = str.toUpperCase().replace(/ /g, "_"); return toCapitalized; }