Skip to content
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
53bf546
tested node js in VSCode
Baba05206 Oct 7, 2025
7f44d44
tested node js in VSCode
Baba05206 Oct 7, 2025
51decb7
tested node js in VSCode
Baba05206 Oct 7, 2025
cc4ee1c
added Line 3 desc + checked output of expression
Baba05206 Oct 7, 2025
a83775d
declared var initials + logged output to console
Baba05206 Oct 7, 2025
e262356
created dir & ext variables + logged output to console
Baba05206 Oct 7, 2025
5a27b07
logged output of num to console + ran prog severally to generate rand…
Baba05206 Oct 7, 2025
e5b59c3
included explanation of the expression
Baba05206 Oct 7, 2025
19900cf
updated to correct code error
Baba05206 Oct 10, 2025
81e5bf5
updated to correct code error
Baba05206 Oct 10, 2025
9584255
updated to correct code error
Baba05206 Oct 10, 2025
20639c2
updated to correct code error
Baba05206 Oct 10, 2025
71c6802
updated to correct code error
Baba05206 Oct 10, 2025
a36e160
completed scheduled tasks
Baba05206 Oct 10, 2025
ee63ed9
completed scheduled tasks
Baba05206 Oct 10, 2025
be6a3b0
completed scheduled tasks
Baba05206 Oct 10, 2025
a6c092d
submitted completed task
Baba05206 Oct 10, 2025
4b403bd
submitted completed task
Baba05206 Oct 10, 2025
ce151be
submitted completed task
Baba05206 Oct 10, 2025
e14a4d8
submitted completed task
Baba05206 Oct 10, 2025
f33255c
submitted completed task
Baba05206 Oct 10, 2025
c26d1a3
refactor: fix syntax errors and improve function definitions in key e…
Baba05206 Oct 15, 2025
496ec07
refactor: remove commented-out code and improve clarity in key error …
Baba05206 Oct 19, 2025
c61be3d
refactor: update movieLength value and enhance comments for clarity
Baba05206 Oct 19, 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
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 is (incrementing variable 'count' by 1) i.e updating the value of the count variable by adding 1 to its current value.
// The = (assignment) operator is used to assign the new value (count + 1) back to the count variable.
// Meaning, 'count' variable now has a new value of 1.
console.log(count); // outputs 1
4 changes: 2 additions & 2 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); // outputs "CKJ"

// https://www.google.com/search?q=get+first+character+of+string+mdn

8 changes: 5 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ 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); // Creates a variable to store the dir part of the filePath variable
const ext = base.slice(base.lastIndexOf(".")); // Create a variable to store the ext part of the variable
console.log(`The dir part of ${filePath} is ${dir}`); // outputs "/Users/mitch/cyf/Module-JS1/week-1/interpret"
console.log(`The ext part of ${filePath} is ${ext}`); // outputs ".txt"

// https://www.google.com/search?q=slice+mdn
// https://www.google.com/search?q=slice+mdn
19 changes: 19 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,22 @@ 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); // outputs a random integer between 1 and 100 (inclusive)

/*
1. Math.random()
Returns a floating-point number between 0 (inclusive) and 1 (exclusive).
2. (maximum - minimum + 1)
Calculates the size of the range (the number of possible values).
3. Math.random() * (maximum - minimum + 1)
Scales the random number to the desired range.
In this case: Math.random() * (100 - 1 + 1) → Math.random() * 100
So now the result is between 0 and 99.999...
4. + minimum
Shifts the range up by the minimum value.
So 0–99 becomes 1–100
5. Math.floor(...)
Rounds the result down to the nearest whole number.
So now we get an integer between 0 and 99 inclusive
*/
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: 4 additions & 2 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
age = age + 1;
let age = 33; //changing const to let allows reassignment.
age = age + 1; //age is now 34

console.log(age); //This line was added just to test the expression. Returns 34.
10 changes: 9 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?

// ReferenceError: Cannot access 'cityOfBirth' before initialization
/*
console.log(`I was born in ${cityOfBirth}`);
let cityOfBirth = "Bolton";
*/
// The error is because cityOfBirth is declared with const and is being accessed before its declaration.
// Changing const to let to help solve the problem.
// I am also declaring the variable 'cityOfBirth' before before using it.
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
// Output: I was born in Bolton
13 changes: 10 additions & 3 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.toString().slice(-4); // Updated this line to convert number to string first
console.log(last4Digits); // THis is added to see the output of the expression. Should print 4213

// 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.
// Before running the code, make and explain a prediction about why the code won't work - I am not very familiar with the slice method on numbers.
/* My prediction is that the slice method is not a function that can be used on numbers, it is a function for strings and arrays.
So I think the error will be something like "slice is not a function" or "cannot read property slice of undefined".
I think this is because numbers do not have properties or methods like strings and arrays do.
To fix this, I will convert the number to a string first, then use the slice method to get the last 4 digits.
I will then convert it back to a number if needed.
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
5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const time12HourClock = "08:53"; //1-variable names cannot start with a number // Changed variable name to start with a letter
//2-Swap the values around to match the variable name
const time24hourClockTime = "20:53"; //1-variable names cannot start with a number // Changed variable name to start with a letter
32 changes: 27 additions & 5 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 @@ -12,11 +12,33 @@ 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 this file.
They are on the following lines:
Line 4: carPrice.replaceAll(",", "")
Line 5: priceAfterOneYear.replaceAll(",", "")
Line 4: Number(carPrice.replaceAll(",", ""))
Line 5: Number(priceAfterOneYear.replaceAll(",", ""))
Line 8: console.log(`The percentage change is ${percentageChange}`)
*/
// 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?

/*
Line 5 - SyntaxError: missing , after argument list (the comma separating the arguments is missing). I added the comma.
*/
// c) Identify all the lines that are variable reassignment statements

