diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..14657ef5f 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,6 +1,10 @@ // Predict and explain first... // =============> write your prediction here +// I predict that the function will capitalise the first character of the string +// And then return a string using string interpolation using the first character of the string +// and a slice of the remaining characters of the string excluding the first character. + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +14,13 @@ function capitalise(str) { } // =============> write your explanation here + +// An error is thrown because of the variable name collision. The function has a parameter called `str` +// but in the body of the function, a variable `str` is declared and that conflicts with the parameter. + // =============> write your new code here + +function upperCased(string) { + const capitalised = `${string[0].toUpperCase()}${string.slice(1)}`; + return capitalised; +} \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..ec4b3e97b 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -3,6 +3,9 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// Because of the variable name collision. The function parameter is called decimalNumber +// but another decimalNumber is declared inside the body of the function. + // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -16,5 +19,11 @@ console.log(decimalNumber); // =============> write your explanation here +// The console prints: SyntaxError: Identifier 'decimalNumber' has already been declared + // Finally, correct the code to fix the problem // =============> write your new code here + +function percentaged(decimalNumber) { + return `${decimalNumber * 100}%`; +} diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..46078e1af 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -5,16 +5,26 @@ // =============> write your prediction of the error here +// The function will not execute because of the undefined variable `num` and the parameter `3`, +// which is not allowed to be defined with just numbers. There must be letters to define a variable +// but can begins with numbers. + function square(3) { return num * num; } // =============> write the error message here +// SyntaxError: Unexpected number + // =============> explain this error message here +// It's referring to the function parameter `3` + // Finally, correct the code to fix the problem // =============> write your new code here - +function squared(value) { + return value * value; +} \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..a513b1479 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -2,6 +2,8 @@ // =============> write your prediction here +// The function prints to the console by multiplying the parameter `a` and `b` + function multiply(a, b) { console.log(a * b); } @@ -10,5 +12,12 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// I missed line 11, the function does indeed prints to the console but returns nothing +// which explains `undefined` in the string interpolation of the function + // Finally, correct the code to fix the problem // =============> write your new code here + +function product(a, b) { + return a * b; +} diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..16cb07d0b 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,8 @@ // Predict and explain first... // =============> write your prediction here +// It will not say `undefined` but will not add parameter `a` and `b` + function sum(a, b) { return; a + b; @@ -9,5 +11,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here + +// The console prints: The sum of 10 and 32 is undefined + // Finally, correct the code to fix the problem // =============> write your new code here + +function add(a, b) { + return a + b; +} \ 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..6b1bc64a8 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,10 +3,12 @@ // Predict the output of the following code: // =============> Write your prediction here +// It will convert 103 number into a String representation and then will either do no manipulations or invert the string + const num = 103; -function getLastDigit() { - return num.toString().slice(-1); +function getLastDigit(digit) { + return digit.toString().slice(-1); } console.log(`The last digit of 42 is ${getLastDigit(42)}`); @@ -22,3 +24,7 @@ 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() has no parameters and referenced a globally scoped constant `num`. +// In order for it to `work properly`, a parameter is required and referencing it in order to +// return an expected value \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..60392f813 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 + // return the BMI of someone based off their weight and height + const result = weight / Math.pow(height, 2); + const bmiString = result.toFixed(1); + return Number(bmiString); +} diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..7e2ec7fa5 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(string) { + return String(string).toUpperCase().replaceAll(" ", "_"); +} diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..4e4847d90 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,23 @@ // 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}`; +} diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..99829ccad 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,6 +11,9 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +const number = 61; +console.log(formatTimeDisplay(number)); + // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit // to help you answer these questions @@ -19,16 +22,26 @@ function formatTimeDisplay(seconds) { // 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 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 assigned to num when pad is called for the last time in this program? Explain your answer // =============> write your answer here + +// "01" diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..efef00253 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,11 +3,18 @@ // 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`; + let hours = Number(time.slice(0, 2)); + const minutes = time.slice(-2); + + if (hours === 24) { + hours = 0; } - return `${time} am`; + + const period = hours < 12 ? "am" : "pm"; + hours = hours % 12 || 12; + const paddedHours = String(hours).padStart(2, "0"); + + return `${paddedHours}:${minutes} ${period}`; } const currentOutput = formatAs12HourClock("08:00"); @@ -23,3 +30,31 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("24:00"); +const targetOutput4 = "12:00 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("12:00"); +const targetOutput5 = "12:00 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + +const currentOutput6 = formatAs12HourClock("09:41"); +const targetOutput6 = "09:41 am"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +);