-
-
Notifications
You must be signed in to change notification settings - Fork 328
London | ITP-JAN-2026 | Said Fayaz Sadat | Sprint 1 | coursework/sprint 1 #997
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
f81891c
1ccf331
90c1eca
a40c512
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| /* const age = 33; | ||
| age = age + 1; | ||
| */ | ||
| // This code will throw an error because we are trying to reassign a value to a constant variable (age). In JavaScript, once a variable is declared with const, its value cannot be changed. To fix this error, we can either change the declaration to let or var, which allows for reassignment, or we can create a new variable to store the updated age. For example: | ||
|
|
||
| let age = 33; | ||
| age = age + 1; | ||
| console.log(age); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| // 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}`); | ||
| /*console.log(`I was born in ${cityOfBirth}`); | ||
| const cityOfBirth = "Bolton"; | ||
| */ | ||
|
|
||
|
|
||
| // The error in this code is that we are trying to use the variable cityOfBirth before it has been declared and assigned a value. In JavaScript, variables declared with const (or let) are not hoisted, which means they cannot be accessed before their declaration. To fix this error, we need to declare and assign a value to cityOfBirth before using it in the console.log statement. For example: | ||
|
|
||
| const cityOfBirth = "Bolton"; | ||
| console.log(`I was born in ${cityOfBirth}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,17 @@ | ||
| const cardNumber = 4533787178994213; | ||
| /*const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.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 | ||
|
|
||
| // The code above won't work because cardNumber is a number, and the slice method is a string method. The slice method cannot be used on a number, which will result in a TypeError. To fix this error, we can convert cardNumber to a string before using the slice method. For example: | ||
|
|
||
|
|
||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.toString().slice(-4); | ||
| console.log(last4Digits); | ||
|
Comment on lines
+15
to
+17
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. Good way to go about the solution without redeclaring the variable |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,9 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| /*const twelveHourClockTime = "20:53"; | ||
| const twentyFourHourClockTime = "08:53"; | ||
| */ | ||
|
|
||
| // | ||
| // The code above won't work because the variable names suggest that twelveHourClockTime should represent a time in 12-hour format, while twentyFourHourClockTime should represent a time in 24-hour format. However, the values assigned to these variables are not consistent with their names. To fix this error, we can either change the variable names to match the values or update the values to match the variable names. For example: | ||
|
|
||
| const twelveHourClockTime = "08:53 PM"; | ||
|
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. Good observation to both variable name and time format |
||
| const twentyFourHourClockTime = "20:53"; | ||
| 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; | ||
|
|
@@ -12,11 +12,24 @@ 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 | ||
| // There are 4 function calls in this file. The lines where a function call is made are: | ||
| // Line 1: carPrice.replaceAll(",", "") | ||
| // Line 2: priceAfterOneYear.replaceAll(",", "") | ||
| // Line 3: Number(carPrice.replaceAll(",", "")) | ||
| // Line 4: Number(priceAfterOneYear.replaceAll(",", "")) | ||
|
Comment on lines
14
to
+19
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. Can you have a look again and see if you would notice a different number of function calls within the code block? |
||
|
|
||
| // 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? | ||
| // The error is occurring on 4 where we are trying to convert the string values of carPrice and priceAfterOneYear to numbers using the Number function. The error is occurring because the replaceAll method is being called on a string that has not been updated with the new value after the first replaceAll call. To fix this problem, we can update the carPrice and priceAfterOneYear variables with the new values after the replaceAll calls. For example: | ||
|
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 a look at the code in the task again and re-evaluate the answer to the question. |
||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
| // Line 4: carPrice = Number(carPrice.replaceAll(",", "")); | ||
| // Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| // Line 1: let carPrice = "10,000"; | ||
| // Line 2: let priceAfterOneYear = "8,543"; | ||
| // Line 7: const priceDifference = carPrice - priceAfterOneYear; | ||
| // Line 8: const percentageChange = (priceDifference / carPrice) * 100; | ||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| // The expression Number(carPrice.replaceAll(",","")) is first calling the replaceAll method on the carPrice string to remove all commas from the string. This is necessary because the presence of commas in a number string can cause issues when trying to convert it to a number. After the commas are removed, the resulting string is passed to the Number function, which converts the string into a numeric value. The purpose of this expression is to convert the carPrice string, which may contain commas, into a number that can be used for calculations. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,19 @@ 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 6 variable declarations in this program. | ||
|
|
||
| // b) How many function calls are there? | ||
| // There is 1 function call in this program, which is console.log(result). | ||
|
|
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // The expression movieLength % 60 calculates the remainder when movieLength is divided by 60. In this context, it is used to determine the number of seconds that are left after accounting for the full minutes in the movie length. Since there are 60 seconds in a minute, using the modulus operator (%) with 60 gives us the remaining seconds that do not make up a full minute. For example, if movieLength is 8784 seconds, then 8784 % 60 would give us the number of seconds remaining after dividing 8784 by 60, which is 24 seconds. | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| // The expression assigned to totalMinutes is calculating the total number of minutes in the movie length. It does this by first subtracting the remaining seconds (calculated in the previous line) from the total movie length in seconds, which gives us the total number of seconds that are part of complete minutes. Then, it divides that result by 60 to convert the total seconds into minutes. For example, if movieLength is 8784 seconds and remainingSeconds is 24 seconds, then (8784 - 24) / 60 would give us the total number of minutes in the movie, which is 146 minutes. | ||
|
|
||
| // 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 that shows the length of the movie in hours, minutes, and seconds. A better name for this variable could be formattedMovieLength or movieDurationFormatted, as it more clearly indicates that it is a formatted representation of the movie length. | ||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| // This code will work for all non-negative integer values of movieLength, as it is designed to convert a length in seconds into a format of hours, minutes, and seconds. However, if movieLength is a negative value, the calculations may not make sense in the context of a movie length, and the output may not be meaningful. | ||
|
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. Can you also think of any scenario where some positive integer values may not produce a good output when applied? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,20 @@ 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? | ||
| function defination | ||
|
|
||
|
|
||
| Now enter just `console` in the Console, what output do you get back? | ||
| This prints the entire console object with all its methods, like log, warn, error, etc. | ||
|
|
||
| Try also entering `typeof console` | ||
| This prints the entire console object with all its methods, like log, warn, error, etc. | ||
|
Comment on lines
5
to
+15
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. Let your comment contain the actual output gotten from running the operation in the task. |
||
|
|
||
| Answer the following questions: | ||
|
|
||
| What does `console` store? | ||
| object | ||
| What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? | ||
| console.log("Hello") calls the log method to print something to the console. | ||
|
|
||
| console.assert(condition, "Message") calls the assert method to check a condition, and only prints the message if the condition is false. | ||
|
Comment on lines
+22
to
+24
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. You need to answer the questions according to the task requirement. Go through the question again and give a more direct answer. |
||
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.
Great description.