Skip to content

Sheffield | May-2025 | Waleed-Yahya Salih-Taha | Sprint1 #638

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 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
3809d8d
declared a variable called initials that stores the frist characters …
Bluejay600 Jun 30, 2025
7fa9849
the declared variable produce the string "CKJ".
Bluejay600 Jun 30, 2025
a5d88d4
1. got filename and the directory plus the file extention.
Bluejay600 Jun 30, 2025
2dcd035
1. worked out what num represents, break down the expression.
Bluejay600 Jun 30, 2025
90ed194
1. turn the two lines into comments
Bluejay600 Jun 30, 2025
f623104
1. interpreted the age variable declaration as const age
Bluejay600 Jul 2, 2025
c71036a
1.the error is the use of the variable 'cityOfBirth' before it's decl…
Bluejay600 Jul 2, 2025
31f0489
1. The error occurs because the `slice` method is being called on a n…
Bluejay600 Jul 2, 2025
1156c03
1. The error occurs because the variable name '12HourClockTime' start…
Bluejay600 Jul 2, 2025
40d86f1
1. Wrote down the 3 lines where a function call is made.
Bluejay600 Jul 2, 2025
b5fd505
saved the changes
Bluejay600 Jul 2, 2025
0c90651
1. answered all the questions about code.
Bluejay600 Jul 2, 2025
b2e98eb
interpreted the code and tested
Bluejay600 Jul 3, 2025
50cfabe
1. used `console.log` to check the value of variable
Bluejay600 Jul 3, 2025
6629ecd
fixed the reason for the error occurs on line 7.
Bluejay600 Jul 6, 2025
6378e92
identified the number of the function calls in total.
Bluejay600 Jul 6, 2025
6cf4e18
reverted the changes done to a file in Sprint-2.
Bluejay600 Jul 6, 2025
88084e2
fixed the line numbering in my answers
Bluejay600 Jul 6, 2025
40faf4a
tested the code after removing .padEnd(2, "0");. Also removed unnecc…
Bluejay600 Jul 6, 2025
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
5 changes: 5 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,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 incrementing the count variable by 1, effectively adding 1 to its current value
// The = operator is used to assign the new value (count + 1) back to the count variable
// The code is valid and will not throw an error
// because count is declared with let, allowing it to be reassigned.
console.log(count); // Output: 1
6 changes: 6 additions & 0 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ let lastName = "Johnson";
let initials = ``;

// https://www.google.com/search?q=get+first+character+of+string+mdn
initials += firstName[0];
initials += middleName[0];
initials += lastName[0];
console.log(initials); // Output: "CKJ"



23 changes: 18 additions & 5 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@

// (All spaces in the "" line should be ignored. They are purely for formatting.)

// 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
// Create a variable to store the ext part of the variable
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";

// Get base (file name)
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
// Get dir (directory path)
const dir = filePath.slice(0, lastSlashIndex);

const dir = ;
const ext = ;
// Get ext (file extension)
const lastDotIndex = base.lastIndexOf(".");
const ext = base.slice(lastDotIndex);

console.log(`The base part of ${filePath} is ${base}`);
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
9 changes: 9 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,12 @@ 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}`);
// The expression Math.random() generates a random floating-point number between 0 (inclusive) and 1 (exclusive).
// Multiplying this by (maximum - minimum + 1) scales the range to the desired range of numbers.
// Adding minimum shifts the range to start from the minimum value.
// Finally, Math.floor() rounds down the result to the nearest whole number, ensuring that num is an integer within the specified range.
// The final result is a random integer between minimum and maximum, inclusive.
// In this case, num will be a random integer between 1 and 100, inclusive.
// The program will output a different random number each time it is run, within the specified range

5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
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?
// You can use a comment to prevent the computer from running these lines
15 changes: 13 additions & 2 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
age = age + 1;
// const age = 33;
// age = age + 1;
// The error occurs because the variable 'age' is declared as a constant using 'const'.
// Constants cannot be reassigned a new value after they are declared.
// To fix this error, you can change the declaration of 'age' to 'let' or 'var' if you want to reassign it later.
// For example: let
age = 33;
age = age + 1;
// Now, 'age' can be reassigned without any error.
// Alternatively, if you want to keep 'age' as a constant, you should not attempt to reassign it.
// In that case, you can simply remove the reassignment line:
console.log(age); // Output: 34
// This will output the value of 'age' after the reassignment, which is now 34.
9 changes: 8 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// 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}`);
// console.log(`I was born in ${cityOfBirth}`);
// const cityOfBirth = "Bolton";

// The error occurs because the variable 'cityOfBirth' is used before it is declared.
// In JavaScript, variables declared with 'const' or 'let' are not hoisted to the top of their scope.
// To fix this error, you should declare the variable 'cityOfBirth' before using it in the console.log statement.
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
// This will correctly output "I was born in Bolton" without any errors.
16 changes: 14 additions & 2 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
// 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

// console.log(`The last 4 digits of the card number are: ${last4Digits}`);

// The error occurs because the `slice` method is being called on a number, which is not a valid operation.
// The `slice` method is a string method, and it cannot be directly applied to a number.
// To fix this error, we need to convert the `cardNumber` to a string before using the `slice` method.
// We can do this by wrapping `cardNumber` in the `String()` function or using the `toString()` method.
// Here's the corrected code:
const cardNumber = 4533787178994213;
const last4Digits = String(cardNumber).slice(-4);
console.log(`The last 4 digits of the card number are: ${last4Digits}`);
// Now, the code will correctly output the last 4 digits of the card number, which are "4213".
15 changes: 13 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// const 12HourClockTime = "20:53";
// const 24hourClockTime = "08:53";

