Skip to content

London | May 2025 | Samuel Tarawally | Sprint 1 | Coursework #660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
**/.DS_Store
**/runme.md
.devcontainer/
4 changes: 4 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,7 @@ 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 reassigns the 'count' variable.
// It evaluates the right side first (0 + 1 = 1), and then assigns that result back to the 'count' variable.
// The 'count' variable is often used to keep track of the number of iterations or occurrences in a loop or process.
Comment on lines +8 to +10
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

Choose a reason for hiding this comment

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

I believe it's an increment operation.

8 changes: 2 additions & 6 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ let firstName = "Creola";
let middleName = "Katherine";
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 = ``;

// https://www.google.com/search?q=get+first+character+of+string+mdn
let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;

console.log(initials);
11 changes: 5 additions & 6 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ 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
// Create a variable to store the ext part of the variable
const dir = filePath.slice(0, lastSlashIndex);
const ext = base.slice(base.lastIndexOf("."));

const dir = ;
const ext = ;

// https://www.google.com/search?q=slice+mdn
console.log(`The base is: ${base}`);
console.log(`The directory is: ${dir}`);
console.log(`The extension is: ${ext}`);
2 changes: 2 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ 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(`The random number is: ${num}`);
Comment on lines +10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you describe how the expression at line 4 works?

Can you also describe the possible values that could be assigned to num using the interval notation ?

  • [, ] => inclusion
  • (, ) => exclusion
    For example, we can say, "Math.random() returns a random number in the interval [0, 1)".

Copy link
Author

Choose a reason for hiding this comment

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

It works by calculating a random number with a value greater than or equal to 0 and less than 1 using Math.random(). This value is then scaled to the desired range and rounded down using Math.floor().

From this num may be assigned values within the interval [1, 100].

4 changes: 2 additions & 2 deletions Sprint-1/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?
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
// The `const` keyword cannot be reassigned to a different object or array.
// Variable references of this type are considered immutable.
// The `let` keyword allows variables to be declared and reassigned.
let age = 33;
age = age + 1;
console.log(age);
3 changes: 2 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// 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}`);
// Variables must be declared before they are used.
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
17 changes: 9 additions & 8 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
// The following code will throw an error when run
// since the variable cardNumber is an integer.
// The slice method is not available on integers.
// 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
// Converting the integer to a string first will fix the issue.
const cardNumber = 4533787178994213;
const last4Digits = String(cardNumber).slice(-4);
console.log(last4Digits);
10 changes: 8 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// Variable names in JavaScript cannot start with a number.
// This code throws a SyntaxError when run.
// const 12HourClockTime = "20:53";
// const 24hourClockTime = "08:53";

// The variable names should be valid identifiers, e.g.
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";
39 changes: 29 additions & 10 deletions 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 @@ -11,12 +11,31 @@ 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) 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:
// 1. `carPrice.replaceAll(",", "")` on line 4.
// 2. `Number(carPrice.replaceAll(",", ""))` on line 4.
// 3. `priceAfterOneYear.replaceAll(",", "")` on line 5.
// 4. `Number(priceAfterOneYear.replaceAll(",", ""))` on line 5.
// 5. `console.log(...)` on 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?
// The error is coming from the line: `priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));`
// The error occurs because there is a missing comma between the two arguments in the `replaceAll()` method: `(",", "")`. It should be `replaceAll(",", "")`.
// To fix this, add a comma between the empty string and the comma character within the `replaceAll` function call, e.g. `priceAfterOneYear.replaceAll(",", "")`.

// c) Identify all the lines that are variable reassignment statements.
// - carPrice = Number(carPrice.replaceAll(",", ""));
// - priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

// d) Identify all the lines that are variable declarations.
// - let carPrice = "10,000";
// - let priceAfterOneYear = "8,543";
// - const priceDifference = carPrice - priceAfterOneYear;
// - const percentageChange = (priceDifference / carPrice) * 100;

// e) Describe what the expression Number(carPrice.replaceAll(",", "")) is doing – what is the purpose of this expression?
// This expression is performing two main operations:
// 1. `carPrice.replaceAll(",", "")`: This part of the expression is called on the `carPrice` string ("10,000"). The `replaceAll()` method is used to remove all occurrences of the comma (",") character from the string. After this operation, "10,000" becomes "10000".
// 2. `Number(...)`: This part then takes the resulting string ("10000") and converts it into a numerical data type.
// The purpose of this entire expression is to convert the string representation of a price, which includes a comma for readability, into a pure numerical value that can be used for mathematical calculations (such as subtraction and division to find the difference and percentage change).
46 changes: 34 additions & 12 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const movieLength = 8784; // length of movie in seconds

