diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..78852574c 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,8 +1,12 @@ // Predict and explain first... // =============> write your prediction here +// The command is to capitalise the first letter of the string. // call the function capitalise with a string input -// interpret the error message and figure out why an error is occurring +// The code will produce an error before the function runs properly. The error message will say, SyntaxError: Identifier 'str' has already been declared + +// interpret the error message and figure out why an error is occurring. +// The error is occurring because the variable str is being declared twice in the function. The first declaration is in the function parameter, and the second declaration is inside the function body. This causes a conflict because you cannot declare a variable with the same name in the same scope. function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; @@ -10,4 +14,12 @@ function capitalise(str) { } // =============> write your explanation here +// The function capitalise takes a string input and attempts to capitalise the first letter of the string. However, the variable str is declared twice, which causes a SyntaxError. To fix this, we can remove the second declaration of str inside the function body and directly return the capitalised string. // =============> write your new code here +// The corrected function should look like this: + +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} + +// Now the function should work correctly and capitalise the first letter of the input string. \ 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 f2d56151f..1bbe48b1e 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,9 +1,12 @@ // Predict and explain first... // Why will an error occur when this program runs? +// The error occurs because the variable decimalNumber is declared twice in the function convertToPercentage. The first declaration is in the function parameter, and the second declaration is inside the function body. This causes a conflict because you cannot declare a variable with the same name in the same scope. // =============> write your prediction here +// The command is to convert a decimal number to a percentage. // Try playing computer with the example to work out what is going on +// function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; @@ -15,6 +18,16 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// The function convertToPercentage takes a decimal number as input and attempts to convert it to a percentage. However, the variable decimalNumber is declared twice, which causes a SyntaxError. To fix this, we can remove the second declaration of decimalNumber inside the function body and directly return the calculated percentage. // Finally, correct the code to fix the problem // =============> write your new code here +// The corrected function should look like this: + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +// Now the function should work correctly and convert a decimal number to a percentage. diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..904bb65f5 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,25 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// The error occurs because the parameter name '3' is not a valid identifier in JavaScript. Variable and parameter names cannot start with a number. This will cause a SyntaxError when the code is parsed. function square(3) { return num * num; } // =============> write the error message here +// The error message will be: SyntaxError: Unexpected number '3'. This indicates that the parser encountered an unexpected token, which is the number '3' used as a parameter name. // =============> explain this error message here +// The error message indicates that the parser is expecting a valid identifier for the parameter name, but it encountered a number instead. In JavaScript, variable and parameter names must start with a letter, underscore, or dollar sign, and cannot start with a number. Therefore, using '3' as a parameter name is not allowed and results in a SyntaxError. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} + +// Now the function should work correctly and return the square of the input number. diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..285831fd6 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 +// The command is to multiply two numbers and print the result. However, the function multiply does not return any value, it only logs the product to the console. Therefore, when we try to use the result of multiply in a template literal, it will be undefined, which will lead to an incorrect output. function multiply(a, b) { console.log(a * b); @@ -9,6 +10,14 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// The function multiply takes two parameters, a and b, and logs their product to the console. However, it does not return any value, which means that when we try to use the result of multiply in the template literal, it will be undefined. This will lead to the output: "The result of multiplying 10 and 32 is undefined". To fix this, we need to modify the multiply function to return the product 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; +} + +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + +// Now the function multiply returns the product of a and b, and the output will be: "The result of multiplying 10 and 32 is 320". diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..3dfccc61c 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 command is to calculate the sum of two numbers and print the result. However, the function sum does not return any value, it only has a return statement without an expression. Therefore, when we try to use the result of sum in a template literal, it will be undefined, which will lead to an incorrect output. function sum(a, b) { return; @@ -9,5 +10,13 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The function sum takes two parameters, a and b, and has a return statement without an expression. This means that the function will return undefined when called. When we try to use the result of sum in the template literal, it will be undefined, which will lead to the output: "The sum of 10 and 32 is undefined". To fix this, we need to modify the sum function to return the actual sum of a and b instead of just having a return statement without an expression. // 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)}`); + +// Now the function sum returns the actual sum of a and b, and the output will be: "The sum of 10 and 32 is 42". diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..d048439da 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,6 +2,7 @@ // Predict the output of the following code: // =============> Write your prediction here +// The code will output: "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3". This is because the function getLastDigit is using the variable num, which is set to 103, instead of the parameter passed to the function. Therefore, it will always return the last digit of 103, which is 3. const num = 103; @@ -15,10 +16,29 @@ 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 is: +// 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 output is the way it is because the function getLastDigit is not using the parameter passed to it. Instead, it is using the variable num, which is set to 103. Therefore, regardless of the input, the function will always return the last digit of 103, which is 3. To fix this issue, we need to modify the function to use the parameter instead of the variable num. // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(num) { + return num.toString().slice(-1); +} + +// Now the function getLastDigit will return the last digit of the number passed as an argument, and the output will be: +// The last digit of 42 is 2 +// The last digit of 105 is 5 +// The last digit of 806 is 6 // 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 is using a global variable num instead of the parameter passed to the function. To correct this problem, we need to change the function definition to accept a parameter and use that parameter to calculate the last digit. The corrected function should look like this: +function getLastDigit(num) { + return num.toString().slice(-1); +} + +// Now the function will work correctly and return the last digit of the input number. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..a96b16a7e 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,7 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + const bmi = weight / (height * height); + return Math.round(bmi * 10) / 10; +} + diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..8753f385d 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,14 @@ // 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 the string in UPPER_SNAKE_CASE + return str.toUpperCase().replace(/ /g, '_'); +} + +// The function toUpperSnakeCase takes a string input and returns the string in UPPER_SNAKE_CASE. It first converts the string to uppercase using the toUpperCase() method, and then replaces all spaces with underscores using the replace() method with a regular expression. + +// Now we can test the function with different inputs +console.log(toUpperSnakeCase("hello there")); // Output: "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // Output: "LORD_OF_THE_RINGS" +console.log(toUpperSnakeCase("javascript is great")); // Output: "JAVASCRIPT_IS_GREAT" diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..bbfbc0d5a 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,10 @@ // 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; +} + +console.log(toPounds(1)); // Output: 2.20462 +console.log(toPounds(10)); // Output: 22.0462 +console.log(toPounds(100)); // Output: 220.462 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..e82addd74 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 +// When formatTimeDisplay is called, pad will be called three times. This is because pad is called for totalHours, remainingMinutes, and remainingSeconds in the return statement of the formatTimeDisplay function. // 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 is called with an input of 61, the value assigned to num when pad is called for the first time will be 0. This is because totalHours will be calculated as (totalMinutes - remainingMinutes) / 60, which will be (1 - 1) / 60 = 0. Therefore, pad(0) will be called for the first time. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value of pad when it is called for the first time with num equal to 0 will be "00". This is because pad uses the padStart method to convert the number to a string and pads it with leading zeros until it reaches a length of 2. Therefore, pad(0) will return "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 is called with an input of 61, the value assigned to num when pad is called for the last time will be 1. This is because remainingSeconds will be calculated as seconds % 60, which will be 61 % 60 = 1. Therefore, pad(1) will be called for the last time in this program. // 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 when it is called for the last time with num equal to 1 will be "01". This is because pad uses the padStart method to convert the number to a string and pads it with leading zeros until it reaches a length of 2. Therefore, pad(1) will return "01".