Skip to content

London | ITP-May-2025 | Halyna Kozlovska | Module-Structuring-and-Testing-Data | Sprint-1 #458

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 15 commits into
base: main
Choose a base branch
from
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 a statement that increases count by 1. The = operator reassigns the result of count + 1 back to the count variable.
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.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`;
console.log(initials);

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

10 changes: 7 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,11 @@ 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);
console.log(`The dir part of ${filePath} is ${dir}`);

// https://www.google.com/search?q=slice+mdn
const lastDotIndex = base.lastIndexOf(".");
const ext = base.slice(lastDotIndex);
console.log(`The ext part of ${base} 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
@@ -1,9 +1,18 @@
const minimum = 1;
const maximum = 100;

// 1. Math.random() generates a decimal between 0 (inclusive) and 1 (exclusive)
// 2. Multiply by (maximum - minimum + 1) to scale to the desired range size
// 3. Math.floor() rounds down to get an integer from 0 up to range size minus 1
// 4. Adding minimum shifts the range so the lowest number is minimum, not zero
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;

Choose a reason for hiding this comment

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

Good explanation.
Extension question: what is the range of values that num can take?

Copy link
Author

@halyna-k halyna-k Jun 13, 2025

Choose a reason for hiding this comment

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

@Amundeep-Dhaliwal Thank you for your comment.
The value of num is an integer within the range from minimum to maximum, including both ends.

Choose a reason for hiding this comment

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

Apologies, should have clarified.

What are the numeric values for the upper & lower limits for num?

Copy link
Author

Choose a reason for hiding this comment

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

@Amundeep-Dhaliwal The lowest value num will be minimum, and the highest - maximum, so minimum ≤ num ≤ maximum. This line of code creates a random integer in that range by scaling a decimal from Math.random(), rounding down, and adding minimum.

Copy link
Author

Choose a reason for hiding this comment

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

@Amundeep-Dhaliwal Did I answer your question?


// Result: num is a random integer between minimum and maximum, inclusive
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


8 changes: 6 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
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?

// Explanation:
// These lines are instructions, not JavaScript code
// Added // to ignore them and prevent SyntaxError when running the file
5 changes: 4 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
// use 'let' instead of 'const' for variables that need to be reassigned
let age = 33;
age = age + 1;
// log the updated age value
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}`);
// declare cityOfBirth before using it to avoid ReferenceError
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
5 changes: 4 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
// convert number to string before using slice
const last4Digits = cardNumber.toString().slice(-4);
// log the last 4 digits
console.log(last4Digits);

// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
Expand Down
9 changes: 7 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// Rename variables to valid identifiers because names cannot start with a number
const ClockTime_12Hour = "08:53";

Choose a reason for hiding this comment

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

This is good practice for naming variables. It can be quite difficult sometimes!

In JS it is common to use camel case when naming variables e.g. Time12Hour. In python it is common to use snake case when naming variables time_12_hour. It is good practice not to mix the two.

Copy link
Author

Choose a reason for hiding this comment

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

@Amundeep-Dhaliwal Thank you. I agree and will keep this in mind for future naming.

const ClockTime_24Hour = "20:53";

// Output the values
console.log("Clock time in 12h format:", ClockTime_12Hour);
console.log("Clock time in 24h format:", ClockTime_24Hour);
27 changes: 26 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(",",""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -13,10 +13,35 @@ console.log(`The percentage change is ${percentageChange}`);

// a) How many function calls are there in this file? Write down all the lines where a function call is made

// Function calls in this file: 5
// 1. carPrice.replaceAll(",", "") [line 4]
// 2. Number(carPrice.replaceAll(",", "")) [line 4]
// 3. priceAfterOneYear.replaceAll(",", "") [line 5]
// 4. Number(priceAfterOneYear.replaceAll(",", "")) [line 5]
// 5. console.log(`The percentage change is ${percentageChange}`); [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?

// add missing comma between the two arguments inside replaceAll
// priceAfterOneYear.replaceAll(",","") [line 5]

// c) Identify all the lines that are variable reassignment statements

// Variable reassignment statements: 2
// 1. carPrice = Number(carPrice.replaceAll(",", "")); [line 4]
// 2. priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",","")); [line 5]


// d) Identify all the lines that are variable declarations

// Variable declarations: 4
// 1. let carPrice = "10,000"; [line 1]
// 2. let priceAfterOneYear = "8,543"; [line 2]
// 3. const priceDifference = carPrice - priceAfterOneYear; [line 7]
// 4. const percentageChange = (priceDifference / carPrice) * 100; [line 8]

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?

// Firstly, carPrice.replaceAll(",", "") removes all commas from the string
// Then Number(...) converts the resulting string into a number type so it can be used in arithmetic

Choose a reason for hiding this comment

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

These look good, thanks for the explanations.

31 changes: 29 additions & 2 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,47 @@ const totalMinutes = (movieLength - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
console.log(result);
// Rename result to formattedTime for clarity
const formattedTime = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
console.log(formattedTime);

// 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:
// movieLength, [line 1]
// remainingSeconds, [line 3]
// totalMinutes, [line 4]
// remainingMinutes, [line 6]
// totalHours, [line 7]
// result. [line 9]

// b) How many function calls are there?

// There is 1 function call:
// console.log() [line 9]

// 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.
// movieLength % 60 gives the remainder after dividing movieLength by 60,
// which represents the leftover seconds after counting full minutes.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// totalMinutes calculates how many whole minutes are in movieLength by
// subtracting leftover seconds and dividing the remainder by 60.

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

// Result is the movie length formatted as "hours:minutes:seconds".
// A better name could be formattedTime.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer

// 1. The code correctly converts movieLength into hours, minutes, and seconds for positive values.
// 2. It doesn’t handle zero or negative values properly—these cases need extra checks.
// 3. Minutes and seconds may appear without leading zeros.
// 4. To improve add validation to handle zero or negative inputs gracefully.

Choose a reason for hiding this comment

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

These answers are good, it is nice to see that you are thinking about the edge cases for the code.

6 changes: 6 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
// 1. Initialise a string representing a price in pence with a trailing "p"
const penceString = "399p";

// 2. Remove the trailing "p" from the string to isolate the numeric part
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

// 3. Pad the numeric string with leading zeros to ensure it has at least 3 digits
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// 4. Extract the pounds part by taking all but the last two digits
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

// 5. Extract the pence part by taking the last two digits and pad with trailing zero if needed
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

// 6. Output the formatted price in pounds and pence
console.log(`£${pounds}.${pence}`);

Choose a reason for hiding this comment

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

This activity looks good.


// This program takes a string representing a price in pence
Expand Down
4 changes: 4 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ Let's try an example.

In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;
- Display a popup message with the text "Hello world!".

What effect does calling the `alert` function have?
- Pauses the script until the user clicks OK.

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?
- Calling the `prompt` function displays a dialog box with a message and an input field where the user can type a response.
What is the return value of `prompt`?
- Value of `prompt` returns the string entered by the user, or null if the user cancels the input.
6 changes: 5 additions & 1 deletion Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course.

Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?
- The function definition of log. Example: ƒ log() { [native code] }

Now enter just `console` in the Console, what output do you get back?
- The whole console object with many functions inside. Example: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}

Try also entering `typeof console`
- "object" because console is an object.

Answer the following questions:

What does `console` store?
- It stores tools to show messages in the browser’s console.
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
- The `.` means “use this function from the console object.” For example, console.log means “use the log function from console.”
Loading