-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
base: main
Are you sure you want to change the base?
Changes from all commits
e813147
7691eb4
049c5a2
aa5a115
3d01dd4
5886ee8
839005d
37219f6
fa6a1f1
cc72238
5ea4fc8
39f256d
28cf27f
189acd6
c0b40c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
// 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 | ||
|
||
|
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 |
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); |
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}`); |
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These look good, thanks for the explanations. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
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}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?