diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..24ded9fd87 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// The variable inside the function str has already declared as function parameter. // 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; -} +//function capitalise(str) { + //let str = `${str[0].toUpperCase()}${str.slice(1)}`; + //return str; +//} // =============> write your explanation here +// Repeatative variable declaration prediction matches with the error message. Then I changed the old variable into new inside the +// function. Then affter running the code it won't return anything cause no function call has been made yet. // =============> write your new code here +// This is my code: +function capitalise(str) { + let newstr = `${str[0].toUpperCase()}${str.slice(1)}`; + return newstr; +} +console.log(capitalise("dipa")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..af0d0faf7f 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,37 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// I thought the function will work properly but couldn't print the correct value cause the value should be convertToPercentage +// but it is declared only as decimalNumber. // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; +//function convertToPercentage(decimalNumber) { + //const decimalNumber = 0.5; + //const percentage = `${decimalNumber * 100}%`; - return percentage; -} + //return percentage; +//} -console.log(decimalNumber); +//console.log(decimalNumber); // =============> write your explanation here +// When I run the code after my prediction it shows SyntaxError: Identifier 'decimalNumber' has already been declared. Which means +// decimalNumber has been declared already as function parameter and then again declared inside the function as variable. Then when I +// declare const decimalNumber = 0.5; outside the function and run the code it shows the value of variable decimalNumber which is 0.5. Then +// I replace console.log(decimalNumber); to console.log(convertToPercentage); and run the code again and it shows this output +// [Function: convertToPercentage] which means only the funtion name. Then I replace console.log(convertToPercentage); to +// console.log(convertToPercentage(decimalNumber)); again and it finally works properly that means converted the decimal number to +// percentage value which shows 50% . + // Finally, correct the code to fix the problem // =============> write your new code here +// Corrected code: + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} +const decimalNumber = 0.5; +console.log(convertToPercentage(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..3e36dff7c7 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,28 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// I think we cannot directly insert value which is 3 as a parameter. We have to declare it as a variable. -function square(3) { - return num * num; -} +//function square(3) { +// return num * num; +//} // =============> write the error message here +// The error message shows SyntaxError: Unexpected number // =============> explain this error message here +// The error shows SyntaxError as Unexpected number which means the function syntax doesnot expect to input +// any values directly in the parameter. That's why the function doesnot work. // Finally, correct the code to fix the problem // =============> write your new code here +// The correct code: + +function square(num) { + return num * num; +} +let num = 3; +console.log(square(num)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..d683f58beb 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,24 @@ // Predict and explain first... // =============> write your prediction here +// I think there is nor function call has been made. a and b only inserted as function parameters. -function multiply(a, b) { - console.log(a * b); -} +// function multiply(a, b) { +// console.log(a * b); +// } -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// The result of multiplying 10 and 32 is undefined. This is the error. Now I think the reason for showing the result undefined is that +// console.log(a * b); only prints 320 but the function does not return anything. // Finally, correct the code to fix the problem // =============> write your new code here +// The correct code: + +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..a461f94f8f 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,23 @@ // Predict and explain first... // =============> write your prediction here +// I think only return; doesnot mean anything means it won't return anything. Besides, the expression a + b; won't also work +// cause after calculating a + b it won't return anything and the output will show undefined value. -function sum(a, b) { - return; - a + b; -} +// function sum(a, b) { +// return; +// a + b; +// } -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// My prediction exactly matches with the output after I run code. // Finally, correct the code to fix the problem // =============> write your new code here +// The correct code: + +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..6e3193e636 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,42 @@ // Predict the output of the following code: // =============> Write your prediction here +// i think the function won't return anything for numbers 42, 105 and 806 because the variable num contains value 103 and the return will only +// work with 103 inside the function. There is no relation between const num = 103; and 42, 105 and 806. -const num = 103; +// const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// function getLastDigit() { +// 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)}`); +// 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 +// The output: +// 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 output is quite similar to my prediction. When the function tried to find out function calls for getLastDigit(42), getLastDigit(105) +// and getLastDigit(806) it doesn't find any of these because those didn't match with the variable declaration. As the function wokred only +// with 103, it returned value for 103 and printed 3 as last digit for all three values. // Finally, correct the code to fix the problem // =============> write your new code here +// The correct 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)}`); // 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..5990bd3ed2 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,10 @@ // 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(weightkg, heightm) { + const BMI = (weightkg / (heightm * heightm)); + return BMI.toFixed(1); +} +let result = calculateBMI(70, 1.73); +console.log (result); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..5dcf6b0db1 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,9 @@ // 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 toUpperSnakeCase(str) { + let newstr = str.replaceAll(" ", "_").toUpperCase(); + return newstr; +} +console.log(toUpperSnakeCase("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..13ac858bbc 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,14 @@ // 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(pence) { + const remainingPence = (pence % 100); + const Pounds = (pence - remainingPence) / 100; + return `£${Pounds}.${remainingPence}`; +} + +console.log (toPounds(155)); +console.log (toPounds(99)); +console.log (toPounds(10000)); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..d222a5a302 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -15,24 +15,29 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +const seconds = 61; +console.log(formatTimeDisplay(seconds)); // 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 +// For each formatTimeDisplay is called, pad will be called 3 times. 1. pad(totalHours), 2. pad(remainingMinutes), 3. pad(remainingSeconds). // 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 +// The value assigned to num when pad is called for the first time will be 0. // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// The return value of pad when it is called for the first time will be "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 +// The value assigned to num when pad is called for the last time in this program will be 1. Because, pad is called for the last time +// for the value of remainingSeconds. // 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 +// The return value of pad when it is called for the last time in this program will be "01". Because, after converting num.toString(1) +// the length of the string "1" is 1 character. So it checks in while (numString.length < 2) and founds "1" which is less than +// 2 character length. Then it will go inside numString = "0" + numString; and executes the function and will return "01". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..54f10bc6a6 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,18 +4,34 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const minutes = time.slice(3); + + if (hours === 0) { + return `12:${time.slice(3)} am`; + } + else if (hours < 12) { + return `${time} am`; + } + else if (hours <= 12) { + return `${time} pm`; + } + else if (hours > 12) { + return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`; } - return `${time} am`; + else if (hours > 12) { + return `${hours - 12}:${minutes} pm`; + } + else if (hours < 12) { + return `${time}:${minutes} am`; +} } - const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` ); +console.log (formatAs12HourClock("08:00")); const currentOutput2 = formatAs12HourClock("23:00"); const targetOutput2 = "11:00 pm"; @@ -23,3 +39,44 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); +console.log (formatAs12HourClock("23:00")); + +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); +console.log (formatAs12HourClock("00:00")); + +const currentOutput4 = formatAs12HourClock("23:59"); +const targetOutput4 = "11:59 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); +console.log (formatAs12HourClock("23:59")); + +const currentOutput5 = formatAs12HourClock("12:00"); +const targetOutput5 = "12:00 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); +console.log (formatAs12HourClock("12:00")); + +const currentOutput6 = formatAs12HourClock("13:00"); +const targetOutput6 = "01:00 pm"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); +console.log (formatAs12HourClock("13:00")); + +const currentOutput7 = formatAs12HourClock("11:59"); +const targetOutput7 = "11:59 am"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}` +); +console.log (formatAs12HourClock("11:59")); \ No newline at end of file