diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..1327a4414 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,22 @@ // Predict and explain first... // =============> write your prediction here +// the function has a parameter str, but then tries to make a new +// variable also called str. so maybe cause error because str already exists // 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; -} +//function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +//} // =============> write your explanation here +// SyntaxError: Identifier 'str' has already been declared +// Prediction was correct, str already exists // =============> write your new code here + +function capitalise(str) { + const capitaliseStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitaliseStr; +} diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..5a0da345d 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -2,19 +2,41 @@ // Why will an error occur when this program runs? // =============> write your prediction here +//decimalNumber already exists as a parameter, so const decimalNumber is making +//a new variable with the same name. There is also a problem where decimalNumber is being +//assigned 0.5, which won't cause errors, but means that no matter what is passed to the function +//decimalNumber will always be 0.5 // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; +//function convertToPercentage(decimalNumber) { +// const percentage = `${decimalNumber * 100}%`; - return percentage; -} +// return percentage; +//} -console.log(decimalNumber); +//convertToPercentage(0.6) +//console.log(decimalNumber); // =============> write your explanation here +//SyntaxError: Identifier 'decimalNumber' has already been declared +//prediction correct, decimalNumber already exists so causes error. Removing the line +//shows another error +//console.log(decimalNumber); +// ^ +//ReferenceError: decimalNumber is not defined +//This is because of scope, decimalNumber is defined in the function, so it only has scope +//in the function // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +let decimalNumber = 0.6; +console.log(decimalNumber); +console.log(convertToPercentage(decimalNumber)); diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..1438d2a51 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... // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// In the function, 3 hasn't been assigned to num, so num is NaN and should return NaN. +//Also when defining a function, the parameter should have the variable name, not a value +//to pass in, unless you want to default value such as num = 3, which assigns 3 to num if +// no value is passed in -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 - +//syntaxError means there is a mistake in the way the code is written, so javascript can't +// interpret the code. in this case there is a number here when there shouldn't be. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..74e23e0a4 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,25 @@ // Predict and explain first... // =============> write your prediction here +//The multiply function doesn't return anything, only prints to console so the function call will have value undefined. +//So when line 12 is executed, first it will print "The result of multiplying 10 and 32 is undefined" +//Then on a new line it prints 320 -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 - +//It first printed 320, then printed "The result of multiplying 10 and 32 is undefined" on the next line +//I think this is because it goes through the line 12, executes the multiply function call first, then finishes the +//console.log function call. // 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..ef7b55841 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,23 @@ // Predict and explain first... // =============> write your prediction here +//when the return line is executed, the function exits. a + b is a separate line because it +//is after the semicolon, so a + b is never reached and not returned. So line 11 will print +//The The sum of 10 and 32 is undefined -function sum(a, b) { - return; - a + b; -} +//function sum(a, b) { +// return; +// a + b; +//} console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +//It did as predicted, for the reason I expected // 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..42f75b5f6 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,24 +1,41 @@ // Predict and explain first... - // Predict the output of the following code: +//The last digit of 42 is 3 +//The last digit of 105 is 3 +//The last digit of 806 is 3 // =============> Write your prediction here +//num is defined outside of the function, so has scope in the function getLastDigit. +//the function doesn't take any arguments and uses num, so will always return 3, no matter what +//is passed to the function because it will use num, which is set to 103 -const num = 103; +//const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +//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)}`); +//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 // Explain why the output is the way it is // =============> write your explanation here +// As I predicted, for the reason above // 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 diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..6b39f4d69 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -16,4 +16,5 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + return (weight / height ** 2).toFixed(1); } diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..fe92c4694 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,6 @@ // 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(inputString) { + return inputString.toUpperCase().replaceAll(" ", "_"); +} diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..d6f1144bd 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}`); +} + +toPounds("10p"); +toPounds("100p"); +toPounds("232134p"); +toPounds("238p"); +toPounds("5234p"); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..6fd34cd30 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -22,17 +22,27 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// 3 // 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 +// 0 // c) What is the return value of pad when it is called for the first time? // =============> write your answer here +// "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 +// 1. Last time it is called is pad(remainingSeconds). remainingSeconds = seconds % 60 +// with seconds = 61 this expression evaluates to 1, so when 1 is passed to pad, num is +// assigned 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 +// "01". Num is 1 which converted to the string "1" which is assigned to numString. +// The while loop runs while numString length less than 2, and adds "0" +// to the beginning string. So "1" becomes "01". the string length is now equal to two, +// so the while loop exits and numString is returned with value "01" diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..5cf4f51e2 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -1,25 +1,305 @@ // This is the latest solution to the problem from the prep. // 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 pad(num) { + let stringNum = num.toString(); + if (stringNum.length < 2) { + stringNum = "0" + stringNum; + return stringNum; + } else { + return stringNum; + } +} +function formatAs12HourClock(time = "") { + if (typeof time != "string") { + return "Not a valid format"; //check if time is a string + } + + if (/\s/.test(time)) { + return "Not a valid time"; //check for whitespace in string + } + + if ( + (time.indexOf(":") === -1 && time.indexOf(".") === -1) || + (time.indexOf(":") !== -1 && time.indexOf(".") !== -1) + ) { + return "Not a valid time"; // check if there is only either one : or . + } + + if (time.indexOf(":") === -1) { + if (time.indexOf(".") !== time.lastIndexOf(".")) { + return "Not a valid time"; //check there is only one . + } + } + if (time.indexOf(".") === -1) { + if (time.indexOf(":") !== time.lastIndexOf(":")) { + return "Not a valid time"; //check if there is only one : + } + } + + let colonPeriodIndex = 0; + if (time.indexOf(":") === -1) { + colonPeriodIndex = time.indexOf("."); + } else { + colonPeriodIndex = time.indexOf(":"); + } + + if ( + time.slice(0, colonPeriodIndex) === "" || + time.slice(colonPeriodIndex + 1) === "" + ) { + return "Not a valid time"; //check if there is a number before and after time separator + } + const hours = Number(time.slice(0, colonPeriodIndex)); + const minutes = Number(time.slice(colonPeriodIndex + 1)); + const stringMinutes = pad(minutes); + const stringHours = pad(hours); -function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); + if ( + isNaN(hours) || + isNaN(minutes) || + hours >= 24 || + hours < 0 || + minutes >= 60 || + minutes < 0 || + hours % 1 != 0 || + minutes % 1 != 0 + ) { + return "Not a valid time"; + } //check if numbers are valid + if (hours === 12) { + return `${stringHours}:${stringMinutes} pm`; + } + if (hours === 0) { + return `12:${stringMinutes} am`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${pad(hours - 12)}:${stringMinutes} pm`; } - return `${time} am`; + + return `${stringHours}:${stringMinutes} am`; } const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + `current output: ${currentOutput}, target output: ${targetOutput}`, ); 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 currentOutput3 = formatAs12HourClock("23:46"); +const targetOutput3 = "11:46 pm"; +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}`, +); + +const currentOutput5 = formatAs12HourClock("00:00"); +const targetOutput5 = "12:00 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}`, +); + +const currentOutput6 = formatAs12HourClock("24:00"); +const targetOutput6 = "Not a valid time"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}`, +); + +const currentOutput7 = formatAs12HourClock("47:00"); +const targetOutput7 = "Not a valid time"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}`, +); + +const currentOutput8 = formatAs12HourClock("15:75"); +const targetOutput8 = "Not a valid time"; +console.assert( + currentOutput8 === targetOutput8, + `current output: ${currentOutput8}, target output: ${targetOutput8}`, +); + +const currentOutput9 = formatAs12HourClock("18:60"); +const targetOutput9 = "Not a valid time"; +console.assert( + currentOutput9 === targetOutput9, + `current output: ${currentOutput9}, target output: ${targetOutput9}`, +); + +const currentOutput10 = formatAs12HourClock("23:59"); +const targetOutput10 = "11:59 pm"; +console.assert( + currentOutput10 === targetOutput10, + `current output: ${currentOutput10}, target output: ${targetOutput10}`, +); + +const currentOutput11 = formatAs12HourClock("00:01"); +const targetOutput11 = "12:01 am"; +console.assert( + currentOutput11 === targetOutput11, + `current output: ${currentOutput11}, target output: ${targetOutput11}`, +); + +const currentOutput12 = formatAs12HourClock("12:01"); +const targetOutput12 = "12:01 pm"; +console.assert( + currentOutput12 === targetOutput12, + `current output: ${currentOutput12}, target output: ${targetOutput12}`, +); + +const currentOutput13 = formatAs12HourClock("13:00"); +const targetOutput13 = "01:00 pm"; +console.assert( + currentOutput13 === targetOutput13, + `current output: ${currentOutput13}, target output: ${targetOutput13}`, +); + +const currentOutput14 = formatAs12HourClock("hello"); +const targetOutput14 = "Not a valid time"; +console.assert( + currentOutput14 === targetOutput14, + `current output: ${currentOutput14}, target output: ${targetOutput14}`, +); + +const currentOutput15 = formatAs12HourClock("15:ab"); +const targetOutput15 = "Not a valid time"; +console.assert( + currentOutput15 === targetOutput15, + `current output: ${currentOutput15}, target output: ${targetOutput15}`, +); + +const currentOutput16 = formatAs12HourClock("12"); +const targetOutput16 = "Not a valid time"; +console.assert( + currentOutput16 === targetOutput16, + `current output: ${currentOutput16}, target output: ${targetOutput16}`, +); + +const currentOutput17 = formatAs12HourClock("12:30abc"); +const targetOutput17 = "Not a valid time"; +console.assert( + currentOutput17 === targetOutput17, + `current output: ${currentOutput17}, target output: ${targetOutput17}`, +); + +const currentOutput18 = formatAs12HourClock("1.30"); +const targetOutput18 = "01:30 am"; +console.assert( + currentOutput18 === targetOutput18, + `current output: ${currentOutput18}, target output: ${targetOutput18}`, +); + +const currentOutput19 = formatAs12HourClock("15.30"); +const targetOutput19 = "03:30 pm"; +console.assert( + currentOutput19 === targetOutput19, + `current output: ${currentOutput19}, target output: ${targetOutput19}`, +); + +const currentOutput20 = formatAs12HourClock("01.30"); +const targetOutput20 = "01:30 am"; +console.assert( + currentOutput20 === targetOutput20, + `current output: ${currentOutput20}, target output: ${targetOutput20}`, +); + +const currentOutput21 = formatAs12HourClock("12.30:45"); +const targetOutput21 = "Not a valid time"; +console.assert( + currentOutput21 === targetOutput21, + `current output: ${currentOutput21}, target output: ${targetOutput21}`, +); + +const currentOutput22 = formatAs12HourClock("12::30"); +const targetOutput22 = "Not a valid time"; +console.assert( + currentOutput22 === targetOutput22, + `current output: ${currentOutput22}, target output: ${targetOutput22}`, +); + +const currentOutput23 = formatAs12HourClock("12..30"); +const targetOutput23 = "Not a valid time"; +console.assert( + currentOutput23 === targetOutput23, + `current output: ${currentOutput23}, target output: ${targetOutput23}`, +); + +const currentOutput24 = formatAs12HourClock("12:"); +const targetOutput24 = "Not a valid time"; +console.assert( + currentOutput24 === targetOutput24, + `current output: ${currentOutput24}, target output: ${targetOutput24}`, +); + +const currentOutput25 = formatAs12HourClock("12."); +const targetOutput25 = "Not a valid time"; +console.assert( + currentOutput25 === targetOutput25, + `current output: ${currentOutput25}, target output: ${targetOutput25}`, +); + +const currentOutput26 = formatAs12HourClock(":30"); +const targetOutput26 = "Not a valid time"; +console.assert( + currentOutput26 === targetOutput26, + `current output: ${currentOutput26}, target output: ${targetOutput26}`, +); + +const currentOutput27 = formatAs12HourClock("."); +const targetOutput27 = "Not a valid time"; +console.assert( + currentOutput27 === targetOutput27, + `current output: ${currentOutput27}, target output: ${targetOutput27}`, +); + +const currentOutput28 = formatAs12HourClock("15:-30"); +const targetOutput28 = "Not a valid time"; +console.assert( + currentOutput28 === targetOutput28, + `current output: ${currentOutput28}, target output: ${targetOutput28}`, +); + +const currentOutput29 = formatAs12HourClock("-15:30"); +const targetOutput29 = "Not a valid time"; +console.assert( + currentOutput29 === targetOutput29, + `current output: ${currentOutput29}, target output: ${targetOutput29}`, +); + +const currentOutput30 = formatAs12HourClock(" :30"); +const targetOutput30 = "Not a valid time"; +console.assert( + currentOutput30 === targetOutput30, + `current output: ${currentOutput30}, target output: ${targetOutput30}`, +); + +const currentOutput31 = formatAs12HourClock("12:30 "); +const targetOutput31 = "Not a valid time"; +console.assert( + currentOutput31 === targetOutput31, + `current output: ${currentOutput31}, target output: ${targetOutput31}`, +); + +const currentOutput32 = formatAs12HourClock("12:30\n"); +const targetOutput32 = "Not a valid time"; +console.assert( + currentOutput32 === targetOutput32, + `current output: ${currentOutput32}, target output: ${targetOutput32}`, );