diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..e2afcf5aa6 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -5,7 +5,7 @@ // 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; } diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..21beb421ef 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -5,14 +5,13 @@ // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; +function convertToPercentage(decimalNumber) { const percentage = `${decimalNumber * 100}%`; return percentage; } - -console.log(decimalNumber); +const result = convertToPercentage(0.5); +console.log(result); // =============> write your explanation here diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..c6d0fd577f 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -5,9 +5,12 @@ // =============> write your prediction of the error here -function square(3) { +function square(num) { return num * num; } +const result = square(3) +console.log(result); + // =============> write the error message here diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..4d6d7bc727 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -3,7 +3,7 @@ // =============> write your prediction here function multiply(a, b) { - console.log(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..b11443f14c 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -2,8 +2,7 @@ // =============> write your prediction here function sum(a, b) { - return; - 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..6d3b7cd76a 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -5,8 +5,8 @@ const num = 103; -function getLastDigit() { - return num.toString().slice(-1); +function getLastDigit(numbertoconvert) { + return numbertoconvert.toString().slice(-1); } console.log(`The last digit of 42 is ${getLastDigit(42)}`); diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..c934f1940f 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,10 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + const bmi = weight / (height * height); + return Number(bmi.toFixed(1)); // return the BMI of someone based off their weight and height -} \ No newline at end of file +} + +console.log(calculateBMI(70, 1.73)); +console.log(calculateBMI(85, 1.80)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..92e98f4b63 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,11 @@ // 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){ +let wordWithUnderscores = str.replaceAll(" ", "_"); + +let finalResult = wordWithUnderscores.toUpperCase(); +return finalResult; +} +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..55481a9e57 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,32 @@ // 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); + + + return `£${pounds}.${pence}`; +} + + +console.log(toPounds("399p")); +console.log(toPounds("50p")); +console.log(toPounds("1005p")); +console.log(toPounds("1p")); \ 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 7c98eb0e8c..53139567cc 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,19 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> 3 times .In the last line of the formatTimeDisplay function, you can see the word pad written three 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 The last time the program calls pad, it is looking for the remainingSeconds. When you have 61 seconds, 60 of them make a full minute, and you have 1 second left over. That leftover 1 is what gets sent to num +// // 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 function takes the number 1. Because the code says padStart(2, "0"), it says: "Hey, this needs to be 2 characters long!". So, it puts a 0 in front of the 1, making it "01". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..e8ea3f9c82 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,22 +4,34 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const minute = time.slice(3, 5); + + let period = "am"; + let displayHours = hours; + + if (hours === 12) { + period = "pm"; + } else if (hours > 12) { + displayHours = hours - 12; + period = "pm"; + } else if (hours === 0) { + // 3. Handle Midnight + displayHours = 12; } - return `${time} am`; + + +const formattedHours = displayHours.toString().padStart(2, "0"); + + return `${formattedHours}:${minutes} ${period}`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); - -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); + +console.assert(formatAs12HourClock("08:30") === "08:30 am", "Error at 08:30"); + +console.assert(formatAs12HourClock("23:45") === "11:45 pm", "Error at 23:45"); + +console.assert(formatAs12HourClock("12:00") === "12:00 pm", "Error at 12:00"); + +console.assert(formatAs12HourClock("00:15") === "12:15 am", "Error at 00:15"); + +console.log("If you see no error messages above, all tests passed!"); \ No newline at end of file