diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..17b65d00b 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,20 @@ // Predict and explain first... -// =============> write your prediction here +// =============> If the function will be called, it will print the first letter of the string +// in capital with the rest of the word in lowercase // 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; +} */ + +// =============> A SyntaxError was thrown because the str was used to name both the function parameter +// and a new variable, which cannot happen in the same scope +// =============> +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; } -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("mother")); diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..8e4a749fd 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,27 @@ // Predict and explain first... +// There will be another syntax error, because the same name is used for both the parameter and the variable +// (decimalNumber) // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> +// We cannot redeclare a variable with the same name in the same scope. // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { +/* function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } - -console.log(decimalNumber); - +*/ // =============> write your explanation here // 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.8)); diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..fed0d1a76 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,23 @@ - // Predict and explain first BEFORE you run any code... // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// I predict it will say 'num is not defined' -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } // =============> write the error message here - +// SyntaxError: Unexpected number // =============> explain this error message here - +// Instead of a parameter name, the actual value was passed, resulting in an error; // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(6)); diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..b675e6293 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,21 @@ // Predict and explain first... -// =============> write your prediction here +// =============> +// Variables haven't been declared, so it will throw a reference error -function multiply(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 - +// It printed 32 because that particular console function is inside the multiply function (local), whereas the other one +// is outside the scope and when we call the multiply function it comes up as undefined because nothing has been returned +// inside of 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)}`); diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..eb918729b 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,21 @@ // Predict and explain first... // =============> write your prediction here +// it will print the following: `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 +// the function sum doesn't return any value. The arithmetic operation doesn't execute because it's defined after 'return' // 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)}`); diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..fad01d643 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,8 +2,8 @@ // Predict the output of the following code: // =============> Write your prediction here - -const num = 103; +// it will not print the correct last digits +/*const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -12,13 +12,29 @@ 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 method uses the num value which is 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 +// The function getLastDigit wasn't using intended parameters diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..17f01474c 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,9 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + let bmi = weight / (height * height); + return bmi.toFixed(1); + // return the BMI of someone based off their weight and height } +console.log(`Your Bmi is ${calculateBMI(56, 1.66)}`); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..97afe4bc9 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,12 @@ // 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 upperSnakeCaseMaker(str) { + let upperCased = str.toUpperCase(); + let symbol = "_"; + let underscored = upperCased.split(" ").join(symbol); + return underscored; +} + +console.log(upperSnakeCaseMaker("the sun is shining")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..02235535b 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,28 @@ // 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}`; +} + +// results of some different inputs: 0p -> £0.00, 3p -> £0.03, 50p -> £0.50, 170p -> £1.70, 1200p -> £12.00, 11000p -> £110.00 +console.log(toPounds("0p")); +console.log(toPounds("3p")); +console.log(toPounds("50p")); +console.log(toPounds("170p")); +console.log(toPounds("1200p")); +console.log(toPounds("11000p")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..158cf6a82 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/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 +// =============> 3 // 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 +// =============> 0 // c) What is the return value of pad when it is called for the first time? -// =============> 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 +// =============> 1, because that's the actual value assigned to the num parameter (remainingSeconds is 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 +// =============> "01", the remainingSeconds is 1, which gets padded to "01" in the return value