Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sprint-2/1-key-exercises/1-count.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Your explanation of line 3 and the assignment operator = is very clear and precise.
Just one quick question: Is there an even shorter shorthand way to write count = count + 1 in JavaScript?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much. Yes it's count++;

Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ 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 takes the current value of count, adds 1 to it, and assigns the new value back to count.
//The = sign is the assignment operator. It assigns the value on the right side to the variable on the left side.
2 changes: 1 addition & 1 deletion Sprint-2/1-key-exercises/2-initials.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! That's a very clean and clever use of template literals and bracket notation.

Quick question: Is there another built-in string method (besides bracket notation) you could use to get the first character of each string? Why might you choose one over the other?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I could also use charAt(0) to get the first character of each string. For example, firstName.charAt(0). I used bracket notation because it is shorter and easier for me to read, but charAt() is also useful because it clearly shows that I am getting a character from the string.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const 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.

const initials = ``;
const initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;

// https://www.google.com/search?q=get+first+character+of+string+mdn
5 changes: 3 additions & 2 deletions Sprint-2/1-key-exercises/3-paths.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spot on! This is a very clean and precise use of lastIndexOf and slice.

Quick question: What would happen if the path didn't have a file extension (for example, just a folder name)? How might you handle that edge case?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if there is no file extension, lastIndexOf(".") would return -1. I could check for that first, and if it returns -1, I would know that there is no extension.

Original file line number Diff line number Diff line change
Expand Up @@ -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 lastDotIndex = filePath.lastIndexOf(".");
const ext = filePath.slice(lastDotIndex);

// https://www.google.com/search?q=slice+mdn
4 changes: 4 additions & 0 deletions Sprint-2/1-key-exercises/4-random.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great explanation! To answer your question: Math.random() is used whenever you need unpredictability, like in games, randomized lists, or security tokens.
The solution branch also uses console.assert(). How do you think using console.assert() helps test code automatically compared to reading a console.log?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think console.assert() helps because it checks if the result is what I expected automatically. If the result is wrong, it shows an error, while with console.log() I would have to check the result myself.

Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ 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

// num represents a random whole number between 1 and 100, including both 1 and 100.
// Math.random() generates a random decimal number.
// Math.floor() rounds the result down to the nearest whole number.
4 changes: 2 additions & 2 deletions Sprint-2/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -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?
// 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?
2 changes: 1 addition & 1 deletion Sprint-2/2-mandatory-errors/1.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job spotting and fixing the TypeError! Changing const to let is the exact right solution since the variable needs to be reassigned.
Quick question: Why do you think JavaScript throws an error if you try to reassign a const variable?

@Thabimalindi Thabimalindi Oct 1, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think JavaScript gives an error because const means the value should not be changed. Once I give it a value, I cannot assign a new value to it. If I need the value to change, I should use let.

Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion Sprint-2/2-mandatory-errors/2.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job finding and fixing the reference error! Moving console.log below the variable declaration is the right fix.
Quick question: Why do you think JavaScript doesn't let you use a variable declared with const before it is initialized?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is because the const variable has not been given a value yet. JavaScript needs it to be declared and initialized first before I can use it.

