From 98f2dbe77185f40accce7e0d08723e1d5bede36f Mon Sep 17 00:00:00 2001 From: Samual-Hu Date: Sat, 21 Feb 2026 16:24:11 +0000 Subject: [PATCH 1/4] completed sprint 1 exercises --- Sprint-1/1-key-exercises/1-count.js | 5 +++++ Sprint-1/1-key-exercises/2-initials.js | 4 ++-- Sprint-1/1-key-exercises/3-paths.js | 7 ++++--- Sprint-1/1-key-exercises/4-random.js | 8 ++++++++ Sprint-1/2-mandatory-errors/0.js | 4 ++-- Sprint-1/2-mandatory-errors/1.js | 2 +- Sprint-1/2-mandatory-errors/2.js | 4 ++-- Sprint-1/2-mandatory-errors/3.js | 4 ++-- Sprint-1/2-mandatory-errors/4.js | 7 +++++-- .../3-mandatory-interpret/1-percentage-change.js | 9 +++++---- Sprint-1/3-mandatory-interpret/2-time-format.js | 11 ++++++----- Sprint-1/3-mandatory-interpret/3-to-pounds.js | 16 ++++++++++++++++ 12 files changed, 58 insertions(+), 23 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..d73658664 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,8 @@ count = count + 1; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing + +// Line 3 is reassigning a new value to the 'count' variable. +// In JavaScript, '=' is the assignment operator, not a mathematical equals sign. +// It first evaluates the expression on the right side (count + 1, which is 0 + 1), +// and then assigns that result (1) back to the variable on the left side, updating its value. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..0a1e1535c 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,7 @@ let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string. // This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. -let initials = ``; - +let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`; +console.log(initials); // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..73ba859dc 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,8 @@ console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = ; -const ext = ; - +const dir = filePath.slice(0, lastSlashIndex); +const ext = filePath.slice(filePath.lastIndexOf(".")); +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${filePath} is ${ext}`); // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..a979d4e11 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,11 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num and running the program several times to build an idea of what the program is doing +console.log(num); +// Answer: The variable `num` represents a random whole number between 1 and 100 (inclusive). +// Breakdown of the expression: +// 1. Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive). +// 2. (maximum - minimum + 1) calculates the total number of possible integers in our range (which is 100). +// 3. Multiplying them scales the random decimal to be between 0 and 99.999... +// 4. Math.floor() rounds that decimal down to the nearest whole number (getting an integer from 0 to 99). +// 5. Finally, '+ minimum' shifts the starting point up by 1, resulting in a final integer from 1 to 100. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..65ad3030d 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,2 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +//This is just an instruction for the first activity - but it is just for human consumption +//We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..031839b47 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,4 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..c46e98083 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,5 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? - -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..ab4ae9f65 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,6 +1,6 @@ -const cardNumber = 4533787178994213; +const cardNumber = "4533787178994213"; const last4Digits = cardNumber.slice(-4); - +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working // Before running the code, make and explain a prediction about why the code won't work diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..960dd48cf 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,5 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const time12Hour = "20:53"; +const time24Hour = "08:53"; + +console.log(time12Hour); +console.log(time24Hour); \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..db10ffebc 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,11 +12,12 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made - +// Answer: There are 5 function calls in total. They are located on lines 4, 5, and 10. // b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? - +// Answer: The error comes from line 5. It occurs because we are trying to reassign a value to `priceAfterOneYear`, which was originally declared as a `const`. To fix it, change `const` to `let` on line 2. // c) Identify all the lines that are variable reassignment statements - +// Answer: Lines 4 and 5. // d) Identify all the lines that are variable declarations - +// Answer: Lines 1, 2, 7, and 8. // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// Answer: It removes the comma from the string using `replaceAll()`, and then converts that cleaned string into a number type using `Number()`. The purpose is to convert a formatted text string into a valid mathematical number so we can do calculations with it later. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..ee3e57f7a 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,15 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions // a) How many variable declarations are there in this program? - +// Answer: There are 6 variable declarations. (They are: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result). // b) How many function calls are there? - +// Answer: There is exactly 1 function call (console.log on line 10). // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators - +// Answer: The `%` symbol is the remainder (modulo) operator. `movieLength % 60` calculates the leftover seconds that do not fit into a full 60-second minute. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? - +// Answer: It subtracts the leftover seconds from the total seconds to get a number cleanly divisible by 60, then divides by 60 to find out exactly how many full minutes are in the movie. // e) What do you think the variable result represents? Can you think of a better name for this variable? - +// Answer: `result` represents the final formatted time string (Hours:Minutes:Seconds). A better, more descriptive name could be `formattedTime` or `movieDurationString`. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Answer: It works for positive whole numbers. However, it won't format single-digit numbers perfectly (e.g., it prints 2:5:9 instead of 02:05:09). Also, it might produce weird results or decimals if we input negative numbers or fractional seconds. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..f9380dbc3 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,19 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// 2. const penceStringWithoutTrailingP = ... +// Removes the last character (the "p") from the string using substring(), leaving just the numbers. + +// 3. const paddedPenceNumberString = ... +// Uses padStart(3, "0") to make sure the string is at least 3 characters long by adding "0"s to the front. + +// 4. const pounds = ... +// Uses substring() to extract all characters EXCEPT the last two. This isolates the pound value. + +// 5. const pence = ... +// Extracts exactly the last two characters for the pence value using substring(), +// and uses padEnd() as a safety measure to ensure it's exactly 2 digits long. + +// 6. console.log(...) +// Uses template literals to piece the pounds and pence back together with a "£" and "." symbol, printing "£3.99". \ No newline at end of file From d90d6117b48d605384f4e83062d15201df7c1827 Mon Sep 17 00:00:00 2001 From: Samual-Hu Date: Sun, 22 Feb 2026 13:49:36 +0000 Subject: [PATCH 2/4] Fix: add error explanations and correct function call count --- Sprint-1/2-mandatory-errors/0.js | 7 ++++++- Sprint-1/2-mandatory-errors/1.js | 4 ++++ Sprint-1/2-mandatory-errors/2.js | 3 +++ Sprint-1/2-mandatory-errors/3.js | 4 ++++ Sprint-1/2-mandatory-errors/4.js | 6 +++++- Sprint-1/3-mandatory-interpret/1-percentage-change.js | 2 +- 6 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index 65ad3030d..b6fb6d9cb 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,7 @@ //This is just an instruction for the first activity - but it is just for human consumption -//We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +//We don't want the computer to run these 2 lines - how can we solve this problem? + +// Explanation: +// Running this without comments gives a "SyntaxError". +// This occurs because the computer tries to read plain English sentences as JavaScript code, which it cannot understand. +// Adding '//' turns them into comments that the computer ignores. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 031839b47..5a282f8fd 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,7 @@ let age = 33; age = age + 1; + +// Explanation: +// The error is "TypeError: Assignment to constant variable." +// This occurs because we used 'const' to declare the 'age' variable. 'const' creates a constant whose value cannot be reassigned after its initial creation. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index c46e98083..16cf15164 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,6 @@ const cityOfBirth = "Bolton"; console.log(`I was born in ${cityOfBirth}`); +// Explanation: +// The error is "ReferenceError: Cannot access 'cityOfBirth' before initialization." +// JavaScript executes code from top to bottom. We tried to print 'cityOfBirth' before we actually declared and initialized it on the next line. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ab4ae9f65..fb7e57d47 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -7,3 +7,7 @@ console.log(last4Digits); // Then run the code and see what error it gives. // Consider: Why does it give this error? Is this what I predicted? If not, what's different? // Then try updating the expression last4Digits is assigned to, in order to get the correct value + +// Explanation: +// The error is "TypeError: cardNumber.slice is not a function." +// This occurs because the original cardNumber was a Number type. The '.slice()' method only works on Strings and Arrays, not on pure numbers. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 960dd48cf..393b54f35 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -2,4 +2,8 @@ const time12Hour = "20:53"; const time24Hour = "08:53"; console.log(time12Hour); -console.log(time24Hour); \ No newline at end of file +console.log(time24Hour); + +// Explanation: +// The error is a "SyntaxError". +// This occurs because it violates JavaScript naming conventions: variable names cannot start with a number. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index db10ffebc..c9a31029c 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,7 +12,7 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made -// Answer: There are 5 function calls in total. They are located on lines 4, 5, and 10. +// Answer: There are 2 function calls in total. They are both `Number()` and are located on lines 4 and 5. // b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? // Answer: The error comes from line 5. It occurs because we are trying to reassign a value to `priceAfterOneYear`, which was originally declared as a `const`. To fix it, change `const` to `let` on line 2. // c) Identify all the lines that are variable reassignment statements From e4ad14a09b479a37bf1734428a3a61f75648f47c Mon Sep 17 00:00:00 2001 From: Samual-Hu Date: Wed, 25 Feb 2026 12:28:48 +0000 Subject: [PATCH 3/4] Fix: correct answers for questions a and b in 1-percentage-change.js --- .../3-mandatory-interpret/1-percentage-change.js | 4 ++-- Sprint-2/1-key-errors/0.js | 11 ++++++++--- Sprint-2/1-key-errors/1.js | 12 ++++++++---- Sprint-2/1-key-errors/2.js | 15 ++++++++++----- Sprint-2/2-mandatory-debug/0.js | 11 ++++++++--- Sprint-2/2-mandatory-debug/1.js | 10 ++++++++-- Sprint-2/2-mandatory-debug/2.js | 16 +++++++++++++--- 7 files changed, 57 insertions(+), 22 deletions(-) diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index c9a31029c..fa2842603 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -12,9 +12,9 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made -// Answer: There are 2 function calls in total. They are both `Number()` and are located on lines 4 and 5. +// Answer: There are 5 function calls in total. They are located on lines 4, 5, and 10. // b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? -// Answer: The error comes from line 5. It occurs because we are trying to reassign a value to `priceAfterOneYear`, which was originally declared as a `const`. To fix it, change `const` to `let` on line 2. +// Answer: The error comes from line 4. Since JavaScript executes from top to bottom, it crashes on line 4 because we are trying to reassign a new value to `carPrice`, which was originally declared as a `const`. To fix it, change `const` to `let` on lines 1 and 2. // c) Identify all the lines that are variable reassignment statements // Answer: Lines 4 and 5. // d) Identify all the lines that are variable declarations diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..706d9c5c9 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,18 @@ // Predict and explain first... // =============> write your prediction here - +// It will crash with a SyntaxError. We can't use 'let' to declare 'str' because 'str' is already taken by the function parameter. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring - +/* I commented this out so it doesn't crash: function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } - +*/ // =============> write your explanation here +//The error confirms "str has already been declared". Function parameters act as local variables, creating a naming conflict with `let str`. // =============> write your new code here +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} +console.log(capitalise("hello")); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..58d527fab 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,9 +2,9 @@ // Why will an error occur when this program runs? // =============> write your prediction here - +// It will crash for two reasons. 1. Trying to log a local variable 'decimalNumber' outside its function. 2. Trying to re-declare the parameter 'decimalNumber' using 'const' inside the function. // Try playing computer with the example to work out what is going on - +/* Commented out broken code: function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -13,8 +13,12 @@ function convertToPercentage(decimalNumber) { } console.log(decimalNumber); - +*/ // =============> write your explanation here - +// 'decimalNumber' is scoped to the function, so console.log outside can't see it (ReferenceError). Inside, re-declaring the parameter with 'const' causes a SyntaxError. Also, the function was never actually called. // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + return `${decimalNumber * 100}%`; +} +console.log(convertToPercentage(0.5)); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..49e61a701 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,22 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here - +// Syntax error. We put a hardcoded number '3' where a parameter name (like 'num') should be. +/* Commented out the broken code so it doesn't crash: function square(3) { return num * num; } - +*/ // =============> write the error message here - +// SyntaxError: Unexpected number // =============> explain this error message here - +// Function definitions require variable names as parameters to act as placeholders, not literal values. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(3)); +console.log(square(4)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..1f0a6c30e 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... // =============> write your prediction here - +//It will print "320", followed by "The result... is undefined". The function lacks a return value. +/* Commented out the broken code: 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 - +// `console.log` just prints to the screen. Without a `return` statement, the function evaluates to `undefined`, which gets injected into the template literal. // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..80b7f5e72 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,19 @@ // Predict and explain first... // =============> write your prediction here - +// It will print "undefined". The 'return;' statement stops the function immediately before it even looks at 'a + b'. +/* Commented out the broken code: function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); - +*/ // =============> write your explanation here +// The 'return' keyword instantly exits the function. Since nothing is attached to it, it returns 'undefined'. The 'a + b;' line is dead code that never gets executed. // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..cbfa01f2d 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,8 @@ // Predict the output of the following code: // =============> Write your prediction here - +// It will always print "3" for every log. The function ignores the numbers we pass in and blindly uses the global 'num' (103). +/* Commented out the broken code: const num = 103; function getLastDigit() { @@ -12,13 +13,22 @@ 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 +// The function lacks a parameter in its definition. Therefore, it ignores the arguments (42, 105, 806) and always falls back to the globally defined 'num' which is 103. // Finally, correct the code to fix the problem // =============> write your new code here - +function getLastDigit(number) { + return number.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 From d2210f1c8349d9ace5cdb30ba4482e1abe2bcb15 Mon Sep 17 00:00:00 2001 From: Samual-Hu Date: Wed, 25 Feb 2026 12:39:25 +0000 Subject: [PATCH 4/4] Fix: Remove accidentally committed Sprint 2 files --- Sprint-2/1-key-errors/0.js | 11 +++-------- Sprint-2/1-key-errors/1.js | 12 ++++-------- Sprint-2/1-key-errors/2.js | 15 +++++---------- Sprint-2/2-mandatory-debug/0.js | 11 +++-------- Sprint-2/2-mandatory-debug/1.js | 10 ++-------- Sprint-2/2-mandatory-debug/2.js | 16 +++------------- 6 files changed, 20 insertions(+), 55 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 706d9c5c9..653d6f5a0 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,18 +1,13 @@ // Predict and explain first... // =============> write your prediction here -// It will crash with a SyntaxError. We can't use 'let' to declare 'str' because 'str' is already taken by the function parameter. + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring -/* I commented this out so it doesn't crash: + function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } -*/ + // =============> write your explanation here -//The error confirms "str has already been declared". Function parameters act as local variables, creating a naming conflict with `let str`. // =============> write your new code here -function capitalise(str) { - return `${str[0].toUpperCase()}${str.slice(1)}`; -} -console.log(capitalise("hello")); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index 58d527fab..f2d56151f 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,9 +2,9 @@ // Why will an error occur when this program runs? // =============> write your prediction here -// It will crash for two reasons. 1. Trying to log a local variable 'decimalNumber' outside its function. 2. Trying to re-declare the parameter 'decimalNumber' using 'const' inside the function. + // Try playing computer with the example to work out what is going on -/* Commented out broken code: + function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; @@ -13,12 +13,8 @@ function convertToPercentage(decimalNumber) { } console.log(decimalNumber); -*/ + // =============> write your explanation here -// 'decimalNumber' is scoped to the function, so console.log outside can't see it (ReferenceError). Inside, re-declaring the parameter with 'const' causes a SyntaxError. Also, the function was never actually called. + // Finally, correct the code to fix the problem // =============> write your new code here -function convertToPercentage(decimalNumber) { - return `${decimalNumber * 100}%`; -} -console.log(convertToPercentage(0.5)); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index 49e61a701..aad57f7cf 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,22 +4,17 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here -// Syntax error. We put a hardcoded number '3' where a parameter name (like 'num') should be. -/* Commented out the broken code so it doesn't crash: + function square(3) { return num * num; } -*/ + // =============> write the error message here -// SyntaxError: Unexpected number + // =============> explain this error message here -// Function definitions require variable names as parameters to act as placeholders, not literal values. + // Finally, correct the code to fix the problem // =============> write your new code here -function square(num) { - return num * num; -} -console.log(square(3)); -console.log(square(4)); + diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index 1f0a6c30e..b27511b41 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,19 +1,14 @@ // Predict and explain first... // =============> write your prediction here -//It will print "320", followed by "The result... is undefined". The function lacks a return value. -/* Commented out the broken code: + 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 -// `console.log` just prints to the screen. Without a `return` statement, the function evaluates to `undefined`, which gets injected into the template literal. + // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 80b7f5e72..37cedfbcf 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,19 +1,13 @@ // Predict and explain first... // =============> write your prediction here -// It will print "undefined". The 'return;' statement stops the function immediately before it even looks at 'a + b'. -/* Commented out the broken code: + function sum(a, b) { return; a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -*/ + // =============> write your explanation here -// The 'return' keyword instantly exits the function. Since nothing is attached to it, it returns 'undefined'. The 'a + b;' line is dead code that never gets executed. // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index cbfa01f2d..57d3f5dc3 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,8 +2,7 @@ // Predict the output of the following code: // =============> Write your prediction here -// It will always print "3" for every log. The function ignores the numbers we pass in and blindly uses the global 'num' (103). -/* Commented out the broken code: + const num = 103; function getLastDigit() { @@ -13,22 +12,13 @@ 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 -// The function lacks a parameter in its definition. Therefore, it ignores the arguments (42, 105, 806) and always falls back to the globally defined 'num' which is 103. // Finally, correct the code to fix the problem // =============> write your new code here -function getLastDigit(number) { - return number.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