diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..92041895b 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here - It will print 320 inside the function, but the template string will show "undefined" because multiply does not return a value. function multiply(a, b) { console.log(a * b); @@ -8,7 +8,11 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here - multiply() uses console.log to display the result, but it doesn't return anything. In JavaScript, a function with no return statement returns undefined, so ${multiply(10, 32)} becomes undefined even though 320 was logged earlier. // Finally, correct the code to fix the problem // =============> write your new code here +function multiplyFixed(a, b) { + return a * b; +} +console.log(`The result of multiplying 10 and 32 is ${multiplyFixed(10, 32)}`); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..30b260162 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,5 @@ // Predict and explain first... -// =============> write your prediction here - +// =============> write your prediction here - should show undefined as there are ";" after return inside the function function sum(a, b) { return; a + b; @@ -10,4 +9,9 @@ console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> write your new code here - you can't divide the return parameters with ";" +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..3084ea0ac 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,7 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here - it should print last digit of 103 const num = 103; @@ -14,11 +14,22 @@ 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 +// =============> write the output here - we have set num as constant value 103, inside the function we use it as it is constant, ignoring other inputs +// 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 +// =============> write your explanation here - // 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 +// It wasn't working because it used the outer variable num instead of the input value diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..1aa6b12c2 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,8 @@ // 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 + const bmi = weight / (height * height); + return Number(bmi.toFixed(1)); +} + +console.log(calculateBMI(70, 1.73)); \ 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..9c8008ce5 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 upperSnake(string) +{ + return string.trim().split(" ").join("_").toUpperCase() +} +console.log(upperSnake("hello there")) \ 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..77f5c6c0e 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,31 @@ // 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(str) +{ +// 1. const penceString = "399p": initialises a string variable with the value "399p" +const penceStringWithoutTrailingP = str.substring( + 0, + str.length - 1 +); + +//2. removes "P" from the string +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +//3. Ensures the string is at least 3 characters long by adding 0 to the start +const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +); + +//4. Extracts everything except the last 2 digits of paddedPenceNumberString +const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + return pence +} +//5. takes the last 2 digits as the pence from paddedPenceNumberStringCollapse comment +console.log(toPounds("399p")) +console.log(toPounds("400p")) +console.log(toPounds("301p")) +console.log(toPounds("302p")) \ 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 7c98eb0e8..271635131 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -16,19 +16,19 @@ function formatTimeDisplay(seconds) { // Questions -// a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// 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 +// b) What is the value assigned to num when pad is called for the first time? - +// =============> write your answer here - First call is pad(totalHours) // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here - it returs 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 +// =============> write your answer here - Last call is pad(remainingSeconds) For 61, remainingSeconds = 61 % 60 = 1, so num is 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 +// =============> write your answer here - With num = 1, pad(1) returns "01". "1".padStart(2, "0") adds zero 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 32a32e66b..392c20f16 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -23,3 +23,17 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +console.log(formatAs12HourClock("23:14")) +console.log(formatAs12HourClock("00:00")) +console.log(formatAs12HourClock("00:01")) +console.log(formatAs12HourClock("11:59")) +console.log(formatAs12HourClock("12:00")) +console.log(formatAs12HourClock("12:01")) +console.log(formatAs12HourClock("13:45")) + + + + + +