diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..34bce0941 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,12 @@ // Predict and explain first... -// =============> write your prediction here + // Prediction: I think the code will have a SyntaxError because 'str' is declared 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; +return `${str[0].toUpperCase()}${str.slice(1)}`; } -// =============> write your explanation here -// =============> write your new code here + // Explanation: The function already has something called str. You can't make a second thing with the same name inside the same function + // New code: removed the 'let str =' line and returned the expression directly. diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..d8526afa4 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,22 @@ // 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 + // When this program runs there will be an identifier SyntaxError because 'decimalNumber' has already been declared + // Also the output will print a decimal since it says console.log(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}%`; return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(0.5)); -// =============> write your explanation here +// 'decimalNumber' is already declared in two different areas in the same function which JavaScript does not allow. +// 'decimalNumber' is only useful inside the function, so it will give an error of not knowing what decimalNumber is, +// even if it succeeds it will give the wrong output since what we are looking for is the percentage. // Finally, correct the code to fix the problem -// =============> write your new code here +// New code: 1. Removed the duplicate `const decimalNumber = 0.5;` line. +// 2. Changed the console.log to call the function and print the result. diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..055fe4632 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -3,18 +3,19 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// Prediction: there is going to be an error because there is a number in the parameter -function square(3) { +function square(num) { return num * num; } -// =============> write the error message here +// it showed SyntaxError: Unexpected number -// =============> explain this error message here +// the parameter of a function must be a name /identifier, not a number. +// JavaScript expected a word as a parameter but fond a number instead hence the SyntaxError. // Finally, correct the code to fix the problem -// =============> write your new code here +// New code : replaced the parameter with a valid identifier diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..2509f2bfb 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,15 @@ // Predict and explain first... -// =============> write your prediction here +// Prediction:I think it will print 320, but the template string might not show the right result, but will return nothing. function multiply(a, b) { - console.log(a * b); + return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// because console.log will print whatever it is given . +// the function has no return statement, so when we run the code it will say undefined because there is nothing to give back. // Finally, correct the code to fix the problem // =============> write your new code here diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..2090aada8 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,13 @@ // Predict and explain first... -// =============> write your prediction here +// =====> It will not run and will come out as undefined function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here -// Finally, correct the code to fix the problem +// =====> There is a semicolon right after return, which tells it to stop there, +// so basically anything that comes after that will not run hence the undefined result. +// Finally, correct the code to fix the problem will be to remove the semicolon. // =============> write your new code here diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..8c1d2766a 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,7 +1,7 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> I think the result prints will always be 3 as num will always be 103. const num = 103; @@ -14,11 +14,23 @@ 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 code is using the outer variable num which is always 103. +// so every call is returning the last digit of 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 +// because there was no parameter so the function has no way of receiving the numbers. so adding a parameter fixes it. \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..6fcfc1731 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,7 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height + return (weight / (height*height)). toFixed(1); } + +console.log(calculateBMI(70, 1.73)); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..75276cf8c 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/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) { + return str.toUpperCase().replaceAll(" ", "_"); +} + +console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS" \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..c123214d2 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,13 @@ // 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 pence = Number(penceString.replace("p", "")); + return `£${(pence / 100).toFixed(2)}`; +} + +console.log(toPounds("399p")); +console.log(toPounds("5p")); +console.log(toPounds("50p")); +console.log(toPounds("1250p")); +console.log(toPounds("0p")); \ No newline at end of file diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..6216bc5aa 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -21,18 +21,19 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> 3 times because the return statement has three pad() calls in it. Hrs, Mins, Secs. // 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 first pad call pad(totalHours) worked out to 0 // c) What is the return value of pad when it is called for the first time? -// =============> write your answer here +// =============> The value is 00, add another because every pad wants 2 digits. // 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 last pad call receives 1 as its num because last call called is pad(remainingSeconds) which is calculated as seconds % 60. +// and with the seconds being 61, that's 61 % 60 giving 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 call is pad(1) and the pad adds a 0 to make the string 2 digits