const movieLength = -8784.847389543985709847385
const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;

Expand All @@ -11,15 +10,38 @@ 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?

// b) How many function calls are there?

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// a) How many variable declarations are there in this program?
// There are 6 variable declarations in this program, these are:
// 1. `movieLength`
// 2. `remainingSeconds`
// 3. `totalMinutes`
// 4. `remainingMinutes`
// 5. `totalHours`
// 6. `result`.

// b) How many function calls are there?
// There is 1 function call in this program: `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
// The expression `movieLength % 60` uses the remainder operator (`%`).
// Per Mozilla Developer documentation, the remainder (%) operator returns the division remainder, with its sign matching the dividend's.
// `movieLength % 60` determines the seconds remaining after converting movieLength to full minutes.

// d) Interpret line 4. What does the expression assigned to totalMinutes mean?
// Line 4: `const totalMinutes = (movieLength - remainingSeconds) / 60;`
// This expression calculates the total number of whole minutes within `movieLength`.
// It first subtracts `remainingSeconds` (which are the seconds that do not make up a full minute) from the `movieLength`.
// This leaves only the seconds that form complete minutes.
// This value is then divided by 60 to convert these seconds into a total count of minutes.

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

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// The variable `result` represents the `movieLength` converted and formatted into a string showing hours, minutes, and seconds, in the format `HH:MM:SS`.
// A better name for this variable could be `movieDuration`, as it more clearly indicates its purpose and possible data type.

// 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 positive integer values of `movieLength`.
// If `movieLength` is 0, the result will be "0:0:0", which is correct.
// If `movieLength` is a negative number, the remainder operator will return a negative remainder (e.g., -8784 % 60 would be "-2:-26:-24").
// If `movieLength` is a floating-point number, the calculations involving the remainder operator and division lead to floating-point values for `remainingSeconds`.
// Therefore, while it handles positive integers and non-integers well, it will not produce meaningful results for negative `movieLength` values and may therefore be
49 changes: 43 additions & 6 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,48 @@ const pence = paddedPenceNumberString

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
// This programme takes a string representing a price in pence
// It then constructs 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
// You need to provide a step-by-step breakdown of each line in this programme
// Describe the purpose and reasoning behind each step

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// To begin, we start with:
// 1. const penceString = "399p": Initialises a string variable with the value "399p".
// This represents the initial price in pence that the programme will process.

// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
// This line removes the trailing 'p' from `penceString` by:
// - `penceString.length`: Obtains the total length of `penceString`, which is 4.
// - `penceString.length - 1`: Calculates the index of the last character, which is 'p'.
// - `penceString.substring(0, penceString.length - 1)`: Extracts the substring from the start of `penceString` up to, but not including, the last character.
// - The result is the removal of the 'p' character, leaving "399".

// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// This line declares a constant variable named `paddedPenceNumberString`.
// - It applies the `padStart()` method to `penceStringWithoutTrailingP` ("399").
// - `padStart(3, "0")` checks if the string's length is less than 3.
// - If so, it adds "0" to the beginning of the string until its length reaches 3.
// - In this case, "399" already has a length of 3, so it remains unchanged.
// - The purpose of this step is to ensure the pence value always has at least three digits (e.g., "5p" becomes "005", "99p" becomes "099").
// - This is important for consistently extracting the pounds and pence parts later, especially for values less than 100p.

// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
// This line extracts the pounds part of the price.
// - It declares a constant variable named `pounds`.
// - It uses `substring(0, paddedPenceNumberString.length - 2)` on `paddedPenceNumberString` ("399").
// - This extracts the substring from the start of the string up to, but not including, the last two characters.
// - In this example, it extracts "3" from "399".

// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
// This line extracts and formats the pence part of the price.
// - It declares a constant variable named `pence`.
// - It uses `substring(paddedPenceNumberString.length - 2)` on `paddedPenceNumberString` ("399").
// - This extracts the last two characters of the string, which are "99".
// - The `padEnd(2, "0")` method ensures the extracted pence value has at least two digits.
Comment on lines +53 to +58
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we expect this program to work as intended for any valid penceString if we deleted .padEnd(2, "0") from the code?
In other words, do we really need .padEnd(2, "0") in this script?

Copy link
Author

Choose a reason for hiding this comment

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

No, .padEnd(2, "0") is not necessary, as paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); guarantees that any string of numbers will consistently have three values.


// 6. console.log(`£${pounds}.${pence}`);
// This final line constructs and prints the formatted price string to the console.
// - It uses a template literal to format the output as "£{pounds}.{pence}".
// - In this case, it outputs "£3.99", which is the price in pounds and pence format.
// - The `console.log` function displays the final result in the console.
Loading