diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..1515107074 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// The function will not work because the variable str is the input to the function. +// Hence, this will throw an error that says that the variable str is already declared. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +12,12 @@ function capitalise(str) { } // =============> write your explanation here +// The error message is 'Uncaught SyntaxError: Identifier 'str' has already been declared.' +// Like what I have explained about, the variable str is the input for the function, so it cannot be declared again with let. +// You need to make a new variable with a new name in order for the function to work. + // =============> write your new code here +function capitalise(str) { + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; +} \ 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 f2d56151f4..f80385184f 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,6 +2,7 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// It will throw an error because decimalNumber is the input for the function, but below we are trying to declare it again using const. // Try playing computer with the example to work out what is going on @@ -15,6 +16,12 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// This function would not run because the variable decimalNumber is declared twice. +// In order for it to work, another variable with a different name must be created. // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage (decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..aa0a2e4918 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,24 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// There will be an error because the name of the input variable is not defined and we are just putting a number which will throw 'unexpected number' error. +// and likewise, the variable num is undefined. -function square(3) { - return num * num; -} +//function square(3) { +// return num * num; +//} // =============> write the error message here +// Uncaught SyntaxError: Unexpected number // =============> explain this error message here +// This error happens when numeral is improperly positioned/used +// In this case, when declaring a function we should always first declare what is the name of the input parameter +// (instead of immediately inserting the parameter). // Finally, correct the code to fix the problem // =============> write your new code here - - +function square(num) { + return num * num; +} \ 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 b27511b417..20bc2516d5 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,9 @@ // Predict and explain first... // =============> write your prediction here +// My prediction is that the second console log will not print what is intended because in the function, the last line is console.log +// Console.log will return nothing (undefined). Instead, the last line should've been return. +// Using return will make the second console log print the result of the function (instead of undefined). function multiply(a, b) { console.log(a * b); @@ -11,4 +14,10 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..7dab236020 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// Return will return nothing since no value is assigned to it. +// The line a + b never runs because a function ends with 'return'. function sum(a, b) { return; @@ -9,5 +11,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The console log will print 'undefined' on the call function. // 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)}`); \ 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 57d3f5dc35..b7b7969bab 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,6 +2,8 @@ // Predict the output of the following code: // =============> Write your prediction here +// The function will not run as intended because the parameter is not set +// Instead it always uses the variable num, so it will always only return that variable's last digit. const num = 103; @@ -15,10 +17,16 @@ 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 +// All outputs are "3", which is the last digit of the variable num, 103. // Explain why the output is the way it is // =============> write your explanation here +// It is because no parameter is set on the function and it instead always uses the variable num. // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(number) { + return number.toString().slice(-1); +} + // 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-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..ddf7174dcd 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 + const squaredHeight = height ** 2; + const dividedWeight = weight / squaredHeight; + const bmi = dividedWeight.toFixed(1); + return bmi; } \ 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 5b0ef77ad9..5a36fd073c 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.split(" ").join("_").toUpperCase(); +} \ 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 6265a1a703..cee2415012 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) { + // remove the "p" + const penceStringWithoutP = penceString.replace("p", ""); + + // separate pounds and pence + let pence = penceStringWithoutP.slice(-2).padStart(2, "0"); + let pounds = penceStringWithoutP.slice(0, -2).padStart(1, "0"); + + // convert to number and format with commas + pounds = Number(pounds).toLocaleString('en-GB'); + + return `£${pounds}.${pence}`; +} + +// examples +console.log(toPounds("5p")); // £0.05 +console.log(toPounds("99p")); // £0.99 +console.log(toPounds("1234p")); // £12.34 +console.log(toPounds("1234567p")); // £12,345.67 \ 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..85ede0eb57 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,33 @@ function formatTimeDisplay(seconds) { // Questions // 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 +// The value assigned to num when pad is called for the first time is the variable TotalHours +// totalHours = (totalMinutes - remainingMinutes) / 60 +// totalMinutes = (seconds - remainingSeconds) / 60 +// remainingMinutes = totalMinutes % 60 +// remainingSeconds = seconds % 60 = 61 % 60 = 1 +// totalMinutes = (61 - 1) / 60 = 1 +// remainingMinutes = 1 % 60 = 1 +// totalHours = (1 - 1) / 60 = 0 +// So, the value assigned to num when pad is called for the first time is 0. // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// The return value of pad when it is called for the first time is "00". +// First, 0 is turned into string --> "0". +// Then, a pad of 0 is added at the start so the string length will be 2. +// Hence, the return value is "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 to num when pad is called for the last time is the variable remainingSeconds. +// As calculated above, remainingSeconds = 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 +// The return value of pad when it is called for the last time is "01". +// First, 1 is turned into string --> "1". +// Then, a pad of 0 is added at the start so the string length will be 2. +// Hence, the return value is "01". \ No newline at end of file diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..61731bfec3 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,14 +2,42 @@ // Make sure to do the prep before you do the coursework // 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`; +// } +// return `${time} am`; +//} + +// Observation: +// the minutes are hardcoded for hours > 12 +// incorrect midnight (00:00) it will return 00:00 am instead of 12:00 am +// incorrect noon (12:00) it will return 12:00 am instead of 12:00 pm +// no padding for single digit hours + +// Fixing the function: 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)); // get hours as number + const minutes = time.slice(2); // keep the minutes string + let suffix = "am"; + + if (hours === 0) { + hours = 12; // midnight + } else if (hours === 12) { + suffix = "pm"; // noon + } else if (hours > 12) { + hours -= 12; + suffix = "pm"; } - return `${time} am`; + + // pad single-digit hours with leading zero + const hoursStr = String(hours).padStart(2, "0"); + + return `${hoursStr}${minutes} ${suffix}`; } + const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( @@ -23,3 +51,17 @@ 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("12:00"); +const targetOutput4 = "12:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); \ No newline at end of file