-
-
Notifications
You must be signed in to change notification settings - Fork 197
London | ITP-May-2025 | Seddiq Azam | Module-Structuring-and-Testing-Data | Sprint-1 #517
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
c77a685
78adab7
d3dc329
6e519a6
bc85193
a7002a7
7ef1d3b
899fccb
6d9a7e1
1feed18
522c361
093b6ed
aa25724
61c5647
d7503e7
c69cb25
52a24eb
4189cd9
bb7ddf7
fbd0541
2fcc88e
26f8858
5c3b903
209fa44
41f73eb
ba814d5
9363e1e
90e75d7
374b28d
ba255ad
3ca5cc1
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,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); |
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}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
const hour24ClockTime = "20:53"; | ||
console.log(hour24ClockTime); | ||
|
||
const hour12ClockTime = "08:53"; | ||
console.log(hour12ClockTime); | ||
|
||
// The variables were renamed because JavaScript does not allow variables to start with numbers | ||
// camelCase format used for variable names |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,105 @@ 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 six variable declarations in the code: | ||
|
||
// Line 1 – const movieLength: | ||
// Stores the total duration of the movie in seconds. | ||
|
||
// Line 3 – const remainingSeconds: | ||
// Calculates the number of seconds remaining after converting to full minutes. | ||
|
||
// Line 4 – const totalMinutes: | ||
// Stores the total number of full minutes, excluding the leftover seconds. | ||
|
||
// Line 6 – const remainingMinutes: | ||
// Calculates the remaining minutes after converting to full hours. | ||
|
||
// Line 7 – const totalHours: | ||
// Stores the total number of full hours in the movie duration. | ||
|
||
// Line 9 – const result: | ||
// Constructs the final time in "HH:MM:SS" format as a string. | ||
|
||
//----------------------------------------------------------------------------------------------- | ||
// b) How many function calls are there? | ||
|
||
// There is one function call in the code: | ||
|
||
// console.log(result): | ||
// This function outputs the final formatted time (in "HH:MM:SS" format) to the console. | ||
|
||
//----------------------------------------------------------------------------------------------- | ||
// 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" uses the modulo operator, | ||
// which is an arithmetic operation that returns the remainder | ||
// after division. | ||
|
||
// In this context, it helps determine how many seconds are left | ||
// after converting the total movie length into full minutes. | ||
|
||
// For example: | ||
// movieLength = 8784 seconds | ||
// 8784 ÷ 60 = 146 full minutes with a remainder of 24 seconds | ||
// So, movieLength % 60 = 24 | ||
|
||
// These 24 seconds are what remain after dividing the movie length | ||
// into whole minutes. | ||
|
||
// If we subtract the 24 seconds from the total: | ||
// 8784 - 24 = 8760 seconds | ||
// Then: 8760 ÷ 60 = 146 full minutes | ||
|
||
//----------------------------------------------------------------------------------------------- | ||
// d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
|
||
// (movieLength - remainingSeconds): | ||
// This subtracts the "remainingSeconds" (24) from the total "movieLength" (8784 seconds). | ||
// The purpose is to remove the leftover seconds, leaving only the number of seconds | ||
// that make up complete minutes. | ||
|
||
// / 60: | ||
// This divides the result by 60 (since there are 60 seconds in a minute) | ||
// to convert the remaining seconds into full minutes. | ||
|
||
// totalMinutes: | ||
// This represents the total number of whole minutes in the movie, | ||
// which in this example is 146. | ||
|
||
//----------------------------------------------------------------------------------------------- | ||
// e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
|
||
// The variable `const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;` | ||
// represents the length of the movie formatted as hours:minutes:seconds (HH:MM:SS). | ||
|
||
// A more descriptive name for this variable could be "movieDurationTime", | ||
// as it clearly indicates that the value represents the movie's duration | ||
// in a time format. | ||
|
||
//----------------------------------------------------------------------------------------------- | ||
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
|
||
// This code will not display properly for movies with a duration of 0 seconds, | ||
// as the output would appear as "0:0:0", which is not well-formatted. | ||
|
||
// We can fix this by applying the .padStart() method to each time unit | ||
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. Consider updating the code to apply padStart for formatting hours, minutes, and seconds. This will ensure that values like '0' are displayed as '00', improving the readability of the time output. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
// (hours, minutes, and seconds) to ensure they are always displayed | ||
// with two digits — for example, "00:00:00". | ||
|
||
// We use .padStart() on string values because all the time units | ||
// are integers, and this method works on strings. | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,31 @@ 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); | ||
// This line removes the last character from the input string, which is the letter "p" (representing pence). | ||
// The reason we remove "p" is that we can't perform calculations on a string that includes non-numeric characters. | ||
|
||
|
||
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
// This line ensures the string is at least three characters long by padding it with leading zeros if necessary. | ||
// This formatting allows us to easily separate the value into pounds and pence — where the last two digits represent pence and the remaining digits represent pounds. | ||
|
||
|
||
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); | ||
// This line extracts the pound portion of the value by taking all characters except the last two. | ||
// For example, if the input is "399p", it becomes "399", and this line extracts "3" as the pound value. | ||
|
||
|
||
// 5. const pence = paddedPenceNumberString | ||
// .substring(paddedPenceNumberString.length - 2) | ||
// .padEnd(2, "0"); | ||
// This line extracts the last two digits, which represent the pence. | ||
// The `.padEnd(2, "0")` ensures that even if the pence value is only one digit (e.g., "5"), it becomes two digits (e.g., "05"). | ||
// So "5p" would correctly display as "£0.05" instead of "£0.5". | ||
Comment on lines
+44
to
+49
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. Could we expect this program to work as intended for any valid 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. .padEnd(2, "0") ensures that the pence value always has two digits, by padding with a zero on the right if necessary. 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. Have you tried verifying your expectations by executing the script without 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. I tested the code with different values. When I used "999p", the output was "£9.99" — both with and without .padEnd(2, "0"). After doing some research, I found that the line: .substring(paddedPenceNumberString.length - 2) returns the last two characters of the string. In this case, it correctly returns "99", which is already two characters long. Therefore, using .padEnd(2, "0") is unnecessary in this situation. |
||
|
||
|
||
// 6. console.log(`£${pounds}.${pence}`); | ||
// This line prints the final result in the correct British currency format. | ||
// If the input is "399p", the output will be "£3.99". | ||
// This step is important for presenting the value in a user-friendly and readable format. |
Uh oh!
There was an error while loading. Please reload this page.