diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..dfdb6336be 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 +// I predict that the error will be a SyntaxError because we are trying to declare a variable with the same name as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same function scope. This will cause a SyntaxError because it creates a conflict in variable naming. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +11,11 @@ function capitalise(str) { } // =============> write your explanation here +// The original error happened because we tried to declare a new variable +// named `str` with `let` even though `str` was already a parameter. That's +// illegal in JavaScript and produces a SyntaxError. By removing the extra +// declaration (or by returning the string directly) the function works. // =============> write your new code here +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..a9f8182442 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,7 @@ // Predict and explain first... - // Why will an error occur when this program runs? // =============> write your prediction here +// I predict that the error will be a SyntaxError because we are trying to declare a variable with the same name as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same function scope. This will cause a SyntaxError because it creates a conflict in variable naming. // Try playing computer with the example to work out what is going on @@ -18,3 +18,10 @@ console.log(decimalNumber); // 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.5)); + diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..fb4b362e88 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,22 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// I predict that the error will be a SyntaxError because we are trying to declare a function with an invalid name. In JavaScript, function names cannot start with a number. By trying to declare a function named `square(3)`, we are violating this rule, which will result in a SyntaxError when the code is parsed. + function square(3) { return num * num; } // =============> write the error message here - +// SyntaxError due to an unexpected token or invalid function name. // =============> explain this error message here - +// This is because `square(3)` is not a valid function declaration in JavaScript, and the parser will not be able to understand it as a function definition. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..a13b167c98 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// I predict that the error will be a ReferenceError because the function `multiply` does not return any value, so when we try to use it inside the template literal, it will return `undefined`. This will cause the output to be "The result of multiplying 10 and 32 is undefined" instead of the expected product of 10 and 32. function multiply(a, b) { console.log(a * b); @@ -9,6 +10,9 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here - +// The error occurs because the `multiply` function does not return a value; it only logs the result to the console. When we use `multiply(10, 32)` inside the template literal, it evaluates to `undefined` since there is no return statement in the function. To fix this, we need to add a return statement to the `multiply` function so that it returns the product of `a` and `b` instead of just logging it. // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + return a * b; +} \ 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 37cedfbcfd..01765c6124 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,6 @@ // Predict and explain first... // =============> write your prediction here - +// I predict that the error will be a ReferenceError because the function `sum` does not return any value, so when we try to use it inside the template literal, it will return `undefined`. This will cause the output to be "The sum of 10 and 32 is undefined" instead of the expected sum of 10 and 32. function sum(a, b) { return; a + b; @@ -9,5 +9,9 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The error occurs because the `sum` function does not return a value; it only has a return statement without any expression. When we use `sum(10, 32)` inside the template literal, it evaluates to `undefined` since there is no return value from the function. To fix this, we need to return the result of `a + b` instead of just having a return statement with no value. // Finally, correct the code to fix the problem // =============> write your new code here +function sum(a, b) { + return a + b; +} \ 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 57d3f5dc35..cfc29b9aa6 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,8 +1,11 @@ // Predict and explain first... - +// this function should square any number but instead we're going to get an error // Predict the output of the following code: // =============> Write your prediction here - +// I predict that the output will be: +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 const num = 103; function getLastDigit() { @@ -15,10 +18,19 @@ 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 output will be: +// 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 +// The function `getLastDigit` is not taking any parameters, so it always returns the last digit of the global variable `num` (which is 103). This means that regardless of what number is passed to the function, it will always return 3. // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(num) { + return num.toString().slice(-1); +} // 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` is not working properly because it does not take any parameters, and it always returns the last digit of the global variable `num`. To fix this, we need to modify the function to accept a parameter (the number we want to find the last digit of) and use that parameter instead of the global variable. This way, we can get the correct last digit for each number passed to the function. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..28ec7794b4 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,7 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + const bmi = weight / (height * height); + return Math.round(bmi * 10) / 10; // return the BMI of someone based off their weight and height } \ 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..d367765766 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 toUpperSnakeCase(str) { + return str.toUpperCase().replace(/ /g, '_'); + // return the string in UPPER_SNAKE_CASE +} \ 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..6375a4bea6 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,8 @@ // 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(kilograms) { + return kilograms * 2.20462; + // return the weight in pounds +} \ No newline at end of file diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8c..5908240970 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,21 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// The `pad` function will be called three times when `formatTimeDisplay` is called, once for each of the time components: hours, minutes, and seconds. Each component is passed to the `pad` function to ensure it is displayed with at least two digits, adding a leading zero if necessary. // 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 +// When `formatTimeDisplay(61)` is called, the first time `pad` is called, `num` is assigned the value of `totalHours`, which 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(0)` is "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 - +// When `formatTimeDisplay(61)` is called, the last time `pad` is called, `num` is assigned the value of `remainingSeconds`, which is 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 return value of `pad(1)` is "01". \ No newline at end of file