diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..f6f09544c 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -4,10 +4,18 @@ // 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; } + */ // =============> write your explanation here +// The error is occurring because we are trying to declare a variable with the same name as the function parameter 'str'. This causes a conflict and results in a SyntaxError. To fix this, we can simply remove the 'let' keyword and assign the new value to 'str' directly, since 'str' is already defined as a parameter. + + // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..a2660a450 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,10 +2,11 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// An error will occur because we are trying to declare a variable with the same name as a function parameter. In this case, we are declaring 'decimalNumber' inside the function, which conflicts with the parameter 'decimalNumber'. This will result in a SyntaxError because we cannot have two variables 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}%`; @@ -13,8 +14,15 @@ function convertToPercentage(decimalNumber) { } console.log(decimalNumber); +*/ // =============> write your explanation here +// The error is occurring because we are trying to declare a variable with the same name as a function parameter. In this case, we are declaring 'decimalNumber' inside the function, which conflicts with the parameter 'decimalNumber'. This will result in a SyntaxError because we cannot have two variables with the same name in the same scope. // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..d5e33ad1a 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 +// An error will occur because the function is defined with an invalid parameter name '3'. In JavaScript, parameter names must be valid identifiers, which cannot start with a number. This will result in a SyntaxError when the code is parsed. -function square(3) { +/* function square(3) { return num * num; } + */ // =============> write the error message here +// SyntaxError: Unexpected number '3'. Parameter names must be valid identifiers and cannot start with a number. // =============> explain this error message here +// The error message indicates that the number '3' is unexpected in the context of a parameter name. In JavaScript, parameter names must be valid identifiers, which means they cannot start with a number. To fix this error, we need to change the parameter name to a valid identifier, such as 'num'. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(5)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..d1c5ef735 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// An error will occur because the function 'multiply' does not return any value. When we try to use the result of 'multiply(10, 32)' in the template literal, it will be 'undefined', which is not the expected output. To fix this, we need to add a return statement in the 'multiply' function to return the product of 'a' and 'b'. -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)}`); +*/ // =============> write your explanation here +// The error is occurring because the 'multiply' function does not return any value. When we call 'multiply(10, 32)', it executes the console.log statement inside the function, which prints the product of 'a' and 'b' to the console. However, since there is no return statement, the function returns 'undefined' by default. When we try to use this 'undefined' value in the template literal, it does not give us the expected output. To fix this issue, we need to add a return statement in the 'multiply' function to return the product of 'a' and 'b'. // 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)}`); \ 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..4e8dd443e 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// An error will occur because the 'sum' function does not return any value. When we try to use the result of 'sum(10, 32)' in the template literal, it will be 'undefined', which is not the expected output. To fix this, we need to add a return statement in the 'sum' function to return the sum of 'a' and 'b'. -function sum(a, b) { +/* function sum(a, b) { return; a + b; } + console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +*/ // =============> write your explanation here +// The error is occurring because the 'sum' function does not return any value. When we call 'sum(10, 32)', it executes the return statement without any value, which means the function returns 'undefined' by default. When we try to use this 'undefined' value in the template literal, it does not give us the expected output. To fix this issue, we need to change the return statement to return the sum of 'a' and 'b', like this: 'return a + b;'. // 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)}`); \ 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..aa8a28a24 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,8 +1,14 @@ // Predict and explain first... +// // Predict the output of the following code: // =============> Write your prediction 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 +/* const num = 103; function getLastDigit() { @@ -12,13 +18,25 @@ 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 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 'getLastDigit' function is not using the input parameter to calculate the last digit. Instead, it is always converting the global variable 'num' (which is set to 103) to a string and slicing the last character, which results in '3'. To fix this issue, we need to modify the 'getLastDigit' function to accept a parameter and use that parameter instead of the global variable 'num'. For example: // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(number) { + return number.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..1ab910411 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 + let bmi = weight / (height * height); + return bmi.toFixed(1); +} +console.log(calculateBMI(80, 1.80)); // should return 24.7 \ 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 5b0ef77ad..330ff45d5 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/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")); // should return "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // should return "LORD_OF_THE_RINGS" \ 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..32932f25d 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).toFixed(1); +} +console.log(toPounds(1)); // should return 2.2 +console.log(toPounds(5)); // should return 11 +console.log(toPounds(10)); // should return 22 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..aa6cde8a4 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,6 +11,8 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(61)); + // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit // to help you answer these questions @@ -18,17 +20,22 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// When formatTimeDisplay is called, the pad function will be called three times: once for totalHours, once for remainingMinutes, and once for remainingSeconds. // 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 pad is called for the first time, the value assigned to num is 0, which is the value of totalHours calculated from the input of 61 seconds. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value of pad when called for the first time is "00", because pad(0) returns "00" after padding 0 with leading zeros to make it 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 +// When pad is called for the last time, the value assigned to num is 1, which is the value of remainingSeconds calculated from the input of 61 seconds. // 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 assigned to num when pad is called for the last time in this program is "01", because pad(1) returns "01" after padding 1 with a leading zero to make it 2 digits. \ No newline at end of file diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..cbf208510 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,9 +4,20 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const minutes = time.slice(3, 5); + + if (hours === 0) { + return `12:${minutes} am`; } + + if (hours === 12) { + return `12:${minutes} pm`; + } + + if (hours > 12) { + return `${hours - 12}:${minutes} pm`; +} + return `${time} am`; } @@ -23,3 +34,24 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("12:00"); +const targetOutput3 = "12:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("00:00"); +const targetOutput4 = "12:00 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("13:00"); +const targetOutput5 = "1:00 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +);