diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..8b1ff082d 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,13 @@ // Predict and explain first... -// =============> write your prediction here +// ===========> I predict there will be an error when the function is called. // 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)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } - -// =============> write your explanation here -// =============> write your new code here +capitalise("hello"); +// ===========> The error happens because str has already been declared as a function parameter. +// ===========> Removed let because str is already declared as a function parameter. diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..dcd511381 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,19 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// ===========> I predict there will be an error because decimalNumber is declared more than once. // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(0.5)); -// =============> write your explanation here +// ===========> The error happens because decimalNumber is already declared as a function parameter. // Finally, correct the code to fix the problem -// =============> write your new code here +// ===========> Removed the duplicate declaration and called the function with 0.5. diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..b3cd2a0e7 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,17 @@ - // Predict and explain first BEFORE you run any code... // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// ===========> I predict there will be a syntax error because 3 cannot be used as a function parameter. -function square(3) { - return num * num; +function square(num) { + return num * num; } -// =============> write the error message here +// ===========> SyntaxError: Unexpected number -// =============> explain this error message here +// ===========> The error happens because a number cannot be used as a function parameter name. // Finally, correct the code to fix the problem -// =============> write your new code here - - +// ===========> Changed 3 to num so the function has a valid parameter name. diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..b7796990d 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,14 @@ // Predict and explain first... -// =============> write your prediction here +// ===========> I predict the multiplication will print 320, but the final result will show undefined. function multiply(a, b) { - console.log(a * b); + return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// ===========> The function prints the result instead of returning it, so the function returns undefined. // Finally, correct the code to fix the problem -// =============> write your new code here +// ===========> Changed console.log to return so the function returns the multiplication result. diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..6d6c87ab7 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,12 @@ // Predict and explain first... -// =============> write your prediction here +// ===========> I predict the result will be undefined because the function returns before adding the numbers. function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// ===========> The function returns before a + b is calculated, so the result is undefined. // Finally, correct the code to fix the problem -// =============> write your new code here +// ===========> Changed the return statement so it returns a + b. diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..9464d3e19 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,11 +1,9 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// ===========> I predict all three results will be 3 because the function always uses num, which is 103. -const num = 103; - -function getLastDigit() { +function getLastDigit(num) { return num.toString().slice(-1); } @@ -15,10 +13,11 @@ 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 -// Explain why the output is the way it is +// ============> The output is 3 for all three numbers. +// ============> The function ignores the numbers passed to it and always uses num, which is 103. // =============> write your explanation here // Finally, correct the code to fix the problem -// =============> write your new code here +// ============> Added num as a parameter so the function uses the number passed to it. // 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-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..5f3087d30 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,6 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height + const bmi = weight / (height * height); + return bmi.toFixed(1); } diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..d4ea8b64c 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,10 @@ // 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) { + const upperCaseString = str.toUpperCase(); + const snakeCaseString = upperCaseString.replaceAll(" ", "_"); + return snakeCaseString; +} +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..74997ebc0 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,12 @@ // 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(pence) { + const penceNumber = Number(pence.replace("p", "")); + const pounds = penceNumber / 100; + return `£${pounds.toFixed(2)}`; +} + +console.log(toPounds("399p")); +console.log(toPounds("5p")); +console.log(toPounds("2500p")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..032be965c 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// ============> 3 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 when it 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 +// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer +// ============> 1, because remainingSeconds is 1. -// e) What is the return value of pad when it is called for the last time in this program? Explain your answer -// =============> write your answer here +// e) What is the return value of pad when it is called for the last time in this program? Explain your answer +// ============> "01", because pad adds a 0 to make the value two characters long. diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..2fc1732bd 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -4,22 +4,25 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(2); + + if (hours === 0) { + return `12${minutes} am`; + } + + if (hours === 12) { + return `12${minutes} pm`; + } + if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2, "0")}${minutes} pm`; } - return `${time} am`; -} -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); + return `${String(hours).padStart(2, "0")}${minutes} am`; +} -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +console.assert(formatAs12HourClock("08:00") === "08:00 am"); +console.assert(formatAs12HourClock("23:00") === "11:00 pm"); +console.assert(formatAs12HourClock("00:00") === "12:00 am"); +console.assert(formatAs12HourClock("12:00") === "12:00 pm"); +console.assert(formatAs12HourClock("13:30") === "01:30 pm");