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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
*.swp
**/.DS_Store
3 changes: 3 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ 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

/* The line 3 is increasing the variable count by 1 and assigning the result to count variable on the left hand * side.
*/
Comment on lines +8 to +9
Copy link
Contributor

Choose a reason for hiding this comment

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

Operation like count = count + 1 is very common in programming, and there is a programming term describing such operation.

Can you find out what one-word programming term describes the operation on line 3?

Copy link
Author

@carlosyabreu carlosyabreu Mar 5, 2026

Choose a reason for hiding this comment

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

Evening @cjyuan

Absolutely sure.
The operator = is called the assignment operator.
On the right hand side of =, the expression count + 1 is evaluated first.
Since count currently has the value 0, count + 1 results in 1.
Then the result 1 is assigned to the variable count on the left hand side of the operator =.
At this stage the value stored in count becomes 1.
To wrap up the line 3 does is to take the current value of count, add 1 to it, and store the result back into count.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was asking for the programming term for operation like "count = count + 1" or "count++".

3 changes: 2 additions & 1 deletion Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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

20 changes: 19 additions & 1 deletion Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/**
// The diagram below shows the different names for parts of a file path on a Unix operating system

// ┌─────────────────────┬────────────┐
Expand All @@ -20,4 +21,21 @@ console.log(`The base part of ${filePath} is ${base}`);
const dir = ;
const ext = ;

// https://www.google.com/search?q=slice+mdn
// https://www.google.com/search?q=slice+mdn
*/

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);

// Create a variable to store the dir part of the filePath variable
const dir = filePath.slice(0, lastSlashIndex + 1);

// Create a variable to store the ext part of the variable
const lastDotIndex = base.lastIndexOf(".");
const ext = lastDotIndex !== -1 ? base.slice(lastDotIndex) : "";

console.log(`The dir part is: ${dir}`);
console.log(`The extension is: ${ext}`);

57 changes: 57 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,64 @@ const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;

console.log(num);

// In this exercise, you will need to work out what num represents?
// 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

/**
* Answear:
*
* 1. The constants
*
* const minimum = 1;
* const maximum = 100;
* These are the lowest and highest possible values for the random number we are generating.
*
* 2. The expression inside Math.random()
*
* Math.random()
*
* This returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).
* So it could be 0.1234, 0.9876, etc., but never exactly 1.
*
* 3. Inside Math.floor()
*
* Math.random() * (maximum - minimum + 1) + minimum
*
* First, we compute:
*
* (maximum - minimum + 1)
*
* With minimum = 1, maximum = 100, this is:
* 100 - 1 + 1 = 100
* So it’s 100. This is the total number of possible integers from 1 to 100 inclusive.
* So the expression becomes:
*
* 4. The range of values
*
* Let’s check possible values step by step:
*
* Case 1: smallest possible random = 0
* 0 * 100 + 1 = 1
*
* Case 2: largest possible random = 0.999… (less than 1)
* 0.999… * 100 = 99.999…
* 99.999… + 1 = 100.999…
*
* But Math.floor() will round down to the nearest integer.
*
* 5. Applying Math.floor()
*
* If we get a result of 100.999… after multiplication and addition, Math.floor(100.999…) gives 100.
* If we get a result of 1.0 (or just over 1 up to 1.999…), Math.floor() gives 1.
* So the possible final num values after Math.floor() are 1, 2, 3, …, 100.
*
* 6. Conclusion
* The code generates a random integer between minimum (1) and maximum (100), inclusive of both.
* So num represents:
* A random whole number from 1 to 100.
*/

8 changes: 6 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
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?

/**
* The // tells JavaScript to ignore everything after it on that line, so the computer will not try to execute these instructions as code, but it remains visible for humans to read and understand the program.
*/
13 changes: 13 additions & 0 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
/**
// trying to create an age variable and then reassign the value by 1

const age = 33;
age = age + 1;
*/

/**
* There's a problem with your code. It's trying to reassign a value to a const variable, which is not allowed in JavaScript as const variables cannot be changed after they're declared.
* It returns a 'TypeError: Assignment to constant variable'
* To fix this, it should use let instead:
*/

// trying to create an age variable and then reassign the value by 1 using the keywork 'let' instead of 'const'

let age = 33;
age = age + 1;
21 changes: 21 additions & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
/**
*
// 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";
*/

/**
* The error is that you're trying to use the variable cityOfBirth in the template literal before it has been declared and initialized.

In JavaScript, you cannot access a const variable before its declaration. This creates what it's callled in JavaScript a Temporal Dead Zone (TDZ) error.

Fix: Move the console.log statement after the variable declaration:
*/

// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?

// The error is:
// ReferenceError: Cannot access 'cityOfBirth' before initialization

const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);

31 changes: 31 additions & 0 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
/** Original code:
*
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.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

/** Prediction
The code won't work because cardNumber is defined as a number (4533787178994213), but the .slice() method is a string method. Numbers don't have a .slice() method.
When the code runs, JavaScript will throw a TypeError saying:
cardNumber.slice is not a function
*/

/**
* Running:
* When running the code we get.
* TypeError: cardNumber.slice is not a function
*
* This matches prediction above that the error occurs because we're trying to use a string method on a number.
*/

