diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..21034a52e 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 f2d56151f..400dc4bea 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 aad57f7cf..091f6ff66 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 b27511b41..48faaed88 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 37cedfbcf..488e0bdfd 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 57d3f5dc3..3d745069c 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 17b1cbde1..b0e4309d0 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 5b0ef77ad..67117e78e 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 6265a1a70..be5253908 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 7c98eb0e8..b8168c6d0 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 32a32e66b..0b4056d2f 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}` );