diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..825d8fffd 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,29 @@ // Predict and explain first... // =============> write your prediction here +/* +My prediction is that this function will take the first letter of a string and capital it. +*/ + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring -function capitalise(str) { +/*function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; -} +}*/ // =============> write your explanation here + +/* +The main reason why the capitalise function have a not define error because the the str variable have been declare in the parameter +inside the function, the str variable is re-declare again that will break the rule of how variable work. +*/ + // =============> write your new code here + +function capitalise(str) { + let firstCapitaLetter = `${str[0].toUpperCase()}${str.slice(1)}`; + return firstCapitaLetter; +}; +console.log(capitalise("hello")); \ 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..5577c030a 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,9 +2,10 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// When the JS executed the code, it will give 2 possible error 1 is decimalNumber is define inside the function and decimalNumber is undefined when using console.log // Try playing computer with the example to work out what is going on - +/* function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -13,8 +14,26 @@ function convertToPercentage(decimalNumber) { } console.log(decimalNumber); - +*/ // =============> write your explanation here +/* +For function convertToPercentage there are 1 error: + inside the function the decimalNumber parameter is being re-declare and given a fix value, this will give this function a + decimalNumber are ready defined. + +Next is when we console.log the function instead of calling the function name and value we used the function parameter variable name +instead. This will give the error decimalNumber is undefined. +*/ + // Finally, correct the code to fix the problem // =============> write your new code here + + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(0.25)); \ 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 aad57f7cf..175445b2f 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -5,16 +5,23 @@ // =============> write your prediction of the error here +// For function square the variable inside the parameter will have a error, because number cant be used when name a variable and num variable is undefine. +/* function square(3) { return num * num; } - +*/ // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +//Because number cant be used when name a variable. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +}; +console.log(square(3)); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..95ebefec0 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,32 @@ // Predict and explain first... // =============> write your prediction here +/* +The function multiply when is log into the terminal. we will receive a print out expression from +first console.log inside the function and undefined value from the string template inside the section console.log. +*/ +/* 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 +/* +First the function multiply have 2 parameter identifier a and b, the main purpose of this function is to multiply any argument that put +into the identifier a and b. Inside the the function is we have the function call console.log and nested inside is the expression from multiply of a and b. +However this will lead to error, since we need to return the expression(value) back to the function so it can be re used when call in a string template or other +function. +*/ // 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..f317779a0 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,27 @@ // Predict and explain first... // =============> write your prediction here - +// The function sum when is call inside the string template. The print out will be "The sum of 10 and 32 is undefined". +/* function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); - +*/ // =============> write your explanation here + +/* +For the function sum the writing format is correct but however instead of return the expression from the combine value +of a and b, right now there is no expression return back to the function. So when the function is used inside the string template, +it give the value of undefined instead of actual value of a and b combine together. +*/ + // Finally, correct the code to fix the problem // =============> write your new code here + +function sum(a, b) { + return a + b; +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); \ 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..cf2ed5718 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,6 +3,11 @@ // Predict the output of the following code: // =============> Write your prediction here +/* The function getLastDigit should get the last digit number from any number argument pass into the function, but however it only take +value from variable num and only give out the value of 3. +*/ + +/* const num = 103; function getLastDigit() { @@ -12,13 +17,35 @@ function getLastDigit() { 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 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 +/* +Because the function getLastDigit when use, instead of picking last digit number of any random number argument pass into the function +it only pick last digit number from the variable num value of 103. So when the user try to input other random number into +the function is only print out the number 3 as the result. +*/ // Finally, correct the code to fix the problem // =============> write your new code here +const num = 103; + +function getLastDigit(digit) { + return digit.toString().slice(-1); +} +console.log(`The last digit of 103 is ${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)}`); + // 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..c638a81df 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,7 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + return Number((weight / (height * height)).toFixed(1)); +} + +console.log(calculateBMI(60,1.56)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..f95e0fc31 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 upperCaseSentence (word){ +return word.toUpperCase().replace(" ","_"); +}; + +console.log(upperCaseSentence("wake up")) \ 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..a85596aed 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,10 @@ // 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){ + let pound = pence * 0.01; + return `£ ${pound.toFixed(2)}`; +}; + +console.log(toPounds("400")); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..6d8d0824a 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,7 +10,7 @@ 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 @@ -18,17 +18,31 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +//when the function formatTimeDisplay when is called, the pad function will call 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 +// The assign value to num when it first call is 0. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value of pad for the first time is 0. // 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 assign value to num when the function pad when it +last called will be 1. Because when the pad function called for the third time, it will +receive the value from the variable remainingSeconds(seconds % 60 = 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 assign return value to num when the function pad when it last called will be 01, +because the when the value remainingSeconds pass into the pad function. It will +turn into string that will be add and combine "0" into the string "1" to the left because the padStart method check the total +length of the string "1" and it is less then 2. +*/ \ No newline at end of file