diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..18e0fd312 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 +// Answer +// If I call the function capitalise with a string input, I predict that it will return an error because the variable str +// has already been declared as a paramter of the function so it can not be re-declared // 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; +//} + +// =============> write your explanation here +//Answer +// The error message "SyntaxError: Identifier 'str' has already been declared" means that the identifier "str" has been declared and so can not be re-declared. +// This error occured because the same variable name occurs as a function parameter and is then redeclared using a let assignment in a function body again. +// Redeclaring the same variable within the same function or block scope using let is not allowed in JavaScript. + +// =============> write your new code here +// Answer function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } -// =============> write your explanation here -// =============> write your new code here +let actualOutput = capitalise("welcome"); +console.log(actualOutput); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..11f80370a 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,53 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// Prediction +// An error will occur when the program runs because a variable cannot be redeclared. +// The parameter "decimalNumber" is already a declared variable, so it cannot be redeclared again using const decimalNumber = 0.5; inside the same 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 +// Explanation +//function convertToPercentage(decimalNumber) { + //const decimalNumber = 0.5; + //const percentage = `${decimalNumber * 100}%`; + + //return percentage; +//} + +// When the script starts, JavaScript defines the function convertToPercentage, but it doesn’t run it yet. +// This means that nothing is executed inside the function; only the function itself is stored in memory. + +// console.log(decimalNumber); +// At this point, JavaScript looks for a variable called decimalNumber in the current scope (the global scope), but none exists. +// We only declared decimalNumber as a parameter inside the function (which hasn’t been called yet). +// Result: Because decimalNumber isn’t defined globally, JavaScript will throw an error +// At this point, JavaScript looks for a variable called decimalNumber in the current scope (the global scope). +// But none exists — we only declared decimalNumber as a parameter inside the function (which hasn’t been called yet). +// Error message: "SyntaxError: Identifier 'decimalNumber' has already been declared" // Finally, correct the code to fix the problem // =============> write your new code here + +decimalNumber = 0.5; +function convertToPercentage(decimalNumber) { + + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +const actualOutput = convertToPercentage(15); +console.log(actualOutput); +console.log(decimalNumber); \ 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..fb135c845 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,29 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// When the code is being read or parsed by Node, it will throw a syntax error because it cannot interpret the number 3 inside the parentheses. +// This is because a number (3) is not a valid parameter name in a function definition. -function square(3) { - return num * num; -} -// =============> write the error message here +//function square(3) { + // return num * num; +//} +// =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// This error message indicates that JavaScript only allows identifiers as parameters in a function definition. +// If a literal value (such as a number, string, or object) is used instead, a SyntaxError will be thrown. + // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} + +const actualOutput = square(3); +console.log(actualOutput); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..ed50ddabe 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,37 @@ // Predict and explain first... // =============> write your prediction here +// Answer +// If this program runs, I predict that the function will return undefined because it has no return statement. +// Also, the console.log inside the multiply function will print its output to the terminal. +// Lastly, because the function returns undefined, when ${multiply(10, 32)} is used inside a template literal inside the console.log function, it will print undefined in that position. -function multiply(a, b) { - console.log(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 +// Explanation +// function multiply(a, b) {} +// This expression defines a function named multiply. +// The function takes two parameters: a and b. + +// console.log(a * b); +// This expression multipies a and b and prints the result in the terminal. +// There is no return statement, so the function implicitly returns undefined. + +// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +// In this expression, multiply(10, 32) is called. +// Because the function returns undefined, the template literal becomes: "The result of multiplying 10 and 32 is undefined" +// That string is printed to the console. // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + console.log(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..dfee55efa 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,46 @@ // Predict and explain first... // =============> write your prediction here +// Answer +// I predict that calling sum(10, 32) will return `undefined`. +// This is because the `return` statement has a semicolon immediately after it, +// so JavaScript interprets it as `return;` which evaluate to "undefined" when the function is called +// The expression `a + b` on the next line will never be executed. +// Therefore, any console.log using sum(10, 32) will display `undefined` in the output. -function sum(a, b) { - return; - a + b; -} -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +//function sum(a, b) { + //return; + //a + b; +//} + +//console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// Explanation +// function sum(a, b) { +// This declares a function named sum that takes two parameters: a and b. + +// return; +// The return statement is immediately followed by a semicolon. +// This causes the function to exit immediately. +// Since nothing is returned explicitly, JavaScript returns undefined by default. + +// a + b; +// This line never executes because the function has already exited at the `return;` line. + +// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// In this expression, sum(10, 32) is called. +// Because the function returns undefined, the template literal becomes: "The sum of 10 and 32 is undefined" +// That string is printed to the console. + // Finally, correct the code to fix the problem +// I will fix the error by placing the expression a + b on the same line as the return statement. +// Optionally, I can use parentheses around the expression, but this is not required. + // =============> 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..672556b8a 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,43 @@ // Predict the output of the following code: // =============> Write your prediction here +// Prediction +// When the function is called, I predict that the output will always be 3. +// This is because the variable 'num' is defined outside the function and is always 103. +// Therefore, regardless of the argument passed to getLastDigit, it will always return the last digit of 103, which is 3. -const num = 103; + // const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// 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 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 +// Output +// 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 +// Explanation +// The output will always be 3 because the function getLastDigit does not have a parameter so can not accept any arguments. + // Finally, correct the code to fix the problem // =============> write your new code here +// New code +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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..92c82f20a 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,11 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + // return the BMI of someone based off their weight and height + let bmi = weight / (height * height); + bmi = Number(bmi.toFixed(1)); + return bmi; +} + +let actualOutput = calculateBMI(70, 1.73); +console.log(actualOutput); // 23.4 diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..7d7051cec 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,16 @@ // 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 convertToUpperSnakeCase(inputString) { + // we need to find everywhere the character space is and replace it with an underscore + // we need to convert the string from lowercase to uppercase + const oldCharacterSpace = " "; + const newCharacterUnderscore = "_"; + const findspaceAndReplace = inputString.replaceAll(oldCharacterSpace, newCharacterUnderscore); + const convertToUpperCase = findspaceAndReplace.toUpperCase(); + return convertToUpperCase; +} + +const actualOutput = convertToUpperSnakeCase("how are you Tolu"); +console.log(actualOutput); \ 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..7d4c52b7f 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,29 @@ // 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(`£${pounds}.${pence}`); +} + +console.log(toPounds("399p")); +console.log(toPounds("5p")); +console.log(toPounds("50p")); +console.log(toPounds("1234p")); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..84b672af5 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,22 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// Three 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 +// 0 // c) What is the return value of pad 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 // 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 +// "01"