diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..25eeb2533 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,26 @@ // Predict and explain first... // =============> write your prediction here +// The code will throw an error because the variable "str" is being declared twice in the same scope. + // 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; -} +//Original code with errors: + +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } // =============> write your explanation here +// The error occurs because the variable "str" is being declared twice in the same scope. This causes a conflict and results in a syntax error. +// To fix this error, we can simply remove the "let" keyword when reassigning the value to "str" inside the function. This way we are not declaring a new variable, but rather reassigning the existing parameter "str". + // =============> write your new code here +// Updated Code: +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} +console.log(capitalise("hello")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..23bd1d8e8 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -3,18 +3,32 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// The program will throw an error because decimalNumber is declared twice in the same function scope. And also decimalNumber is used in console.log(decimalNumber) outside the function, where it doesn't exist. + // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; +// Code with errors: - return percentage; -} - -console.log(decimalNumber); +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5 +//const percentage = `${decimalNumber * 100}%`; +// +// return percentage; +//} +// console.log((decimalNumber)); // =============> write your explanation here +// The error occurs because the variable "decimalNumber" is being declared twice in the same scope. This causes a conflict and results in a syntax error. Additionally, "decimalNumber" is being used in console.log(decimalNumber) outside the function, where it doesn't exist, which will also cause a ReferenceError. + +// To fix this error, i simply removed the second declaration of decimalNumber inside the function and also changed console.log(decimalNumber) to console.log(convertToPercentage(0.5)) to call the function with an argument and log the result. + // 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)); // output: 50% diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..4f0212d82 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,27 @@ - // Predict and explain first BEFORE you run any code... +// The code will throw an error because of an unexpected number "3" in the function parameter. The function parameter must be a variable name, not a number. +// Also, there is no num parameter declared in the function, so even if the function definition worked, it would sill cause a reference error when trying to use "num" in the return statement. // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here -function square(3) { - return num * num; -} +//function square(3) { +// return num * num; +//} // =============> write the error message here +// SyntaxError: Unexpected number in function parameter list. This error occurs because "3" is not a valid parameter name. Parameters should be variable names that can be used within the function body. +//ReferenceError: num is not defined. This error occurs because "num" is being used in the return statement, but it has not been declared or defined anywhere in the function. // =============> explain this error message here +// To fix this error, we need to change the function parameter from "3" to a valid variable name, such as "num". This way, we can pass a number as an argument when calling the function, and it will be used correctly within the function body to calculate the square of that number. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(3)); // output: 9 diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..4819dab47 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 +// The code will throw an error message because multiply only logged the result and didnt return it. So when we try to use the result of multiply(10, 32) in the template literal, it will be undefined, which will cause an error when trying to perform string interpolation with an undefined value. -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 +// To fix this error, we need to change the multiply function to return the result of the multiplication instead of just logging it. This way, when we call multiply(10, 32) inside the template literal, it will return the correct value (320) that can be used in the string interpolation without causing an error. // 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)}`); // output: 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..106a59b7d 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 +// The code will throw an error message because the function "sum" has a return statement that is not returning any value. -function sum(a, b) { - return; - a + b; -} +//function sum(a, b) { +// return; +// a + b; +//} -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +//console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The function "sum" is not returning any value. +// To fix the error, we need to change the return statement to return the result of the addition of "a" and "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)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..03005dd7b 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,6 +3,8 @@ // Predict the output of the following code: // =============> Write your prediction here +//I predict that it will print out the wrong last digit of all numbers. + const num = 103; function getLastDigit() { @@ -15,10 +17,28 @@ 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 + +// The output is the way it is because the function "getLastDigit" is using a global variable "num" which is set to 103. So, regardless of the input passed to the function, it will always return the last digit of 103, which is 3. + // 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" is not working properly because it is using a global variable "num" instead of accepting and using a function. Adding `num`as a parameter makes the function work with any number passed in. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..c8d224e6a 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,6 @@ // 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 + return (weight / (height * height)).toFixed(1); +} diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..77c1676e6 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,7 @@ // 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.replaceAll(" ", "_").toUpperCase(); +} diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..0bef4ecf6 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}`; +} + +// Call the function a number of times to check it works +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("45p")); // £0.45 +console.log(toPounds("8p")); // £0.08 +console.log(toPounds("1200p")); // £12.00 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..e6b3a929a 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -19,16 +19,26 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// pad will be called 3 times because it is used for minutes, hours and seconds in the return string. + // 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, The first call is pad(totalHours),and for 61 seconds, totalHours will be 0 because 61 seconds is less than 1 hour. + // c) What is the return value of pad is called for the first time? // =============> write your answer here +// 00, pad(0) converts 0 to "0" and padStart(2, "0") makes it "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, the last call is pad(remainingSeconds), and for 61 seconds, remainingSeconds = 61 % 60 = 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, the last call is pad(1). pad converts 1 to "1", then padStart(2, "0") adds a leading zero, so it returns "01" diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..342b81dbb 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,24 +2,56 @@ // Make sure to do the prep before you do the coursework // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. -function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; - } - return `${time} am`; -} - -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +const currentOutput4 = formatAs12HourClock("12:00"); +const targetOutput4 = "12:00 pm"; console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` ); + +const currentOutput5 = formatAs12HourClock("00:45"); +const targetOutput5 = "12:45 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + +const currentOutput6 = formatAs12HourClock("12:30"); +const targetOutput6 = "12:30 pm"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); + +const currentOutput7 = formatAs12HourClock("13:05"); +const targetOutput7 = "01:05 pm"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}` +); + +// I tested the function with edge cases around midnight and noon. +// I found that the original function handled times like "08:00" and "23:00", +// but it did not correctly handle "00:xx" (midnight times) or "12:xx" (noon times). + +// Bugs found: +// - "00:00" returned "00:00 am" instead of "12:00 am" +// - "00:45" returned "00:45 am" instead of "12:45 am" +// - "12:00" returned "12:00 am" instead of "12:00 pm" + +// Fix: +// I added separate conditions for: +// - hours === 0 (midnight -> 12:xx am) +// - hours === 12 (noon -> 12:xx pm) +// - hours > 12 (convert to pm by subtracting 12) +// I also kept the minutes using time.slice(2), so the ":MM" part stays the same. + +// After fixing, the function correctly formats morning, noon, afternoon, +// evening, and midnight times in 12-hour clock format.