diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..4170751bd 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,28 @@ // Predict and explain first... // =============> write your prediction here +//Answer: I predict that there will be a Reference Error, because str get's re-declared with let +//inside the function even though it already is declared as it is the parameter. Perhaps it would +// be solved by removing the "let". // 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; } +console.log(capitalise("hellllo"); +*/ + // =============> write your explanation here +// Answer: I got a Syntax Error, Identifier 'str' has already been declared. So 'str' needs to not +// be declared again. I will try to write the code without the let // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} +console.log(capitalise("hellllo")); diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..46997f2b9 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -2,9 +2,13 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// Answer: I will get a Syntax Error, because the variable decimalNumber is a parameter +// of the function, and then it gets re-declared inside the function. Removing the +// "const" would make sure it's not re-declared, just value reassigned on that line. // 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 +17,26 @@ function convertToPercentage(decimalNumber) { } console.log(decimalNumber); +*/ // =============> write your explanation here +// Answer: I got SyntaxError: Identifier 'decimalNumber' has already been declared +// I will make sure it's not re-declared in the function by removing the "const" + +// After removing the "const" I got a new error. ReferenceError: decimalNumber is not defined. I realized +// that the console.log at the bottom didn't call the function but just logged decimalNumber which hadn't +// been defined in the global scope. Putting back the "const", I will move the declaration of decimalNumber +// to outside the function to make the scope global. Now the global decimalNumber has nothing to do with the +// local parameter decimalNumber 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; +} + +const decimalNumber = 0.5; + +console.log(decimalNumber); diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..510e7a2c4 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,38 @@ - // 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 +// Answer: I predict that we will get a Reference Error: num is not defined. Or perhaps an error about the 3, as +// it is a number and therefore not a valid parameter name. I believe perhaps parameter names are like variable +// names and can't start with a number. +/* function square(3) { return num * num; } +*/ // =============> write the error message here +// Answer: SyntaxError: Unexpected number // =============> explain this error message here +// Answer: Yes we got an error message about the number 3. We wouldn't get a message about num not being defined +// since the function isn't called in the code, so the inside of the function can't produce an error. +// I will first try to update the 3 to n3, to see if it is a valid parameter name and see if the error goes away, +// just as an experiment. + +// Answer: The error did go away. However it didn't solve our problem as we still want to receive 3 squared when +// calling the function. I will change n3 to the proper parameter name, num. So it can be referenced inside the +// function body. Then I will make a function call and pass in 3 there, as an argument to the parameter num. +// Then I will log the result to see if it worked. // 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..966bab42c 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,29 @@ // Predict and explain first... // =============> write your prediction here +// Answer: I predict the console will log first 320, and then on a new line "The result of multiplying 10 and 32 is ${NaN}" +// This is because the function logs the result in it's body, so it will log it first as it is run, but +// because it's not explicitly returning anything, it will just return NaN into the string literal logged at the end. +/* 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 +// Answer: In reality, I was almost correct, but the function returned undefined, not NaN, so it logged +// 320, and then The result of multiplying 10 and 32 is undefined. Oh and also of course the string +// interpolation brackets weren't included in the logged string like I had predicted. +// I will fix the problem by returning a * b in the function body, instead of logging it. // 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..08bf5e52f 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,30 @@ // Predict and explain first... // =============> write your prediction here +// Answer: I predict that what happens is `The sum of 10 and 32 is undefined` gets logged to the console. +// That is because even though the function sum has a return statement, it doesn't return anything. There +// is a value that is meant to be returned below the return statement, but because the function already returned +// it will never reach that line in execution. That is why it is greyed out. +/* function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +*/ // =============> write your explanation here +// Answer: Running the code logged "The sum of 10 and 32 is undefined" like I thought. I will fix the problem +// by moving a + b; in the function body to be on the same line as return, so it gets returned instead of undefined. + // 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)}`); + +// Answer: now "The sum of 10 and 32 is 42" is logged. diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..0639357bb 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,7 +2,11 @@ // Predict the output of the following code: // =============> Write your prediction here +// Answer: I believe that when we run the code we will get a Reference Error, because we are trying to +// call a function getLastDigit with an argument even though the function doesn't have any parameters. +// Or possibly the error might reference num in the function body being undefined. +/* const num = 103; function getLastDigit() { @@ -12,13 +16,46 @@ 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 + // Explain why the output is the way it is // =============> write your explanation here +// Answer: I was wrong, there was no error message. Instead +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 +// was logged to the console. There was no Reference Error about num being undefined in the function body +// because num IS defined, just above the function and has global scope, so it is reachable by the function. +// Also, no error was thrown due to calling the function with arguments even though it accepted no parameters. +// This is because JavaScript is a dynamic/forgiving language that rather removes surplus information and keeps +// executing the code than stops it and gives an error message. So any surplus arguments passed into a function +// call just gets ignored. That is why the passed arguments have no effect on the function's return value. +// To make them have effect, I will add a parameter to the function, and call it num. Then when num gets accessed +// in the function body, it will not be the value of the global num, but instead the function-local parameter +// num. + // Finally, correct the code to fix the problem // =============> write your new code here +const num = 103; + +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 +// Answer: now it works as expected +// The last digit of 42 is 2 +// The last digit of 105 is 5 +// The last digit of 806 is 6 diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..42b122752 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,15 @@ // 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 Number.parseFloat(weight / (height * height)).toFixed(1); } + +// Tests +console.log(calculateBMI(55, 1.63)); +console.log(calculateBMI(120, 1.73)); +console.log(calculateBMI(80, 1.69)); +console.log(calculateBMI(51, 1.55)); + +// Please could I have a little feedback about if I have refactored the function return too much? +// And if so, what is a good guide on how many operations to perform in one line..? I can also +// ask this in class if you prefer. diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..8d834bde9 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,12 @@ // 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 str.toUpperCase().split(" ").join("_"); +} + +//tests +console.log(toUpperSnakeCase("i want to scream")); +console.log(toUpperSnakeCase("lord of the rings")); +console.log(toUpperSnakeCase("This is a loud file name")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..725a0c73e 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,32 @@ // 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]; +} + +//tests +let [pounds, pence] = toPounds("399p"); +console.log(`£${pounds}.${pence}`); +[pounds, pence] = toPounds("3995p"); +console.log(`£${pounds}.${pence}`); +[pounds, pence] = toPounds("42895p"); +console.log(`£${pounds}.${pence}`); +[pounds, pence] = toPounds("2p"); +console.log(`£${pounds}.${pence}`); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..39eaa8a60 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -21,18 +21,20 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// Answer: 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 +// Answer: 0 // c) What is the return value of pad when it is called for the first time? -// =============> write your answer here +// Answer: "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 +// Answer: 1. Num is 1 in the last call to pad, because the argument sent into pad is remainingSeconds, which is +// 1 because 1 is the remainder after dividing 61 with 60. // 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 +// Answer: "01". The return value is "01" because pad first turned 1 into a string "1", and then used a while loop +// to concatenate (pad) zeros at the start of the "1" string until it became two in length. diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..126136c37 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -5,7 +5,9 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12 < 10 ? "0" : ""}${hours - 12}:${time.slice(-2)} pm`; + } else if (hours === 12) { + return `${time} pm`; } return `${time} am`; } @@ -14,12 +16,40 @@ 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("12:00"); +const targetOutput3 = "12:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}`, +); + +const currentOutput4 = formatAs12HourClock("15:45"); +const targetOutput4 = "03:45 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}`, +); + +const currentOutput5 = formatAs12HourClock("08:25"); +const targetOutput5 = "08:25 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}`, +); + +const currentOutput6 = formatAs12HourClock("12:17"); +const targetOutput6 = "12:17 pm"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}`, );