diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..d7bc3f3e53 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,23 @@ // Predict and explain first... -// =============> write your prediction here +// Prediction: SyntaxError because `str` is re-declared with `let` inside the function +// where it already exists as a parameter. You cannot use `let` to re-declare a variable +// that already exists in the same scope. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +// Original broken code: +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } + +// Explanation: The parameter `str` already exists in the function scope. +// Using `let str` tries to re-declare it, which is a SyntaxError. +// Instead of reassigning `str` (which represents the original input), +// we use a new const `capitalisedStr` to make it clear this is a different value. + function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + const capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; } - -// =============> write your explanation here -// =============> write your new code here diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..1f4cabacba 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,28 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// Prediction: Two errors — (1) re-declaring decimalNumber with const inside the function +// when it is already a parameter causes a SyntaxError. +// (2) console.log(decimalNumber) outside the function will throw ReferenceError because +// decimalNumber is not defined in the outer scope. // Try playing computer with the example to work out what is going on +// Original broken code: +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; +// return percentage; +// } +// console.log(decimalNumber); + +// Explanation: The parameter decimalNumber already exists in the function scope; +// const re-declaration is illegal. Also, decimalNumber is not accessible outside the function. +// Fix: remove the const re-declaration inside the function, and pass a value when calling it. + 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)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..fd9e0e3310 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,19 @@ - // 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 - -function square(3) { - return num * num; -} - -// =============> write the error message here +// Prediction: SyntaxError — a function parameter must be a valid identifier, not a number literal. -// =============> explain this error message here - -// Finally, correct the code to fix the problem - -// =============> write your new code here +// Original broken code: +// function square(3) { +// return num * num; +// } +// Error message: SyntaxError: Unexpected number +// Explanation: `3` is a number literal and cannot be used as a parameter name. +// Parameters must be valid identifiers (variable names). +// Fix: use a named parameter like `num`. +function square(num) { + return num * num; +} diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..83cea4380d 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,19 @@ // Predict and explain first... +// Prediction: The template literal will print "undefined" for the function result because +// multiply uses console.log internally but does not return a value (returns undefined). -// =============> write your prediction here +// Original broken code: +// function multiply(a, b) { +// console.log(a * b); +// } +// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + +// Explanation: Functions without a return statement return undefined. +// The outer console.log then interpolates undefined into the string. +// Fix: replace console.log inside multiply with a return statement. function multiply(a, b) { - console.log(a * b); + return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..ff7d5fb412 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,20 @@ // Predict and explain first... -// =============> write your prediction here +// Prediction: The sum will print "undefined" because return; exits the function +// immediately without a value, so a + b is never evaluated. + +// Original broken code: +// function sum(a, b) { +// return; +// a + b; +// } +// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +// Explanation: return; with no expression returns undefined. +// The a + b on the next line is unreachable dead code and is never executed. +// Fix: put a + b on the same line as return. function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); - -// =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..c1d8633ea9 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,29 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// Prediction: All three logs will print "3" (the last digit of the module-level const num = 103) +// because getLastDigit ignores its parameter and always uses the outer num. +// Expected output: +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 -const num = 103; +// Original broken code: +// const num = 103; +// function getLastDigit() { +// return num.toString().slice(-1); +// } -function getLastDigit() { +// Actual output matched prediction — all printed "3". + +// Explanation: The function has no parameter, so any argument passed in is discarded. +// It always reads the outer `num` variable which is 103. +// Fix: add a parameter to getLastDigit and use it instead of the outer variable. + +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)}`); - -// Now run the code and compare the output to your prediction -// =============> write the output here -// Explain why the output is the way it is -// =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here - -// 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 17b1cbde1b..305dad6a11 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,5 @@ // 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 + return Number((weight / (height * height)).toFixed(1)); } \ 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..ca893c05f1 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -1,16 +1,6 @@ -// A set of words can be grouped together in different cases. +function toUpperSnakeCase(str) { + return str.replaceAll(" ", "_").toUpperCase(); +} -// For example, "hello there" in snake case would be written "hello_there" -// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. - -// Implement a function that: - -// Given a string input like "hello there" -// When we call this function with the input string -// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE" - -// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" - -// 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 +console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // "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 6265a1a703..d58031cd52 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -1,6 +1,12 @@ -// In Sprint-1, there is a program written in interpret/to-pounds.js +function toPounds(penceString) { + const withoutP = penceString.substring(0, penceString.length - 1); + const padded = withoutP.padStart(3, "0"); + const pounds = padded.substring(0, padded.length - 2); + const pence = padded.substring(padded.length - 2).padEnd(2, "0"); + return `£${pounds}.${pence}`; +} -// You will need to take this code and turn it into a reusable block of code. -// 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 +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("9p")); // £0.09 +console.log(toPounds("50p")); // £0.50 +console.log(toPounds("1000p")); // £10.00 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..0d53b353aa 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -21,18 +21,19 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> pad is called 3 times per call to formatTimeDisplay (once for totalHours, once for remainingMinutes, once for remainingSeconds). // Call formatTimeDisplay with an input of 61, now answer the following: +// (remainingSeconds = 1, totalMinutes = 1, remainingMinutes = 1, totalHours = 0) // b) What is the value assigned to num when pad is called for the first time? -// =============> write your answer here +// =============> num = 0 (totalHours), because totalHours is the first argument in the template literal. -// c) What is the return value of pad is called for the first time? -// =============> write your answer here +// c) What is the return value of pad when it is called for the first time? +// =============> "00" — "0".length < 2, so "0" is prepended, giving "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 +// =============> num = 1 (remainingSeconds). It is the last argument passed in the template literal, so pad is called with it last. // 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".length < 2, so "0" is prepended, giving "01".