Skip to content
Open
2 changes: 1 addition & 1 deletion Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ let count = 0;
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 an assignment statement. The = is the assignment operator, which takes the value on the right side (count + 1, which evaluates to 1) and assigns it to the variable on the left side (count), updating count's value from 0 to 1
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.

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?

@cjyuan increment

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.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`;
console.log(initials);

// 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);
const ext = base.slice(base.lastIndexOf("."));

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
16 changes: 15 additions & 1 deletion Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
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

// Breaking down what num represents:
// num generates a random integer between 1 and 100 (inclusive)

// Step-by-step evaluation:
// 1. Math.random() → generates a decimal between 0 (inclusive) and 1 (exclusive)
// 2. (maximum - minimum + 1) → calculates the range size: (100 - 1 + 1) = 100
// 3. Math.random() * 100 → gives a decimal between 0 and 99.999...
// 4. Math.floor(...) → rounds down to get integers from 0 to 99
// 5. + minimum → shifts the range from 0-99 to 1-100

// So num represents a random integer from 1 to 100
3 changes: 2 additions & 1 deletion 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?
We don't want the computer to run these 2 lines - how can we solve this problem?
By adding // at the beginning of each line to turn the text into comments.
3 changes: 2 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// 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);
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 ?
//The variable cityOfBirth is being used before it's declared. To fix the error, we need to declare the variable before using it in the console.log statement.

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
12 changes: 7 additions & 5 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.toString().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 a number, but .slice() only works on strings and arrays
// ERROR: TypeError: cardNumber.slice is not a function
// SOLUTION: Convert the number to a string first using .toString() method
console.log(last4Digits); // Should output: "4213"
//The issue is that cardNumber is a number, but the .slice() method only works on strings (and arrays).
// Convert the number to a string first, then use .slice()
7 changes: 5 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
//const 12HourClockTime = "20:53";
//const 24hourClockTime = "08:53";
// Variable names cannot start with a number
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";
18 changes: 18 additions & 0 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,29 @@ 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
// 5 function calls
//Line 4: carPrice.replaceAll(",", "")
//Line 4: Number(...) – wraps the above
//Line 5: priceAfterOneYear.replaceAll(",", "") ✅ But this line has a syntax error (we’ll get to it)
//Line 5: Number(...) – wraps the above
//Line 10: console.log(...)


// 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?
//syntax error! On line 5, there's a missing comma in the replaceAll method.
Copy link
Contributor

Choose a reason for hiding this comment

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

In the function call .replaceAll(",", ""), there's a programming term for "," and "" (the values passed into the function). Could you find out what this term is?

Copy link
Author

Choose a reason for hiding this comment

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

arguments

//correct code should be
//priceAfterOneYear = Number(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";

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// This expression converts a string with commas into a number that can be used for mathematical calculations:
// 1. carPrice.replaceAll(",", "") - removes all commas from the string "10,000" → "10000"
// 2. Number(...) - converts the cleaned string "10000" into the number 10000
// Purpose: Strings with commas cannot be used in math operations, so we need to clean and convert them to numbers first
19 changes: 19 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,33 @@ 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?
// 6 variable declarations
//Line 1: movieLength
//Line 3: remainingSeconds
//Line 4: totalMinutes
//Line 6: remainingMinutes
//Line 7: totalHours
//Line 9: result

// b) How many function calls are there?
// 1 function call
//Line 10: 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 % operator is the remainder operator. It returns the remainder left over when one operand is divided by a second operand.
// In this case, movieLength % 60 calculates the number of seconds remaining after dividing the total movie length by 60 (the number of seconds in a minute).
// For example, if movieLength is 8784 seconds, then 8784 % 60 equals 24, meaning there are 24 seconds left over after accounting for full 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 by first removing the remaining seconds and then dividing by 60 (the number of seconds in a minute).

// 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 output of the total hours, remaining minutes, and remaining seconds of the movie.
// A better name for this variable could be formattedMovieLength.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// The code will work for all non-negative integer values of movieLength. It correctly calculates hours, minutes, and seconds for any length of time.
// However, if movieLength is negative or not an integer, the results may not make sense in the context of a movie length.
// For example, if movieLength is negative, the calculations for hours, minutes, and seconds will yield negative values, which are not valid for a movie duration.
// Additionally, if movieLength is a non-integer (e.g., a float), the calculations will still work, but the interpretation of the result may be less clear since movie lengths are typically represented as whole seconds.
5 changes: 5 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ console.log(`£${pounds}.${pence}`);

// 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 to isolate the numeric part
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the numeric string with leading zeros to ensure it is at least 3 digits long
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds portion from the padded string
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the pence portion and ensures it is 2 digits long
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, we don't need .padEnd(2, "0") in this script. —Since padStart(3, "0") ensures the string is minimum 3 characters, and we're extracting the last 2, we'll always get exactly 2 characters. The .padEnd(2, "0") is redundant.

// 6. console.log(`£${pounds}.${pence}`): outputs the final formatted string representing the price in pounds
3 changes: 3 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?
->It creat a pop up with the word "Hello world!"

Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`.

What effect does calling the `prompt` function have?
What is the return value of `prompt`?
-> It displays a dialog box in the browser asking the user to enter some input.
The user can type a response and click "OK" or click "Cancel".
Comment on lines +20 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

What value does prompt() return? How can the program that call prompt() find out if the user clicked OK or Cancel?

Copy link
Author

Choose a reason for hiding this comment

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

The prompt() function returns different values depending on what the user does:
1. If the user types something and clicks "OK": prompt() returns a string containing whatever the user typed
◦ Even if they type nothing and click OK, it returns an empty string ""
2. If the user clicks "Cancel" (or presses ESC): prompt() returns null
The return value is either a string (including empty string "") or null. This null value is how the program knows the user clicked Cancel rather than OK.

7 changes: 7 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,19 @@ 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?
-> ƒ log() { [native code] }

Now enter just `console` in the Console, what output do you get back?
-> console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}

Try also entering `typeof console`
->'object'

Answer the following questions:

What does `console` store?
->console stores an object that contains functions for outputting information to the browser’s debugging console.

What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
->The . operator accesses a specific property or method on an object. console.log means "use the log function from the console object.
console.assert means access the assert function inside the console object.