diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..fef8524f9d 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -10,4 +10,12 @@ function capitalise(str) { } // =============> write your explanation here +// The error occurred because str is declared twice. Instead the expression is returned directly without declaring a new variable + // =============> write your new code here + +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} +const newName = capitalise(""); +console.log(newName); \ 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..ea8325714c 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 +// decimalNumber is declared twice and passed a value inside the function. +// The function hasn't been called properly + // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -16,5 +19,17 @@ console.log(decimalNumber); // =============> write your explanation here +// The error occurred because decimalNumber is declared twice. No need to create the percentage variable too. +// Instead the expression is returned directly without declaring a new variable. +// The function should be called with a value passed in as an argument and the result should be stored in a variable to be logged to the console. +// convertedPercentage is declared to store the result of the function call and then logged to the console. + // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + return`${decimalNumber * 100}%`; +} + +const convertedPercentage = convertToPercentage(0.5); +console.log(convertedPercentage); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..7f908975d6 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -5,16 +5,31 @@ // =============> write your prediction of the error here +// num is not declared as a result undefined error will be displayed. + function square(3) { return num * num; } // =============> write the error message here +// /Users/Russom/Desktop/x.js:1 +//function square(3) { +//SyntaxError: Unexpected number + // =============> explain this error message here +// The error message indicates that 3 cannot be passed as a parameter in the function declaration. +// The parameter should be a variable name not a number. + // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(3)); + +// The output of the function will be 9 as 3 * 3 = 9. \ 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..27bfaeda39 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -2,6 +2,10 @@ // =============> write your prediction here +// the function won't throw error. However, the function doesn't +// have a return expression. Therefore the function is called inside +// the console.log it will give undefined function. + function multiply(a, b) { console.log(a * b); } @@ -10,5 +14,18 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// 320 +//The result of multiplying 10 and 32 is undefined + +// The above error message is displayed when tested with node. +// The console.log inside the function outputs the correct value(320) +// However the function multiply does not return value so when the multiply function is called on line 9 it returns 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)}`); \ 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..02ed101cb2 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 +// This will throw an error because there is a semicolon after return statement. + function sum(a, b) { return; a + b; @@ -9,5 +11,17 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here + +//The sum of 10 and 32 is undefined + +// The above error message is displayed when tested with node. +// The cause of the error message is the semicolon that is placed after the return statement. + // 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..5e501071a5 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,6 +3,8 @@ // Predict the output of the following code: // =============> Write your prediction here +// num is declared outside of the function. So when getLastDigit is called it will return undefined. + const num = 103; function getLastDigit() { @@ -15,10 +17,31 @@ 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 + // Explain why the output is the way it is // =============> write your explanation here + +// 1) The above output (3) is displayed because the variable num is declared as a constant +// 2) .slice(-1) is used to get the last digit of num inside the function. +// 3) The function does not take any parameters so when it is called with an arguments it always returns the last digit of the constant num 103. + // 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)}`); +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 + +// Now the num is passed to the function as a parameter. +// Therefore the function will return the last digit of the number when it is when it is called and passed with an argument. \ 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 17b1cbde1b..845372ac10 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,11 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + return (weight / (height * height)).toFixed(1); // return the BMI of someone based off their weight and height -} \ No newline at end of file +} + +console.log(` Your Body Mass Index is: ${calculateBMI(70, 1.73)}`); + +// On line 19 after weight and heigh are calculated to give the BMI. +// The .toFixed(1) method is used to round the BMI to 1 decimal place. \ 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..7250fb520a 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,13 @@ // 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 upperSnakeCase(str) { + return str.toUpperCase().split(" ").join("_"); +} + +console.log(upperSnakeCase("upper snake case")); + +// 1) On line 19 the return statement inside the function first converts the strings to uppercase using toUpperCase() +// 2) Then the split() method is used to split the strings into an array of words. +// 3) Finally the join() method is used to join the words inserting an underscore between each word. diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..fd94484297 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,47 @@ // 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( + // p is removed from the string with the sub string method. + 0, + penceString.length - 1 + // length - 1 is used to get the index of the last character in penceString, which is p. + ); + + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + // padStart is used to ensure that the string has at least 3 characters by adding 0 at the start of the string + // if it is less than 3 characters long. This is important for formatting purposes, + // as we want to ensure that we have at least 3 digits to represent pounds and pence. + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + // substring is used to extract the pounds portion of the string by taking all characters except the last two, + // which represent pence. + ); + + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + // substring is used to extract the pence portion of the string by taking the last two characters. + // padEnd is used to ensure that the pence portion has at least 2 characters by adding 0 at the end of the string + // if it is less than 2 characters long. This is important for formatting purposes, + // as we want to ensure that we have at least 2 digits to represent pence. + return `£${pounds}.${pence}`; + // finally, the function returns a string representing the price in pounds, + // formatted with a £ symbol and a decimal point separating pounds and pence. +} +console.log(toPounds("399p")); +console.log(toPounds("1p")); +console.log(toPounds("3999p")); +// above are some test cases to check if the function works correctly for different inputs. + +// This program takes a string representing a price in pence +// The program then builds up a string representing the price in pounds + +// You need to do a step-by-step breakdown of each line in this program +// Try and describe the purpose / rationale behind each step + +// To begin, we can start with +// 1. const penceString = "399p": initialises a string variable with the value "399p" diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..02082608d1 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -23,16 +23,26 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// pad will be called 3 times when formatTimeDisplay is called. Once for totalHours, once for remainingMinutes and one 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 when pad is called for the first time is 0. This is because totalHours is calculated as (totalMinutes - remainingMinutes) / 60, which results in 0 when the input is 61 seconds. + // 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. this is because the value of num is 0 and the pad function adds a 0 to the front of the string until the num characters has at least 2 characters. + // 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 1. This is because the last call to pad is for remainingSeconds which is calculated as seconds % 60, the result of which is 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 of pad when it is called for the last time is 01. This is because the pad function adds a 0 to the front of the string if the characters of the string are less than 2. \ 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..aec272562d 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -4,22 +4,81 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); // The minute variable is added to manipulate the minutes by storing the original minutes as we are converting only the hours not the minutes. + if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`; // Here String object is added to convert hours into strings so the padStart can be used. This is because the padStart method only works with strings. + } else if (hours === 12) { + return `12:${minutes} pm`; + } else if (hours === 0) { + return `12:${minutes} am`; + } else { + return `${time} am`; } - return `${time} am`; } - -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput1 = formatAs12HourClock("08:00"); +const targetOutput1 = "08:00 am"; console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + currentOutput1 === targetOutput1, + `current output 1: ${currentOutput1}, target output: ${targetOutput1}` ); const currentOutput2 = formatAs12HourClock("23:00"); const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + `current output 2: ${currentOutput2}, target output: ${targetOutput2}` +); + +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output 3: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("12:00"); +const targetOutput4 = "12:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output 4: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("00:01"); +const targetOutput5 = "12:01 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output 5: ${currentOutput5}, target output: ${targetOutput5}` ); + +const currentOutput6 = formatAs12HourClock("12:01"); +const targetOutput6 = "12:01 pm"; +console.assert( + currentOutput6 === targetOutput6, + `current output 6: ${currentOutput6}, target output: ${targetOutput6}` +); + +const currentOutput7 = formatAs12HourClock("23:59"); +const targetOutput7 = "11:59 pm"; +console.assert( + currentOutput7 === targetOutput7, + `current output 7: ${currentOutput7}, target output: ${targetOutput7}` +); + +const currentOutput8 = formatAs12HourClock("13:00"); +const targetOutput8 = "01:00 pm"; +console.assert( + currentOutput8 === targetOutput8, + `current output 8: ${currentOutput8}, target output: ${targetOutput8}` +); + +// Edge test cases that should be tested: + +// 1) 08:00 -> 08:00 am +// 2) 23:00 -> 11:00 PM +// 3) 00:00 -> 12:00 AM +// 5) 12:00 -> 12:00 PM +// 6) 00:01 -> 12:01 AM +// 7) 12:01 -> 12:01 PM +// 8) 23:59 -> 11:59 PM +// 9) 13:00 -> 1:00 PM