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
7 changes: 7 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ let count = 0;

count = count + 1;

console.log(count); // should print 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


// On line 3, the code `count = count + 1;` updates the value of the variable `count`. JavaScript first takes the current value stored in `count`, adds `1` to it, and then assigns the new result back into the same variable.
// The `=` symbol here is the **assignment operator**. It does not mean “equals” in mathematics. Instead, it means “take the value on the right-hand side and store it in the variable on the left-hand side.”
// After this line runs, `count` now contains the value `1` instead of `0`.
4 changes: 3 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,9 @@ 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); // should print "CKJ"

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

7 changes: 5 additions & 2 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ 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); // dir part is "/Users/mitch/cyf/Module-JS1/week-1/interpret"
const ext = base.slice(base.lastIndexOf(".")); // ext part is ".txt"

console.log(`The dir part of ${filePath} is ${dir}`); // print: The dir part of /Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt is /Users/mitch/cyf/Module-JS1/week-1/interpret
console.log(`The ext part of ${filePath} is ${ext}`); // print: The ext part of /Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt is .txt

// https://www.google.com/search?q=slice+mdn
15 changes: 15 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,22 @@ const maximum = 100;

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

console.log(num); // Will output numbers like: 5, 42, 87, 100, etc.

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

// Math.random() generates a random decimal between 0 (inclusive) and 1 (exclusive)
// Examples: 0.1, 0.5, 0.99
// (maximum - minimum + 1) calculates the range size
// 100 - 1 + 1 = 100
// Math.random() * 100 creates numbers from 0 to 99.999...
// Examples: 0.1 × 100 = 10, 0.5 × 100 = 50, 0.99 × 100 = 99
// Math.floor() rounds down to the nearest integer
// Results in integers from 0 to 99
// + minimum shifts the range
// Adds 1 to move from 0-99 to 1-100
// The expression generates a random integer between the minimum value (1) and the maximum value (100), inclusive of both endpoints.
// Final Answer: num is a random integer from 1 to 100
6 changes: 4 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
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?

// We can use comments to "comment out" these lines
4 changes: 3 additions & 1 deletion 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;
let age = 33;
age = age + 1;

console.log(age);
2 changes: 1 addition & 1 deletion Sprint-1/2-mandatory-errors/2.js
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}`); // moved the console.log below const
9 changes: 8 additions & 1 deletion 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);

console.log(last4Digits); // 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.
// 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


// Before running the code, I predicted: The code will throw an error because cardNumber is defined as a number (not a string), and numbers don't have a .slice() method.
// The .slice() method is only available on strings and arrays, not on number primitives
// To fix the code, we need to convert the number to a string first, then use .slice()
6 changes: 4 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";

// updated the variable names to be more descriptive
39 changes: 38 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(",", "")); // Fixed syntax error by adding missing comma

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -20,3 +20,40 @@ console.log(`The percentage change is ${percentageChange}`);
// 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?


// Answers

// a) Function Calls (5 total):

// carPrice.replaceAll(",", "") (Line 3)
// Number() (Line 3)
// priceAfterOneYear.replaceAll(",", "") (Line 4)
// Number() (Line 4)
//console.log() (Line 8)

// b) Error Fix:

// Error line: Line 4 - missing comma in .replaceAll("," "")
// Fix: Change to .replaceAll(",", "")
// Error type: SyntaxError: missing ) after argument list

// c) Variable Reassignments:

// carPrice = Number(carPrice.replaceAll(",", "")) (Line 3)
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")) (Line 4)

// d) Variable Declarations:

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

// e) Expression Purpose:

// Number(carPrice.replaceAll(",", "")) converts a formatted currency string with commas into a numeric value for mathematical calculations:
// Removes commas: "10,000" → "10000"
// Converts to number: "10000" → 10000

// Output: The percentage change is 14.57 (14.57% price decrease)
28 changes: 28 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,31 @@ console.log(result);
// 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


// Answers:

// a) Variable Declarations: 5
// movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result

// b) Function Calls: 1
// console.log()

// c) movieLength % 60
//The modulo operator returns the remainder after division
// This calculates the remaining seconds that don't make a full minute

// d) Line 4: totalMinutes calculation
// Removes the leftover seconds, then divides by 60 to convert total seconds to minutes
// Result: Total minutes in the movie

// e) result variable
// Represents the movie length formatted as HH:MM:SS
// Better name: formattedDuration or movieDuration

// f) Testing different values
// ✅ Works for: 3600 (1:00:00), 3661 (1:01:01), 65 (0:01:05)
// ❌ Issue with: Single-digit minutes/seconds (e.g., 3610 shows 1:10 instead of 1:01:10)
// Problem: Missing leading zeros for values < 10

// Output: 2:26:24 (2 hours, 26 minutes, 24 seconds)
22 changes: 21 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,24 @@ 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"


// const penceString = "399p"
// Stores original price string with 'p' suffix

// Remove trailing 'p'
// Uses .substring() to remove the last character
// "399p" → "399"

// Pad to 3 digits
// Ensures string has at least 3 digits for consistent parsing
// "399" → "399" (no change here)

// Extract pounds
// Takes all digits except last 2 as pounds
// "399" → "3"

// Extract pence
// Takes last 2 digits as pence, ensures 2-digit format
// "399" → "99"
22 changes: 22 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,25 @@ Now try invoking the function `prompt` with a string input of `"What is your nam

What effect does calling the `prompt` function have?
What is the return value of `prompt`?



Effect of `alert("Hello world!"):`

Shows a popup dialog with "Hello world!" and an OK button

Pauses JavaScript execution until user clicks OK

Effect of `const myName = prompt("What is your name?"):`

Shows a popup dialog with the question, input field, and OK/Cancel buttons

Pauses execution and waits for user input

Return value of `prompt:`

If user types text and clicks OK: returns the input as a string

If user clicks Cancel: returns `null`

The value gets stored in the `myName` variable
10 changes: 10 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@ In this activity, we'll explore some additional concepts that you'll encounter i
Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?
I got ƒ log() { [native code] } - shows the actual log function code

Now enter just `console` in the Console, what output do you get back?
Console {} - displays the console object with all its methods

Try also entering `typeof console`
"object" - confirms console is an object

Answer the following questions:

What does `console` store?
A built-in browser object that contains logging and debugging methods

What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
The . is dot notation for accessing object properties

console.log means: access the log method that belongs to the console object

console is the parent object, log is one of its methods/functions
Loading