diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..6c824a5f7 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 +// =============> write your prediction here: +// there is promblem declaring the string twice. // 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: It say's syntax error "str" has been declared twice. We can change variable name in tp text or anything else -// =============> write your explanation here // =============> write your new code here + +function capitalise(text) { + let capitalisedText = `${text[0].toUpperCase()}${text.slice(1)}`; + return capitalisedText; +} +console.log(capitalise("hello world")); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..4b11b4c25 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,27 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> write your prediction here: const decimalNumber = 0.5; this line should be declare outside of function // 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 +// =============> write your explanation here: In line 9 const decimalNumber = 0.5; should not be in their because it will redeclare in side function which will be error, it will be local scope if its inside a function. declaring outside of code make global scope, can call through out the code. // Finally, correct the code to fix the problem // =============> write your new code here +const decimalNumber = 0.5; +function convertToPercentage(decimalNumber){ + const percentage = `${decimalNumber*100}%`; + return percentage; + +} +console.log(decimalNumber); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..181ec7974 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,23 @@ // 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: because parameter should use as num . -function square(3) { - return num * num; -} +//function square(3) { + // return num * num; +//} + +// =============> write the error message here: syntax error: unexpected number. -// =============> write the error message here +// =============> explain this error message here: We cannot use parameter as num like 3 or 4 in inside function as argument we should use num, so we can re call many times through out the code. -// =============> explain this error message here // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num){ + return num * num; +} +console.log(square(3)); +console.log(square(4)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..b0bdba4b7 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,16 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here: there is no return statement -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 +// =============> write your explanation here; +//multiply function is logged with out define so the result will be undefined, need add return statement to complete it. // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..45daa7d65 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,17 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here: there is ; error + + +// =============> write your explanation here: function will be undefined because of ";" by removing ; placing a+b in same line will return function. +// Finally, correct the code to fix the problem +// =============> write your new code here function sum(a, b) { - return; - a + b; + return a+b; + } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +//Between "return value" and "function", which one is undefined? -// =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here +//return value will be undefined if function have not return statement it will automatically returns undefined. \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..275089586 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,38 @@ -// Predict and explain first... +// Predict and explain first...because of const num, it won't change output. // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here: 3 const num = 103; - 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 806 is ${getLastDigit(806)}`); // Now run the code and compare the output to your prediction -// =============> write the output here +// =============> write the output here: 3 for all console.log. // Explain why the output is the way it is -// =============> write your explanation here +// =============> write your explanation here: 3 for all of the the console.log because it won't take any argument it will always take global variable num = 103. // 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)}`); + +// Does the function toPounds() return the last digit of the given parameter? +// No, it doesn't return toPounds() of the last digit of the given parameter. + + + + // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem +//because of line 6. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..8adb9bc43 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,10 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file +} +function calculateBMI(weight, height){ + let bmi = weight /(height * height); + return parseFloat(bmi.toFixed(1)); +} + +console.log(calculateBMI(70, 2.99)); \ 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 5b0ef77ad..0b032d3fd 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 +const text = `hello there`; +console.log(text.toUpperCase().replace(/ /g, "_")); + +const sentence = `lord of the rings`; +console.log(sentence.toUpperCase().replace(/ /g, "_")); \ 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 6265a1a70..57706ca60 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,24 @@ // 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(`The last digit of 399p is ${toPounds("399p")}`); +console.log(`The last digit of 52p is ${toPounds("52p")}`); +console.log(`The last digit of 1099p is ${toPounds("1099p")}`); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..baae34072 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,6 +10,8 @@ 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 @@ -17,18 +19,18 @@ function formatTimeDisplay(seconds) { // 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: 0 // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> 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 +// =============> write your answer here: 1, when pad is called for the last time, pad(remainingSecond) is remainingSecond=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: It will be 01 as we used padStart(2, "0") which will make 2 digit for example if value was 1 it will return as 01.