Original file line number Diff line number Diff line change
@@ -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}`);
9 changes: 3 additions & 6 deletions Sprint-2/2-mandatory-errors/3.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Your prediction, debugging process, and use of console.assert() are spot on.

Quick question: Do you think last4Digits needs to be converted back to a number for typical use cases (like showing the last 4 digits on a receipt), or is keeping it as a string actually better? Why?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think keeping it as a string is better because we are only displaying the last 4 digits, not doing calculations with them.

Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.toString().slice(-4);

// 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
// 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
// The original code doesn't work because cardNumber is a number and .slice() cannot be used on a number.
// Converting cardNumber to a string allows .slice(-4) to return the last 4 characters.
4 changes: 2 additions & 2 deletions Sprint-2/2-mandatory-errors/4.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! You successfully used valid variable names that follow the naming rules by starting with letters.

Quick question: Besides letters, what are the other two special characters that JavaScript allows you to use at the beginning of a variable name?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other two special characters are the underscore _ and the dollar sign $. So a JavaScript variable name can start with a letter, _, or $.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
const twelveHourClockTime = "8:53pm";
const twentyFourHourClockTime = "20:53";
36 changes: 23 additions & 13 deletions Sprint-2/3-mandatory-interpret/1-percentage-change.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Your breakdown of function calls, variable declarations, reassignments, and how replaceAll works with Number is very thorough and accurate.
Quick question: Is there any other method in JavaScript besides replaceAll() that you could use to remove characters from a string, or is this the best approach for commas?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I could also use replace() to remove a comma. I think replaceAll() is better here because it will remove all the commas if the number has more than one.

Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@ let carPrice = "10,000";
let priceAfterOneYear = "8,543";

carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;

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

// 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?

// c) Identify all the lines that are variable reassignment statements

// d) Identify all the lines that are variable declarations

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// a) There are 5 function calls.
// carPrice.replaceAll(",", "")
// Number(carPrice.replaceAll(",", ""))
// priceAfterOneYear.replaceAll(",", "")
// Number(priceAfterOneYear.replaceAll(",", ""))
// console.log(`The percentage change is ${percentageChange}`)

// b) The error is on line 5 because there is a missing comma between "," and "".
// It should be replaceAll(",", "").

// c) The variable reassignment statements are:
// carPrice = Number(carPrice.replaceAll(",", ""));
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

// d) The variable declarations are:
// let carPrice = "10,000";
// let priceAfterOneYear = "8,543";
// const priceDifference = carPrice - priceAfterOneYear;
// const percentageChange = (priceDifference / carPrice) * 100;

// e) Number(carPrice.replaceAll(",", "")) removes the comma from the carPrice string
// and then converts the result from a string into a number.
20 changes: 13 additions & 7 deletions Sprint-2/3-mandatory-interpret/2-time-format.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Your breakdown is extremely thorough, and I love your suggestion to rename result to formattedMovieLength, as well as your attention to edge cases like decimals.

Quick question: If movieLength could include decimals, what built-in Math method could you use to ensure you are always working with whole numbers?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could use Math.floor() to remove the decimal part and make sure I am working with a whole number.

Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ 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?
// a) There are 6 variable declarations:
// movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours and result.

// b) How many function calls are there?
// b) There is 1 function call:
// console.log(result);

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
// c) movieLength % 60 gives the remainder after dividing movieLength by 60.
// This represents the number of seconds left after converting the rest into whole minutes.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// d) (movieLength - remainingSeconds) / 60 removes the leftover seconds
// and divides the remaining seconds by 60 to calculate the total number of whole minutes.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// e) result represents the movie length formatted as hours:minutes:seconds.
// A better variable name could be formattedMovieLength.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// f) The code works correctly for non-negative whole numbers of seconds.
// If movieLength contains a decimal value, the result may contain decimal seconds.
// Negative values would also not represent a normal movie duration.
31 changes: 27 additions & 4 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const penceStringWithoutTrailingP = penceString.substring(
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
Expand All @@ -20,8 +21,30 @@ console.log(`£${pounds}.${pence}`);
// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds

// You need to do a step-by-step breakdown of each line in this program
// Try and describe the purpose / rationale behind each step
// 1. const penceString = "399p":
// Initialises a string variable with the value "399p".

// 2. penceString.substring(0, penceString.length - 1):
// Removes the last character "p" from the string.
// The result is "399".

// 3. penceStringWithoutTrailingP.padStart(3, "0"):
// Makes sure the string has at least 3 characters by adding "0" to the start if needed.
// "399" already has 3 characters, so it remains "399".

// 4. paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2):
// Takes everything except the last 2 characters.
// This gives us the pounds part of the price.
// The result is "3".

// 5. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2):
// Takes the last 2 characters of the string.
// This gives us the pence part of the price.
// The result is "99".

// 6. .padEnd(2, "0"):
// Makes sure the pence part has 2 characters.

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 7. console.log(`£${pounds}.${pence}`):
// Combines the pounds and pence and prints the formatted price.
// The final output is £3.99.
16 changes: 13 additions & 3 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// I predict an error will occur because str is already the name of the function parameter.

// call the function capitalise with a string input
capitalise("hello");

// 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
// =============> write your new code here
// The error occurs because str is declared twice in the same scope.
// str is already a parameter of the capitalise function, so we cannot declare it again using let.

// New code:
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

console.log(capitalise("hello"));
15 changes: 12 additions & 3 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// I predict an error will occur because decimalNumber is already the name
// of the function parameter and it is declared again using const.

// Try playing computer with the example to work out what is going on

Expand All @@ -14,7 +15,15 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// The error occurs because decimalNumber is declared twice in the same scope.
// It is already a parameter of the function, so it cannot be declared again using const.

// Finally, correct the code to fix the problem
// =============> write your new code here

function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(convertToPercentage(0.5));
23 changes: 14 additions & 9 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@

// 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 there will be a syntax error because 3 is a number
// and a function parameter must be a valid variable name.

function square(3) {
return num * num;
}
// function square(3) {
// return num * num;
// }

// =============> write the error message here
// Error message:
// Identifier expected.

// =============> explain this error message here
// The error occurs because 3 is being used as the function parameter.
// A function parameter must be a valid variable name, such as num.
// The number 3 should be passed into the function as an argument instead.

// Finally, correct the code to fix the problem

// =============> write your new code here

function square(num) {
return num * num;
}

console.log(square(3));
16 changes: 13 additions & 3 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
// Predict and explain first...

// =============> write your prediction here
// I predict the multiplication will calculate 320,
// but the final message will show undefined because the function
// logs the result instead of returning it.

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
// The multiply function uses console.log() to display the result,
// but it does not return a value.
// A function without a return statement returns undefined.
// This is why the final message contains undefined.

// 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)}`);
16 changes: 13 additions & 3 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here

// I predict the result will be undefined because the return statement
// does not return the result of a + b.

function sum(a, b) {
return;
Expand All @@ -8,6 +10,14 @@ function sum(a, b) {

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The function returns immediately when it reaches the return statement.
// Because there is no value after return, the function returns undefined.
// The line a + b is never reached.

// 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)}`);
26 changes: 20 additions & 6 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// I predict that all three console.log statements will show 3
// because the function uses the global variable num, which is 103.

const num = 103;

Expand All @@ -14,11 +15,24 @@ 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 output is:
// 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 getLastDigit function does not have a parameter.
// It always uses the global variable num, which has the value 103.
// Therefore, the arguments 42, 105 and 806 are ignored.
// The last digit of 103 is 3, so the function returns 3 every time.

// 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
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)}`);
4 changes: 4 additions & 0 deletions Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
function calculateBMI(weight, height) {
const bmi = weight / (height * height);
return bmi.toFixed(1);
}
Loading
Loading