diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..e11446e2e 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,25 @@ // Predict and explain first... -// =============> write your prediction here +// =============>// I predict JavaScript will give a SyntaxError because str is declared twice in the function. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring -function capitalise(str) { +/*function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } - -// =============> write your explanation here +capitalise("hello");*/ +/* =============> The error happens because str is already the function's parameter. + let str tries to use the same name again, so JavaScript gives a SyntaxError. + The function stops before it can run. */ // =============> write your new code here +function capitalise(str) { + if (str === "") { + return ""; + } + + const result = `${str[0].toUpperCase()}${str.slice(1)}`; + return result; +} + +console.log(`Result: "${capitalise("")}"`); diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..9fb514925 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,29 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============>// I predict a SyntaxError because decimalNumber is declared as a parameter and then declared again with const. // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { +/*function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(decimalNumber);*/ -// =============> write your explanation here +/* =============> decimalNumber is already the function parameter. + const decimalNumber tries to declare the same name again inside the function, + so JavaScript gives a SyntaxError. */ // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + const percentage = `${(decimalNumber * 100).toFixed(1)}%`; + + return percentage; +} + +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..042368fa6 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,21 @@ - // Predict and explain first BEFORE you run any code... // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> // I predict a SyntaxError because 3 is a number, but a function parameter needs to be a name. -function square(3) { +/*function square(3) { return num * num; } +*/ +// =============> SyntaxError: Unexpected number -// =============> write the error message here - -// =============> explain this error message here +// =============> The error happens because 3 is not a name. A parameter needs a name, like num. // 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..1661caf95 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 +// =============> I couldn't predict until I run the code -function multiply(a, b) { +/*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 +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);*/ +/* =============> The Terminal gives an answer of 320 but also says "The result of multiplying 10 and 32 is undefined" +which makes me think that the function has no return so it gives back 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-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..0d7d597b5 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,19 @@ // Predict and explain first... -// =============> write your prediction here +// =============> I predict the code won't work because there's a ";" after return. -function sum(a, b) { +/*function sum(a, b) { return; a + b; } -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);*/ -// =============> write your explanation here +/* =============> The sum is undefined because the semicolon after return ends it too early, +the function returns before adding a and b.*/ // 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-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..5f4ce680b 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,9 +1,9 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> I predict the result will be wrong because the num is set set on 103. -const num = 103; +/*const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -11,14 +11,33 @@ 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)}`); +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 +/* =============> the output: +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 +/* =============> The problem is that the function ignores the numbers passed to it, + it always uses num, which is 103, so it returns 3 every time.*/ // Finally, correct the code to fix the problem // =============> write your new code here +/*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)}`);*/ // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem +// In this case getLastDigit is not working because it's parameter is empty and not declared. +// Here's the right code: +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)}`); diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..ba8f010d1 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,6 @@ // 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 + return (weight / (height * height)).toFixed(1); } +console.log(calculateBMI(52, 1.63)); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..574482364 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/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(words) { + return words.toUpperCase().trim().split(/\s+/).join("_"); +} + +console.log(toUpperSnakeCase("hello there")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..8562c5843 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,20 @@ // 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 penceNumberString = String(penceString).replace(/p$/i, ""); + const paddedPenceNumberString = penceNumberString.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("599p")); +console.log(toPounds("6p")); +console.log(toPounds("2568p")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..be7bd335e 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -14,6 +14,7 @@ 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 @@ -21,18 +22,17 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============>Pad will be called 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 +// =============> the first time pad is called, num gets the value of totalHours, which is 0. // c) What is the return value of pad when it is called for the first time? -// =============> write your answer here - +// =============> the first time pad is called, it returns 00 because num is 0 and pad adds a zero in front. // 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 last time pad is called, num is 1 because there is 1 second left. // 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 last time pad is called, it returns 01 because num is 1 and pad adds a 0 in front. diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..b6914afaf 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -3,23 +3,49 @@ // 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`; + const [hoursString, minutesString = "00"] = time.split(":"); + const hours = Number(hoursString); + const minutes = minutesString.padStart(2, "0"); + + let suffix = "am"; + if (hours >= 12) { + suffix = "pm"; } - return `${time} am`; -} -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); + let formattedHours = hours % 12; + if (formattedHours === 0) { + formattedHours = 12; + } + + return `${formattedHours}:${minutes} ${suffix}`; +} const currentOutput2 = formatAs12HourClock("23:00"); const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + `current output: ${currentOutput2}, target output: ${targetOutput2}`, ); + +const noonOutput = formatAs12HourClock("12:00"); +const noonTarget = "12:00 pm"; +console.assert( + noonOutput === noonTarget, + `current output: ${noonOutput}, target output: ${noonTarget}`, +); + +const midnightOutput = formatAs12HourClock("00:00"); +const midnightTarget = "12:00 am"; +console.assert( + midnightOutput === midnightTarget, + `current output: ${midnightOutput}, target output: ${midnightTarget}`, +); + +const singleDigitMinuteOutput = formatAs12HourClock("9:5"); +const singleDigitMinuteTarget = "9:05 am"; +console.assert( + singleDigitMinuteOutput === singleDigitMinuteTarget, + `current output: ${singleDigitMinuteOutput}, target output: ${singleDigitMinuteTarget}`, +); + +console.log("All formatAs12HourClock tests passed.");