-
-
Notifications
You must be signed in to change notification settings - Fork 315
London | 26-ITP-January | Damian Dunkley | Sprint 1 | coursework/sprint-1 #949
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
36a00cc
9fe6f44
fb41b78
d9ecdf5
aa7d600
ad67e7b
e4e3d07
6b83e27
4004b30
cb505df
630fa14
54784c1
1304d1b
28c854e
6c190f1
8db0f90
7606c3b
1956015
6e86920
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,6 +1,7 @@ | ||
| let count = 0; | ||
|
|
||
| count = count + 1; | ||
|
|
||
| console.log(count); // Output: 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 that updates the value of the count variable. The expression on the right-hand side (count + 1) calculates the new value by taking the current value of count (which is 0) and adding 1 to it, resulting in 1. The = operator then assigns this new value back to the count variable, effectively updating it from 0 to 1. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| const minimum = 1; | ||
| const maximum = 100; | ||
|
|
||
|
|
||
| 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 | ||
|
|
||
| //Num generates a random number 0-0.9999999999999999, then multiplies it by (100-1+1=100), which gives a number between 0 and 99.99999999999999. Then it adds 1, which gives a number between 1 and 100. Finally, it uses Math.floor to round down to the nearest whole number, giving a final result of a random integer between 1 and 100 (inclusive). |
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,8 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| //const age = 33; | ||
| let age = 33; | ||
| age = age + 1; | ||
| console.log(age); | ||
|
|
||
| //problem: we cannot reassign a value to a variable that has been declared with "const". We can solve this problem by changing the declaration of the variable from "const" to "let", which allows us to reassign values to the variable. So I have commed line 3 and added line 4 to "let age = 33;". Also, I have added a line at the end to log the value of age to the console, so we can see the result of the reassignment to check that it is working correctly. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // 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}`); | ||
| //const cityOfBirth = "Bolton"; | ||
| //problem: 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" cannot be accessed before they are declared. To fix this error, we need to declare and assign a value to "cityOfBirth" before we try to use it. So we would move the line "const cityOfBirth = "Bolton";" above the console.log statement. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,16 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| //const last4Digits = cardNumber.slice(-4); | ||
|
|
||
|
|
||
| // convert number → string, then slice the last 4 chars | ||
| const last4 = cardNumber.toString().slice(-4); | ||
|
|
||
| console.log(last4); // "4213" | ||
|
|
||
| // 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 | ||
| // cardNumber is a variable that holds a number, and numbers do not have a slice method. The slice method is used for strings and arrays, but not for numbers. To fix this error, we need to convert the cardNumber to a string before using the slice method. We can do this by using the toString() method on cardNumber, like this: cardNumber.toString().slice(-4). This will convert the number to a string and then allow us to use the slice method to get the last 4 digits. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,12 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| //const 12HourClockTime = "20:53"; | ||
| //const 24hourClockTime = "08:53"; | ||
| // JavaScript variable names (identifiers) must not start with a digit. | ||
| // They may only start with:a letter | ||
| // (A–Z or a–z) | ||
|
|
||
| const T12HourClockTime = "20:53"; | ||
| const T24hourClockTime = "08:53"; | ||
| console.log (T12HourClockTime); | ||
| console.log (T24hourClockTime); | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,21 @@ 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. They are: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result. | ||
| // b) How many function calls are there? | ||
| // There are 2 function calls in this program. They are: the template literal function call when assigning a value to the result variable (line 9) and the console.log function call on line 10. | ||
|
Contributor
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. Line 9 isn’t a function call, it’s just a template literal creating a string using backticks and |
||
|
|
||
| // 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 calculates the remainder when movieLength is divided by 60. This gives us the number of seconds that are left over after converting the total seconds into minutes. Movilength is 146 minutes and 24 seconds, so the remaining seconds is 24. | ||
|
|
||
| // 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, excluding the remaining seconds, based on the movilength (8784s)so the calculation is; (8784-24)/60 = 146 minutes. | ||
|
|
||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
|
|
||
| // The variable result is the length of the movie in hours, minutes, and seconds. A better name for this variable could be movieDurationhms, as it more clearly indicates that it is the duration of the movie in hours, minutes, and seconds. | ||
|
Contributor
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. Your suggestion for variable name |
||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| // Movie length will work for all integers, based it it being a real movie and therefore non negative and the timing device only able to measure whole seconds. If movieLength is not an integer (e.g., a floating-point number), the calculations may yield unexpected results due to the way JavaScript handles division and modulus operations with non-integer values. | ||
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.
You could do this slightly simpler. Do you already have a variable that does
filePath.slice(lastSlashIndex + 1)?