diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..d873760596 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -9,5 +9,10 @@ function capitalise(str) { return str; } -// =============> write your explanation here -// =============> write your new code here +// The error is on line 8. +// The parameter 'str' has already been declared in the function. +// Declaring 'let str' again causes the error. +function capitalise(str) { + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..4cab3ce025 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -14,7 +14,13 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// The parameter decimalNumber is already declared. +// Declaring const decimalNumber again causes the error. // 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..26f5e7fc82 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,21 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// function parameter should not be a number, it should be a variable name that holds a number. +// the parameter is a placeholder for the number. -function square(3) { +/*function square(3) { return num * num; -} +}*/ -// =============> write the error message here +// SyntaxError: Unexpected number -// =============> explain this error message here +// The error occurs because a function parameter must be a variable name, not a number. // 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..5a3d9b0553 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,19 @@ // Predict and explain first... -// =============> write your prediction here +// I predict this will show 'undefined' because the function doesn't return a value. +/* 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 function doesn't return a value, so the template literal displays 'undefined'. // 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)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..23ef4d9455 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,17 @@ // Predict and explain first... -// =============> write your prediction here - +// I predict this will show 'undefined' because the function doesn't return a value. +/* function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); - -// =============> write your explanation here +*/ +// The function returns 'undefined', so the template literal displays 'undefined'. // 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 57d3f5dc35..b17bee77e8 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,8 +1,8 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here - +// the last digit of any number will always be 3 becuse of the const num = 103; +/* const num = 103; function getLastDigit() { @@ -12,13 +12,22 @@ 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 +// =============> output is as predicted, the last digit of any number will always be 3 because of the const num = 103; // Explain why the output is the way it is -// =============> write your explanation here +// =============> // num is always 103, so the function always returns 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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..3f2f8643f4 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,5 @@ // 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 + return (weight / (height * height)).toFixed(1); } \ 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..b922960d83 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, '_'); +} \ 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..5f37b15d24 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,28 @@ // 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}`; +} + +console.log(toPounds("399p")); +console.log(toPounds("40p")); +console.log(toPounds("5p")); +console.log(toPounds("1234p")); \ 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 17127bc01e..3b8cc08302 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -21,18 +21,19 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// pad() is called 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 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 +// =============> 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 +// =============> The return value of pad when it is called for the last time is "01". +// This is because the value of num is 1, and we are padding it with zeros to make it 2 characters long. diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..16ec3407bb 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,9 +4,19 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + + if (hours === 0) { + return `12:00 am`; + } + + if (hours === 12) { + return `12:00 pm`; + } + if (hours > 12) { return `${hours - 12}:00 pm`; } + return `${time} am`; } @@ -23,3 +33,18 @@ 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}` +); \ No newline at end of file