-
-
Notifications
You must be signed in to change notification settings - Fork 328
London | 26-ITP-Jan | Johnny Vargas | Sprint 1 | Coursework #968
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 14 commits
b134e3d
e505570
4afab4b
0e92b5a
f602cdd
223b14a
7902ea5
1a08bb2
bf43f7a
029360e
30949fb
72be197
cafa65e
cb64829
8300538
8ab1e36
50f1a25
368cc05
00fb750
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,2 +1,4 @@ | ||
| 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? | ||
|
|
||
| // We can comment it out by using "//" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,8 @@ | |
|
|
||
| const age = 33; | ||
| age = age + 1; | ||
|
|
||
|
|
||
|
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. Spot on, let's take it a step further and fix the code itself too |
||
| // The error "Assignment to constant variable" occurs because const is used to declare a constant value that cannot be changed. | ||
| // Since the code tries to reassign the value on line 4 (age = age + 1), the program crashes. | ||
| // To fix this, we would use let, which allows for variable reassignment. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,7 @@ | |
|
|
||
| console.log(`I was born in ${cityOfBirth}`); | ||
| const cityOfBirth = "Bolton"; | ||
|
|
||
|
|
||
| // The 'ReferenceError' occurs because the code tries to use the variable cityOfBirth before it has been defined. | ||
|
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. Same as the other file, let's also fix the code itself |
||
| // JavaScript reads from top to bottom, so the variable must be declared BEFORE it is called in the console.log. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = cardNumber.toString().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 | ||
|
|
||
|
|
||
| // I predict an error will happen because cardNumber is a number and not a string and slice can not be used for pure numbers. | ||
| // We can fix it by turning the number into a string by using .toString() before we slice it. | ||
|
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. The solution is mostly correct, but there is an issue with typing. By doing toString(), we have now converted |
||
|
|
||
| console.log("The last four digits are: " + last4Digits); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| const hourClockTime12 = "20:53"; | ||
| const hourClockTime24 = "08:53"; | ||
|
|
||
| // In JavaScript, variable names aren't allowed to start with a number. | ||
| // The computer gets confused because it thinks 12 is a value, not a name. | ||
| // To fix it, we have to start the name with a letter, like clockTime12Hour or just twelveHourClockTime. | ||
|
|
||
| console.log(hourClockTime12 + " | " + hourClockTime24); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,11 +12,19 @@ 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 | ||
| // 4, 5, 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? | ||
| // It is coming from line 5. Because ("," "") is missing a comma separating the strings. | ||
|
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. Awesome, let's implement the comment and fix the code itself too |
||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
| // 4, 5. | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| // 1, 2, 7 , 8. | ||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| // JavaScript can't do math with commas. | ||
| // replaceAll(",", "") part finds the , in "10,000" and turns it into nothing "", turning the String into "10000". | ||
| // Even without the comma, it's still a String (text). The number part converts the text "10000" into the actual Number 10000. | ||
| // The purpose is so it turns a string of numbers into actual numbers to do math with. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,23 @@ 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. (6 Const in this case) | ||
|
|
||
| // b) How many function calls are there? | ||
| // 1, the last console.log(result) | ||
|
|
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
| // movieLength % 60 represents the seconds/minutes left over after you divide the total seconds/minutes by 60 | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| // This expression calculates the total number of whole minutes in the movie. | ||
| // It subtracts the "leftover" seconds so you have a number perfectly divisible by 60. | ||
| // It divides by 60 to convert seconds into 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 time string in HH:MM:SS (Hours:Minutes:Seconds) format. | ||
| // A better name for it could be movieLengthFormatted. | ||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| // If the seconds are a single digit like "5", the result will look something like 2:26:5. It makes it look unprofessional compared to say 02:26:05. | ||
|
||
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.
Awesome one, this is mostly correct, although less common, there is a case that can break this, what would happen if we have a file/directory name with a dot? Can we update the implementation slightly so we can handle that?