diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..103c3a3b5 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,27 @@ // Predict and explain first... -// =============> write your prediction here +// str is a parameter in the function capitalise, so there will likely be a syntax error when trying to declare a second variable (let str) with the same name in the same scope. + // 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 +// Error message: SyntaxError: Identifier 'str' has already been declared +// A parameter is already a variable declared inside the function's scope. +// let cannot declare a second variable with the same name in the same scope, so +// JavaScript rejects the whole file before running anything. That is why the +// error appears even before capitalise is called. + +// =============> write your new code here function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("hello")); // Hello diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..aad307d76 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -2,19 +2,37 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// I predict a SyntaxError, because decimalNumber is the parameter and is declared +// again inside the function with const. +// Even after fixing that, console.log(decimalNumber) is outside the function, where +// decimalNumber does not exist, so it would cause a ReferenceError. // Try playing computer with the example to work out what is going on +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; +// +// return percentage; +// } +// +// console.log(decimalNumber); + +// =============> write your explanation here +// First error: SyntaxError: Identifier 'decimalNumber' has already been declared. +// The parameter already declares decimalNumber in the function's scope, so const +// cannot declare it again. The hardcoded 0.5 would also ignore whatever value was passed in. +// +// Second error (after removing the const line): ReferenceError: decimalNumber is not defined. +// Parameters only exist inside the function's local scope. Outside the function we +// should call the function and log its return value instead. + +// Finally, correct the code to fix the problem +// =============> write your new code here function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here +console.log(convertToPercentage(0.5)); // 50% diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..0d842c89e 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,30 @@ - // 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 predict a SyntaxError, because 3 is written where a parameter name should be. +// Parameters must be variable names, not values. -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 +// When declaring a function, the brackets hold parameter names: placeholders that +// receive values when the function is called. JavaScript expected a name there and +// found the number 3, which is not a valid name, so it reports an "Unexpected number". +// The body also uses num, which is never declared, so num should be the parameter. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(3)); // 9 diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..a8a020a20 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,30 @@ // Predict and explain first... // =============> write your prediction here +// I predict it prints 320 on one line, then +// "The result of multiplying 10 and 32 is undefined". +// multiply logs the answer but never returns it. -function multiply(a, b) { - console.log(a * b); -} - -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +// 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 +// Actual output: +// 320 +// The result of multiplying 10 and 32 is undefined +// console.log only prints a value to the terminal; it does not send it back to +// the caller. A function with no return statement returns undefined, so the +// template literal receives undefined. The 320 appears first because multiply +// runs (and logs) while the template literal is being built. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> 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)}`); +// The result of multiplying 10 and 32 is 320 \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..a12a4d687 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,25 @@ // Predict and explain first... // =============> write your prediction here +// I predict "The sum of 10 and 32 is undefined", because return is on its own line +// and a + b is on the next line. -function sum(a, b) { - return; - a + b; -} - -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// function sum(a, b) { +// return; +// a + b; +// } +// +// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// Actual output: The sum of 10 and 32 is undefined +// return; with nothing after it ends the function immediately and returns undefined. +// The line a + b is never reached. return must be followed by the value on the same line. + // 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)}`); +// The sum of 10 and 32 is 42 \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..1ba958e87 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,23 +2,43 @@ // Predict the output of the following code: // =============> Write your prediction here +// I predict all three lines will say the last digit is 3, because getLastDigit +// has no parameter and always uses the global num, which is 103. -const num = 103; - -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)}`); +// const num = 103; +// +// 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)}`); // 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 +// getLastDigit is declared with no parameters, so the arguments 42, 105 and 806 +// are ignored. Inside the function, num refers to the global constant 103. +// 103.toString() is "103" and .slice(-1) takes the last character, "3", every time. + // Finally, correct the code to fix the problem // =============> write your new code here +// Give the function a parameter so it uses the number passed in, and remove the global num. +function getLastDigit(num) { + return num.toString().slice(-1); +} + +console.log(`The last digit of 42 is ${getLastDigit(42)}`); // 2 +console.log(`The last digit of 105 is ${getLastDigit(105)}`); // 5 +console.log(`The last digit of 806 is ${getLastDigit(806)}`); // 6 // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem +// It ignored its input because it had no parameter. Adding num as a parameter means +// each call works on the number it was given. diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..9fd3521ec 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -16,4 +16,11 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + const heightSquared = height * height; + const bmi = weight / heightSquared; + return bmi.toFixed(1); // toFixed returns a string rounded to 1 decimal place } + +console.log(calculateBMI(70, 1.73)); // "23.4" +console.log(calculateBMI(85, 1.8)); // "26.2" +console.log(calculateBMI(50, 1.6)); // "19.5" \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..1289f07ed 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(text) { + // replaceAll swaps every space for an underscore, then toUpperCase capitalises every letter + return text.replaceAll(" ", "_").toUpperCase(); +} + +console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS" +console.log(toUpperSnakeCase("code your future")); // "CODE_YOUR_FUTURE" \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..e3ecbda86 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) { + // Remove the trailing "p", e.g. "399p" -> "399" + const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1, + ); + + // Pad with zeros so there is always at least one pounds digit and two pence digits + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + // Everything except the last two digits is pounds + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2, + ); + + // The last two digits are pence + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + return `£${pounds}.${pence}`; +} + +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("5p")); // £0.05 +console.log(toPounds("50p")); // £0.50 +console.log(toPounds("12345p")); // £123.45 \ No newline at end of file diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..201295510 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -22,17 +22,29 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// 3 times: once each for totalHours, remainingMinutes and remainingSeconds in the return line. // Call formatTimeDisplay with an input of 61, now answer the following: +// With seconds = 61: remainingSeconds = 61 % 60 = 1, totalMinutes = (61 - 1) / 60 = 1, +// remainingMinutes = 1 % 60 = 1, totalHours = (1 - 1) / 60 = 0. +// formatTimeDisplay(61) returns "00:01:01". // b) What is the value assigned to num when pad is called for the first time? // =============> write your answer here +// 0. The first call is pad(totalHours), and totalHours is 0. // c) What is the return value of pad when it is called for the first time? // =============> write your answer here +// "00". num.toString() gives "0", which is 1 character long, so the while loop adds +// one "0" to the front, making it 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 +// 1. The last call is pad(remainingSeconds), and remainingSeconds is 61 % 60 = 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". "1" has length 1, so the while loop adds one leading "0", then stops once the +// length is 2. + +console.log(formatTimeDisplay(61)); // 00:01:01 \ No newline at end of file