diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..680421683 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,23 @@ // Predict and explain first... -// =============> write your prediction here +// It will make the first letter in the string input capitalised. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +// "SyntaxError: Identifier 'str' has already been declared" + +// capitalise("matthaus enjoys foood.") + +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } + +// The error is thrown by declaring the 'str' variable again in the function with 'let' which causes it throw the SyntaxError, as it is already been declared. + +capitalise("matthaus enjoys food") function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + return `${str[0].toUpperCase()}${str.slice(1)}`; } -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("Matthaus enjoys food")); \ No newline at end of file diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..a40a68ab8 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,30 @@ // Predict and explain first... +// It will give you a percentage of 50% using the function by multiplying the 'decimalNumber' by 100 for the 'percentage' variables. // Why will an error occur when this program runs? -// =============> write your prediction here +// It will be a syntaxError because the 'decimalNumber' is already called in the function parameter. // 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); + +// decimalNumber in console.log isn't a defined number in the functions parameter now so it will return back as NaN "Not a Number", by adding '0.05' to the console.log it now has a number to calculate the percentage to + +// Finally, correct the code to fix the problem + + function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; -} - -console.log(decimalNumber); -// =============> write your explanation here +} -// Finally, correct the code to fix the problem -// =============> write your new code here + console.log(convertToPercentage(0.5)) diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..e371e45eb 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,26 @@ // Predict and explain first BEFORE you run any code... +// The function won't return be able to return the value to the caller, due to the expression not being assigned. // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// The expression will return as unexpected number in the console log -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } -// =============> write the error message here +// SyntaxError: Unexpected number -// =============> explain this error message here +// The parameter set by the function isn't attached to the num value of the return, so the return doesn't have a value to work with // Finally, correct the code to fix the problem -// =============> write your new code here +function square(num) { + return num * num; +} + +console.log(square(3)); + diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..7baf95cb0 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,18 @@ // Predict and explain first... -// =============> write your prediction here +// There is no return in the function, the wrong use of console.log is being used, therefore the function isn't being closed and only is the console.log being logged twice, causing the function's parameters to be defined by the return. -function multiply(a, b) { - console.log(a * b); -} +// function multiply(a, b) { +// console.log(a * b); +// } -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// The instead of console.log being used in the functions expression, there should be a return. // 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-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..b2732b2b9 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,17 @@ // Predict and explain first... -// =============> write your prediction here +// By using the semi-colon after the return, this is closing off the function and leaving the parameters as undefined, causing the console.log state that the parameters are undefined. +// function sum(a, b) { +// return; +// a + b; +// } + +// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +// by using the semi-colon after the return, and having the parameters put into a separate line, the return can't define the parameters of the function. +// Finally, correct the code to fix the problem 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 -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..168f78549 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,11 +1,30 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// Because the num variable is defined globally and the function doesn't set the parameter using num, which isn't being called and the consol.log ignores it and refers back to the global variable being set on line 6 +// const num = 103; + +// function getLastDigit() { +// 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)}`); + +// Now run the code and compare the output to your prediction +// 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 +// There is no parameter set in the function, so the function when called is ignored and the console.log only outputs the const variable of num + +// Finally, correct the code to fix the problem const num = 103; -function getLastDigit() { +function getLastDigit(num) { return num.toString().slice(-1); } @@ -13,12 +32,5 @@ 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 -// Explain why the output is the way it is -// =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here - // 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..abce50349 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,8 @@ // 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 + let bmi = weight / (height * height); + return bmi.toFixed(1); } + +console.log(calculateBMI(72, 1.82)); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..13e950e56 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/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 + +const string = "hello there" + +function cases(inputStr) { + return inputStr.toUpperCase().replace(/ /g, "_"); +} + +console.log(cases(string)); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..39b1343b4 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/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("458p")); +console.log(toPounds("756p")); +console.log(toPounds("5425p")); \ No newline at end of file diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..ff2516667 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -15,24 +15,26 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(61)); + // 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 // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// 3 times (once for hours, once for minutes and once for seconds) // 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 +// With an input of 61 seconds, totalHours evaluates to 0. Therefore, num is assigned 0 on the first call // c) What is the return value of pad when it is called for the first time? -// =============> write your answer here +// The return value is "00". Since the input is 0, its string length is less than 2, so the while loop runs it twice to prepend two zeros ("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 +// The value assigned is 1. This is for remainingSeconds, which receives the remainder of 61 % 60, resulting in 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 is "01". Because the input is 1, its string length is less than 2, so the while loop runs once to prepend a single zero("01") diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..9235ebd83 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -3,11 +3,23 @@ // 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)); + const hoursStr = time.slice(0, 2); + const hours = Number(hoursStr); + const minutes = time.slice(3, 5); + + if (hours === 0) { + return `12:${minutes} am`; + } + if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2, '0')}:${minutes} pm`; } - return `${time} am`; + + if (hours === 12) { + return `12:${minutes} pm`; + } + + return `${hoursStr}:${minutes} am`; } const currentOutput = formatAs12HourClock("08:00"); @@ -23,3 +35,21 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("12:30"); +const targetOutput3 = "12:30 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("00:15"); +const targetOutput4 = "12:15 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +console.log(formatAs12HourClock("13:15")); +console.log(formatAs12HourClock("05:53")); +console.log(formatAs12HourClock("19:36")); \ No newline at end of file