/**
* Why the error occurs:
.slice() is a method that belongs to the String and Array prototypes, not the Number prototype. When JavaScript tries to execute cardNumber.slice(-4), it looks for a slice property on the number object, and can't find it, and throws a TypeError message.
*/

/** Fixing the code
To get the last 4 digits correctly, we need to convert the number to a string first then performs the operation.
*/

const cardNumber = 4533787178994213;
const last4Digits = cardNumber.toString().slice(-4);

console.log(last4Digits); // "4213"
12 changes: 11 additions & 1 deletion Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
/**
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const 24hourClockTime = "08:53";
*/

/**
* In JavaScript, variable names cannot start with a number. They must begin with a letter, underscore (_), or dollar sign ($).
*/

// Here the correct version
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";
48 changes: 47 additions & 1 deletion Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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;
Expand All @@ -13,10 +13,56 @@ console.log(`The percentage change is ${percentageChange}`);

// a) How many function calls are there in this file? Write down all the lines where a function call is made

/**
* There are 5 function calls in this file:

* carPrice.replaceAll(",", "") - Line 4
* Number() (enclosing the replaceAll result) - Line 4
* priceAfterOneYear.replaceAll("," "") - Line 5 (contains a syntax error but is intended as a function call)
* Number() (wrapping the replaceAll result) - Line 5 (intended as a function call)
* console.log() - Line 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?

/**
* Error location: Line 5

* Why the error occurs:
* There's a syntax error on line 5 - a comma is missing between the parameters in the replaceAll() method. The * code shows replaceAll("," "") but should be replaceAll(",", "").
*
* How to fix:
* Add a comma between the parameters:
*/

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

/**
* Lines with variable reassignment:
* Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
* Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); (after fixing the syntax error)
* These lines reassign new values to the existing variables.
*/

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

/**
* Lines with variable declarations:

* Line 1: let carPrice = "10,000";
* Line 2: let priceAfterOneYear = "8,543";
* Line 7: const priceDifference = carPrice - priceAfterOneYear;
* Line 8: const percentageChange = (priceDifference / carPrice) * 100;
*/

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?

/**
* This expression performs two operations:
*
* carPrice.replaceAll(",", "").
* It removes all commas from the string "10,000", converting it to "10000" (a string without commas)
* Number() - Converts the resulting string "10000" into an actual number type (10000)
* Ojective: The overall purpose is to convert a formatted price string (like "10,000") into a numeric value that can be used for mathematical calculations. This allows the code to properly calculate the price difference and percentage change, which wouldn't work correctly with the original string values.
*/

50 changes: 50 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,63 @@ console.log(result);

// a) How many variable declarations are there in this program?

/**
* There are five variable declarations. Here are:
* movieLength
* remainingSeconds
* totalMinutes
* remainingMinutes
* totalHours
*/

// b) How many function calls are there?

/**
* There is one 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

/**
* Based on the provided documentation from MDN, I can explain what the expression `movieLength % 60` represents. According to the "Arithmetic operators" section of the MDN document linked, the `%` operator is the "Remainder operator".
It returns the remainder left over when one operand (the left one, `movieLength`) is divided by a second operand (the right one, `60`).
In the context of the code:
`movieLength` stores a duration in seconds (8784).
Dividing `8784` by `60` tells how many whole minutes are in that duration, however it doesn't divide evenly all the time.
`movieLength % 60` specifically calculates the leftover seconds that do not form a full minute.
Based on the example of 8784 seconds:
8784 ÷ 60 = 146 minutes with a remainder.
60 * 146 = 8760 seconds.
8784 - 8760 = 24 seconds.
Therefore, this expression correctly isolates the seconds component of the total time duration, which is then stored in the `remainingSeconds` variable.
*/

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

/**
* Line 4 is const totalMinutes = (movieLength - remainingSeconds) / 60;.
*
* This expression first subtracts the remainingSeconds from the total movieLength.
* This results in a number of seconds that is perfectly divisible by 60.
* Then, it divides that number by 60 to convert it into the total number of whole minutes represented by the movie's length.
*/

// e) What do you think the variable result represents? Can you think of a better name for this variable?

/**
* The result variable represents the movie's length formatted as a string in the traditional hours:minutes:seconds format (e.g., "2:26:24" for a movie length of 8784 seconds).
* A better, more descriptive name for this variable would be formattedDuration or timeString. This makes its
* purpose clearer to anyone reading the code.
*/

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer

/**
* This code will work for all non-negative integer values of movieLength, but the output format might not always be the most appropriate. Here's the reason::

* For very short movies (e.g., 45 seconds): It will output "0:0:45". The hours and minutes are shown as single digits, which is technically correct but not the typical "00:00:45" format often expected.
* For durations with zero seconds (e.g., 3600 seconds = 1 hour): It will output "1:0:0".
* Potential problem: If movieLength were a negative number or not a number (e.g., a string), the code would either produce a negative time or result in an error like NaN (Not-a-Number), which wouldn't make sense.
* The code assumes the input is a valid positive number of seconds.
*/
Loading
Loading