/*
Lines 4 and 5 are variable reassignment statements.
carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
*/
// d) Identify all the lines that are variable declarations

/*
Lines 1 and 2 are variable declarations.
let carPrice = "10,000";
let priceAfterOneYear = "8,543";
Lines 7 and 8 are also variable declarations.
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?
// The expression is converting the string value of carPrice, which contains a comma, into a number by first removing the comma using the replaceAll method and then converting the resulting string into a number using the Number function. This allows for mathematical operations to be performed on carPrice.
28 changes: 24 additions & 4 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,34 @@ 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 6 variable declarations in this program.
They are on the following lines:
Line 1: const movieLength = 8784; // length of movie in seconds
Line 3: const remainingSeconds = movieLength % 60;
Line 4: const totalMinutes = (movieLength - remainingSeconds) / 60;
Line 6: const remainingMinutes = totalMinutes % 60;
Line 7: const totalHours = (totalMinutes - remainingMinutes) / 60;
Line 9: const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
*/
// b) How many function calls are there?
/*
There is 1 function call in this program.
It is on the following line:
Line 10: console.log(result);
*/

// Consider: Why do we say this is a function call? What is being passed to the function as an argument?
/*
A function call is when a function is invoked or executed. In this case, the function being called is console.log, which is a built-in function in JavaScript that outputs a message to the console. The argument being passed to the function is the variable 'result', which contains the formatted string representing the movie length in hours, minutes, and seconds.
The value being passed to the console.log() function as an argument is the final calculated and formatted string stored in the result variable.
*/

// 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 the value of movieLength (which is in seconds) is divided by 60. This operation is known as the modulus operator (%). In this context, it is used to determine the number of seconds that are left over after converting the total movie length into full minutes. Since there are 60 seconds in a minute, this expression effectively gives us the remaining seconds that do not make up a full minute.
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// The expression assigned to totalMinutes is (movieLength - remainingSeconds) / 60. This expression first subtracts the remaining seconds (calculated in line 3) from the total movie length in seconds (movieLength). This gives the total number of seconds that can be fully converted into minutes. Then, this result is divided by 60, since there are 60 seconds in a minute. The final result is the total number of full minutes in the movie length, excluding any leftover seconds.
// 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 string that shows the movie length in hours, minutes, and seconds. A better name for this variable could be formattedMovieLength or movieDurationFormatted, as these names more clearly indicate that the variable contains a formatted representation of the movie's duration.
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer

Choose a reason for hiding this comment

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

Do you have an answer for this last question?

7 changes: 6 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ console.log(`£${pounds}.${pence}`);
// Try and describe the purpose / rationale behind each step

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 1. const penceString = "399p": initializes a string variable with the value "399p"
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): removes the trailing 'p' from the string
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): ensures the string has at least 3 characters by padding with leading zeros if necessary
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds part of the string (all but the last two characters)
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the pence part (the last two characters) and pads with trailing zeros if necessary
// 6. console.log(`£${pounds}.${pence}`): formats and prints the final price in pounds and pence format
12 changes: 10 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@

// call the function capitalise with a string input
// 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
//Undeclared variables are automatically declared when first used. As a parameter in the 'capitalise' function,
// 'str' is already declared. trying to declare it again with 'let' will cause a syntax error.

// =============> write your new code here
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("hello world")); // This line is inserted only to test the code. It returns "Hello world"
16 changes: 15 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// =============> write your prediction here

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

/*
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -13,8 +13,22 @@ function convertToPercentage(decimalNumber) {
}

console.log(decimalNumber);
*/

// =============> write your explanation here
/*
1. Undeclared variables are automatically declared when first used. As a parameter in the 'convertToPercentage' function,
'decimalNumber' is (automatically) already declared. trying to declare it again with 'const' will cause a syntax error.
2. Also, the 'console.log(decimalNumber);' line is outside the function, so 'decimalNumber' is not defined in that scope.
3 decimalNumber = 0.5; overwrites the argument supplied, and so to make the function work as intended, this line should be removed, so the function does not ignore its input argument.
*/

// 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));
19 changes: 12 additions & 7 deletions Sprint-2/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

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

*/
// =============> write the error message here

// 1-SyntaxError: Unexpected number.
// =============> explain this error message here

/*
1-The parameter name '3' is not a valid identifier. Function parameters names must (be valid variable names & not literal values), i.e. start with a letter, underscore (_), or dollar sign ($), and cannot be a number.
2-Also, the function uses 'num' which is not defined anywhere. It should use the parameter name instead.
3-Finally, the function is not called anywhere, so it won't produce any output.
*/
// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
console.log(square(3)); // This line is inserted only to test the function. It returns 9
12 changes: 10 additions & 2 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// Predict and explain first...

// =============> write your prediction here

// The function is not returning the output of its calculation. Therefore, when we try to log the result of the function call, it will return 'undefined'.
/*
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
// 1- The function 'multiply' does not return any value. It only logs the result to the console.
// 2- When 'multiply(10, 32)' is called inside the template literal, it returns 'undefined', so the final output will be "The result of multiplying 10 and 32 is 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)}`);
13 changes: 11 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here

// The function is not returning the output of its calculation. Therefore, when we try to log the result of the function call, it will return 'undefined'.
/*
function sum(a, b) {
return;
a + b;
}

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

*/
// =============> write your explanation here
// 1- The function 'sum' does not return any value. It only has a return statement without any value, which means it returns 'undefined'.
// 2- When 'sum(10, 32)' is called inside the template literal, it returns 'undefined', so the final output will be "The sum of 10 and 32 is undefined".

// 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)}`);
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const http = require("node:http");

const hostname = "127.0.0.1";
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello, World!\n");
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Loading
Loading