diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..9ea982a1d 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,29 @@ // Predict and explain first... // =============> write your prediction here +// I guess the function is trying to capitalise the first letter of the string, +// capture the rest of the string from index 1 to the end of the string, +// and concatenate them together to return the capitalised string // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring - +/* function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +*/ // =============> write your explanation here +// After calling the function capitalise with a string input, +// I got a syntax error that "str" has already been declared. +// This is because str has already been declared as a parameter of the function, +// when a new variable named str, it causes a conflict. + // =============> write your new code here +// I renamed the new variable to capitalisedStr to avoid the conflict + +function capitalise(str) { + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; +} +console.log(capitalise("hello")); // should return "Hello" diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..66c66fb06 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,10 +1,18 @@ // Predict and explain first... +// At the first glance, I believe the function would occur errors because the variable decimalNumber is declared twice. +// Besides, console.log is trying to log a variable that is not defined in global scope. // Why will an error occur when this program runs? // =============> write your prediction here +// The first error should be a syntax error that "decimalNumber" has already been declared. +// Since decimalNumber is declared as a parameter of the function, +// when a new variable also named decimalNumber is declared inside the function, +// it causes a conflict. -// Try playing computer with the example to work out what is going on +// The second error should be a reference error that "decimalNumber is not defined". +// Try playing computer with the example to work out what is going on +/* function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -13,8 +21,19 @@ function convertToPercentage(decimalNumber) { } console.log(decimalNumber); +*/ // =============> write your explanation here +// After running the code, I got a syntax error that "decimalNumber" has already been declared. +// I also found out that the decimalNumber inside the function should be declared again. +// Besides, if we want to log the result of the function, we should call the function with an argument inside console.log(). // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} +console.log(convertToPercentage(0.5)); // should return "50%" +console.log(convertToPercentage(0.75)); // should return "75%" diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..a446c3882 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,27 @@ - // 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 guess the error is a reference error because the parameter is not defined correctly. +/* function square(3) { return num * num; } - +*/ // =============> write the error message here +// function square(3) { +// ^ +//SyntaxError: Unexpected number +// In fact, the error is a syntax error because number 3 is unexpected here. // =============> explain this error message here +// Number 3 is not a valid parameter name, which is misplacing and causes a syntax error. // Finally, correct the code to fix the problem // =============> write your new code here - - +function square(num) { + return num * num; +} +console.log(square(3)); // should return 9 diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..5eb351210 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,24 @@ // Predict and explain first... // =============> write your prediction here - +// It seems the function multiply did not return any value. +// Therefore, it is like a console.log returning undefined, while it is inside another console.log. +// So the output should be "The result of multiplying 10 and 32 is undefined". +/* 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 +// After running the code, I got a separate output of 320 and "The result of multiplying 10 and 32 is undefined". +// This is because the function multiply does not return any value, so it returns undefined by default. +// The console.log inside the function multiply prints 320 when the function is called. // 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)}`); // should return "The result of multiplying 10 and 32 is 320" diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..f5f433d79 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,23 @@ // Predict and explain first... // =============> write your prediction here +// It looks like the function sum return nothing because a+b is written in the next line after return statement. +// Therefore, the output should be "The sum of 10 and 32 is undefined". +/* function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); - +*/ // =============> write your explanation here +// After running the code, I got "The sum of 10 and 32 is undefined". +// The return statement ends the function execution and a+b is never evaluated. + // 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)}`); // should return "The sum of 10 and 32 is 42" diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..8c2afa56a 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,9 +2,11 @@ // Predict the output of the following code: // =============> Write your prediction here +// The output should always be 3 because the parameter is not defined in the function, +// and the variable num is always 103 in the global scope. const num = 103; - +/* function getLastDigit() { return num.toString().slice(-1); } @@ -12,13 +14,30 @@ 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)}`); - +*/ // Now run the code and compare the output to your prediction // =============> write the output here +/* +The last digit of 42 is 3 +The last digit of 105 is 3 +The last digit of 806 is 3 +*/ +// As expected, the output is always 3. + // Explain why the output is the way it is // =============> write your explanation here +//The num is defined in the global scope as 103. +// And the function does not accept any parameters. +// So every time the function is called, it returns the last digit of 103, which is 3. + // 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)}`); // should return 2 +console.log(`The last digit of 105 is ${getLastDigit(105)}`); // should return 5 +console.log(`The last digit of 806 is ${getLastDigit(806)}`); // should return 6 // 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 17b1cbde1..fba0a98b7 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,9 @@ // 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 bmi = weight / (height * height); + return bmi.toFixed(1); +} + +console.log(calculateBMI(70, 1.73)); // should return 23.4 diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..f1d549e32 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/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 + +function toUpperSnakeCase(str) { + // return the string in UPPER_SNAKE_CASE + return str.toUpperCase().replaceAll(" ", "_"); +} + +console.log(toUpperSnakeCase("hello there")); // should return "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // should return "LORD_OF_THE_RINGS" diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..0b87c9185 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,64 @@ // 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 + +/* Code from Sprint-1 below for reference +const penceString = "399p"; + +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"); + +console.log(`£${pounds}.${pence}`); +*/ + +function toPounds(penceString) { + if (typeof penceString !== "string" || !penceString.endsWith("p")) { + console.log("Invalid input, please provide a penceString ending with 'p'"); + return "Invalid input, please provide a penceString ending with 'p'"; + } + + const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 + ); + + if (Number.isNaN(Number(penceStringWithoutTrailingP))) { + console.log("Invalid input, please provide a numeric penceString"); + return "Invalid input, please provide a numeric penceString"; + } + + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + console.log(`£${pounds}.${pence}`); + return `£${pounds}.${pence}`; +} + +toPounds("399p"); +toPounds("5p"); +toPounds("89p"); +toPounds("1234p"); +toPounds("0p"); +toPounds("70000p"); +toPounds("abcp"); +toPounds(500); +toPounds("abc"); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..833f85de0 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -1,4 +1,11 @@ +let timesToBeCalled = 0; + function pad(num) { + timesToBeCalled += 1; + console.log( + ` Pad has been called ${timesToBeCalled} times and num is ${num}` + ); + console.log(` The return value will be ${num.toString().padStart(2, "0")}`); return num.toString().padStart(2, "0"); } @@ -11,6 +18,8 @@ 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 @@ -18,17 +27,27 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// The pad function will be called 3 times when formatTimeDisplay is called. +// 1st: for totalHours +// 2nd: for remainingMinutes +// 3rd: for remainingSeconds // 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 is 0 when pad is called for the first time because 61 seconds is 0 hours, 1 minute, and 1 second. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value of pad is "00" in string format because 0 is padded to 2 digits with leading zeros. // 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 is 1 when pad is called for the last time. +// It is because the remaining seconds is 1 second after taken 60 seconds for 1 minute. // 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 assigned to num is "01" in string format. +// It is because 1 second is padded to 2 digits with a leading zero. diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..dd08a082b 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,22 +4,139 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = Number(time.slice(3)); + if (isNaN(hours) || isNaN(minutes)) { + throw new Error("Invalid time"); + } + if (hours < 0 || hours > 24 || minutes < 0 || minutes > 59) { + throw new Error("Invalid time"); + } + if (time[2] !== ":") { + throw new Error("Invalid time"); + } + if (time.length !== 5) { + throw new Error("Invalid time"); + } + + const minutesStr = String(minutes).padStart(2, 0); + + if (hours == 0) { + return `12:${minutesStr} am`; + } + + if (hours == 12 && minutes >= 0) { + return `12:${minutesStr} pm`; + } + + if (hours == 24) { + return `12:${minutesStr} am`; + } + if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2, 0)}:${minutesStr} pm`; } + return `${time} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput = formatAs12HourClock("08:01"); +const targetOutput = "08:01 am"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +const currentOutput2 = formatAs12HourClock("23:09"); +const targetOutput2 = "11:09 pm"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("12:00"); +const targetOutput3 = "12:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("00:00"); +const targetOutput4 = "12:00 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("15:30"); +const targetOutput5 = "03:30 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + +const currentOutput6 = formatAs12HourClock("11:15"); +const targetOutput6 = "11:15 am"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); + +const currentOutput7 = formatAs12HourClock("00:30"); +const targetOutput7 = "12:30 am"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}` +); + +const currentOutput8 = formatAs12HourClock("12:45"); +const targetOutput8 = "12:45 pm"; +console.assert( + currentOutput8 === targetOutput8, + `current output: ${currentOutput8}, target output: ${targetOutput8}` +); + +const currentOutput9 = formatAs12HourClock("24:00"); +const targetOutput9 = "12:00 am"; +console.assert( + currentOutput9 === targetOutput9, + `current output: ${currentOutput9}, target output: ${targetOutput9}` +); + +const currentOutput10 = formatAs12HourClock("24:05"); +const targetOutput10 = "12:05 am"; +console.assert( + currentOutput10 === targetOutput10, + `current output: ${currentOutput10}, target output: ${targetOutput10}` +); + +// Edge cases and invalid inputs + +try { + formatAs12HourClock("25:00"); + console.assert(false, "Expected an error for invalid hours"); +} catch (e) { + console.assert( + e.message === "Invalid time", + `Unexpected error message: ${e.message}` + ); +} + +try { + formatAs12HourClock("10:60"); + console.assert(false, "Expected an error for invalid minutes"); +} catch (e) { + console.assert( + e.message === "Invalid time", + `Unexpected error message: ${e.message}` + ); +} + +try { + formatAs12HourClock("ab:cd"); + console.assert(false, "Expected an error for non-numeric input"); +} catch (e) { + console.assert( + e.message === "Invalid time", + `Unexpected error message: ${e.message}` + ); +}