From d7431d77161255f5f18a5192746a9c784921416b Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Sun, 5 Jul 2026 20:20:14 +0100 Subject: [PATCH] Complete task for Sprint-2 cleanly --- Sprint-2/1-key-errors/0.js | 14 ++++------ Sprint-2/1-key-errors/1.js | 22 ++++++--------- Sprint-2/1-key-errors/2.js | 23 ++++----------- Sprint-2/2-mandatory-debug/0.js | 15 ++++------ Sprint-2/2-mandatory-debug/1.js | 14 ++++------ Sprint-2/2-mandatory-debug/2.js | 27 +++++++----------- Sprint-2/3-mandatory-implement/1-bmi.js | 2 +- Sprint-2/3-mandatory-implement/2-cases.js | 20 ++++--------- Sprint-2/3-mandatory-implement/3-to-pounds.js | 16 +++++++---- Sprint-2/4-mandatory-interpret/time-format.js | 28 ++++++++----------- 10 files changed, 70 insertions(+), 111 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..ab694faa1b 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,9 @@ -// Predict and explain first... -// =============> write your prediction here - -// call the function capitalise with a string input -// interpret the error message and figure out why an error is occurring +// Predict: SyntaxError or ReferenceError because `str` is re-declared with `let` inside the function +// where it already exists as a parameter. +// Explanation: You cannot use `let` to re-declare a variable that already exists in the same scope. +// Fix: remove the `let` keyword — just reassign str. 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 diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..ec47cab7a4 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,14 @@ -// Predict and explain first... - -// Why will an error occur when this program runs? -// =============> write your prediction here - -// Try playing computer with the example to work out what is going on +// Prediction: Two errors — (1) re-declaring decimalNumber with const inside the function +// when it is already a parameter shadows and causes a SyntaxError. +// (2) console.log(decimalNumber) outside the function will throw ReferenceError because +// decimalNumber is not defined in the outer scope. +// Explanation: The parameter decimalNumber already exists; 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 to the function. 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..f1691d09f2 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,9 @@ -// Predict and explain first BEFORE you run any code... +// Prediction: SyntaxError — a function parameter must be a valid identifier, not a literal number. +// Error: SyntaxError: Unexpected number +// Explanation: `3` is a number literal and cannot be used as a parameter name. +// Fix: use a named parameter like `num`. -// 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; +function square(num) { + return num * num; } - -// =============> write the error message here - -// =============> explain this error message here - -// Finally, correct the code to fix the problem - -// =============> write your new code here - - diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..f1b522c21f 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,11 @@ -// Predict and explain first... - -// =============> write your prediction here +// 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). +// 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..3e7c689a10 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,11 @@ -// 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. +// Explanation: return; with no expression returns undefined. The a + b on the next +// line is unreachable dead code. +// 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..90ad3ac10b 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,17 @@ -// Predict and explain first... +// 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. +// Output: +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 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. -// Predict the output of the following code: -// =============> Write your prediction here - -const num = 103; - -function getLastDigit() { +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..aaf374025d 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -15,24 +15,20 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } -// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit -// to help you answer these questions +// a) pad is called 3 times per call to formatTimeDisplay (once for hours, minutes, seconds). -// Questions +// Call formatTimeDisplay(61): +// remainingSeconds = 61 % 60 = 1 +// totalMinutes = (61 - 1) / 60 = 1 +// remainingMinutes = 1 % 60 = 1 +// totalHours = (1 - 1) / 60 = 0 +// pad is called with: totalHours=0, remainingMinutes=1, remainingSeconds=1 -// a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// b) num = 0 (totalHours) when pad is called for the first time. -// Call formatTimeDisplay with an input of 61, now answer the following: +// c) pad(0): "0".length < 2, so prepend "0" -> "00". Return value: "00" -// b) What is the value assigned to num when pad is called for the first time? -// =============> write your answer here +// d) num = 1 (remainingSeconds) when pad is called for the last time. +// It is the last argument passed in the template literal. -// c) What is the return value of pad is called for the first time? -// =============> write your answer here - -// 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 - -// 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 +// e) pad(1): "1".length < 2, so prepend "0" -> "01". Return value: "01"