diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..3a843eda2b 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> The fuction intends to capitalize the first letter of the input string +// from th left starting with the first character (str[0]). // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +10,8 @@ function capitalise(str) { return str; } -// =============> write your explanation here -// =============> write your new code here +// =============> str has already been declared as a parameter function, so it cannot be redeclared within the function body. This will cause a syntax error. To fix this, we should use a different variable name for the new string we are creating. +// =============> + function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; \ 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 f2d56151f4..eff952dc5c 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,8 @@ // Predict and explain first... -// Why will an error occur when this program runs? -// =============> write your prediction here +// Why will an error occur when this program runs? +// =============> decimalNumber is a constant variable, and cannot be changed. +// The code is attempting to change it which will cause an error. // Try playing computer with the example to work out what is going on @@ -14,7 +15,12 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// =============> The code is attempting to get the percentage of decimalNumber // Finally, correct the code to fix the problem -// =============> write your new code here +function convertToPercentage(decimalNumber) { + decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; + return percentage; +} +console.log (convertToPercentage(0.5)); \ 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..f8b6621567 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,22 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> num is not defined which causes an error. function square(3) { return num * num; } -// =============> write the error message here +// =============> 1. "Uncaught SyntaxError: Unexpected number" Means the "3" is not valid there. +// 2. Uncaught SyntaxError: Illegal return statement. Means "num" has no idea of what it should be -// =============> explain this error message here // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> by adding square(num) the function will work when given a number +function square(num) { + return num * num; +} +console.log(square(3)) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..26b87fd105 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> Code is attempting to multiple a & b but there is no return value function multiply(a, b) { console.log(a * b); @@ -8,7 +8,11 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> because there is no return value, the outcome stays as undefined while giving the answer +// to fix this, change "console.log" in the second row to "return" +// // Finally, correct the code to fix the problem -// =============> write your new code here +function multiply(a, b) { + return (a * b); +} diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..a4e2e1d976 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> the function is attempting to add "a" & "b" together +// but the return is formatted wrong. function sum(a, b) { return; @@ -8,6 +9,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> because of the way line 6 and 7 is written the answer will be undefined. +//remove the semi colon from line 6 and then add line 7 to it. // Finally, correct the code to fix the problem -// =============> write your new code here + +function sum(a, b) { + return (a + b); +} + +console.log(sum(10, 32)) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..b145646d95 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,8 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> The function is trying to get the last digit of "num" +// but because on "const", the "num" is always locked to "103" leaving the return always "3" const num = 103; @@ -14,11 +15,22 @@ 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 +// =============> Just as predicted, the function is trying to get the last digit, which is always "3" // Explain why the output is the way it is -// =============> write your explanation here +// =============> because of "const" the num is always locked to "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)}`); + // This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +// Explain why getLastDigit is not working properly +// It's not working properly because of the "const num = 103" +// to fix, I have removed the line and added "(num)" after "function getLastDigit" +// In order to define what "num" is. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..55f462875e 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,11 @@ 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) { + const bmi = weight / (height * height); + return parseFloat(bmi.toFixed(1)); +} + +console.log(calculateBMI(70, 1.73)); \ 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 5b0ef77ad9..3ae8e664d5 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,11 @@ // 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 Capitalise(inputString) { + return inputString.toUpperCase() + +} + +Console.log(Capitalise("hello there")); \ 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..cfd2bf1b46 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,23 @@ // 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 + +const penceString = "399p"; + +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"); + +console.log(`£${pounds}.${pence}`); + diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..9053e171ee 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> Pad will be called 3 times, once for totalHours, once for remainingMinutes, and once for 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 will be 0 // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> The return value 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 it is the value of the remainingSeconds variable, which is calculated as 61 % 60 = 1. // 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 output is "01" because the pad function adds a leading zero to the number 1, resulting in a two-character string "01". \ No newline at end of file