// The error occurs because the variable name '12HourClockTime' starts with a digit, which is not allowed in JavaScript.
// Variable names must begin with a letter, underscore (_), or dollar sign ($).
// To fix this error, we can rename the variable to start with a letter or an underscore.
// For example, we can rename it to 'hour12ClockTime' or '_12HourClockTime'.
const hour12ClockTime = "20:53";
const hour24ClockTime = "08:53";

console.log(`The 12-hour clock time is: ${hour12ClockTime}`);
console.log(`The 24-hour clock time is: ${hour24ClockTime}`);
// This will correctly output the 12-hour and 24-hour clock times without any errors.
52 changes: 45 additions & 7 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,60 @@
let carPrice = "10,000";
let priceAfterOneYear = "8,543";
// let carPrice = "10,000";
// let priceAfterOneYear = "8,543";

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

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

console.log(`The percentage change is ${percentageChange}`);
// 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
//There are 5 function calls in total.
// 1. `carPrice.replaceAll(",", "")` on line 4.
// 2. numbeer(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 occurs on line 7: `priceAfterOneYear.replaceAll("," "")`
// The error is due to a syntax mistake in the `replaceAll` method call. There is a missing comma between the two arguments.
// To fix this, we need to add the missing comma, it should be `priceAfterOneYear.replaceAll(",", "")`.

// c) Identify all the lines that are variable reassignment statements
// The variable reassignment statements are:
// 1. `carPrice = Number(carPrice.replaceAll(",", ""));` on line 4
// 2. `priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));` on line 5

// d) Identify all the lines that are variable declarations
// The variable declarations are:
// 1. `let carPrice = "10,000";` on line 1
// 2. `let priceAfterOneYear = "8,543";` on line 2
// 3. `const priceDifference = carPrice - priceAfterOneYear;` on line 7
// 4. `const percentageChange = (priceDifference / carPrice) * 100;` on line 8
// 5. `console.log(...)` on line 10 (though this is a function call, it also serves as a declaration in this context)

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// The expression `Number(carPrice.replaceAll(",", ""))` is converting the string representation of the car price (which includes commas) into a number.
// It first removes all commas from the string using `replaceAll(",", "")`, and then converts the resulting string into a number using the `Number` function.
// This is necessary to perform arithmetic operations on the car price, as arithmetic operations cannot be performed directly on strings.

//correct the code
let carPrice = "10,000";
let priceAfterOneYear = "8,543";

// Remove commas and convert to numbers
carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

// Calculate difference and percentage change
const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;

// Output the result
console.log(`The percentage change is ${percentageChange.toFixed(2)}%`);

// The corrected line should be:
11 changes: 11 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@ 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?
// There are 5 variable declarations in this program:
// 1. `movieLength` on line 1
// 2. `remainingSeconds` on line 3
// 3. `totalMinutes` on line 4
// 4. `remainingMinutes` on line 6
// 5. `totalHours` on line 7

// b) How many function calls are there?
// There are no function calls in this program. All operations are performed using arithmetic operators and string interpolation.
Comment on lines +15 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

There are more than 5 variable declarations and more than 0 function calls.

Copy link
Author

Choose a reason for hiding this comment

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

we declare a variable using (let & const). how come we have more than 5 variable declaration?
we only have 4 if we are talking about the given code not the corrected one.

Copy link
Contributor

Choose a reason for hiding this comment

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

I counted 6 const in the code at lines 1-9 in Sprint-1/3-mandatory-interpret/2-time-format.js.


// 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` calculates the remainder when `movieLength` (in seconds) is divided by 60. This gives the number of seconds that do not fit into complete minutes, effectively providing the remaining seconds after converting to minutes.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// The expression `(movieLength - remainingSeconds) / 60` calculates the total number of minutes in the movie. It first subtracts the remaining seconds from the total movie length (to account for any leftover seconds), and then divides the result by 60 to convert seconds into minutes.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// The variable `result` represents the formatted time of the movie in the format "hours:minutes:seconds". A better name for this variable could be `formattedMovieTime` or `movieDuration` to make it clearer that it holds the duration of the movie in a human-readable format.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// Yes, this code will work for all non-negative integer values of `movieLength`. It will correctly convert any length of the movie given in seconds into the format "hours:minutes:seconds". However, if `movieLength` is negative, the output will not be meaningful, as negative time does not have a valid representation in this context. Therefore, it is advisable to ensure that `movieLength` is a non-negative integer before running the code.
19 changes: 9 additions & 10 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
const penceString = "399p";
const penceString = "399";

const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

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
// The purpose of this program is to convert a string representing a price in pence (e.g., "399p") into a formatted string representing the price in pounds (e.g., "£3.99").
// The program achieves this by manipulating the string to extract the numeric value and format it correctly.

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): removes the trailing 'p' from the string, resulting in "399".
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the string with leading zeros to ensure it has at least 3 characters, resulting in "399" (no change needed here since it already has 3 characters).
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds part of the string by taking all characters except the last two, resulting in "3".
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumber
Copy link
Contributor

Choose a reason for hiding this comment

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

The description at line 33 seems incomplete.

On separate note, 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.

.padEnd(2, "0") ensures that a string is at least 2 characters long by padding it with "0" on the right.
yes, we need .padEnd(2, "0") to make sure there are always two digits to work with.

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tried executing the script without padEnd(2, "0") using different values of penceString to validate your expectation?