-
-
Notifications
You must be signed in to change notification settings - Fork 546
London | 26-ITP-Sep | Sakiya Mayow | Sprint 2 | JavaScript-Fundamentals #1573
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
401df8c
7dc9303
050721b
67e2dff
8380925
1c653d1
1a7c56f
e325f3c
9f4f143
bba3df3
e556d1d
9781f22
082f415
c1a4b08
f18d99c
4f047d6
f738b2f
bc1dbce
d2bfb65
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,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? | ||
|
|
||
| //We can add 2 forward slashes , by typing two forward slashes ( // ) at the beginning of the line, we can turn both lines into a comment. This then means that the computer will completely ignore that line and not run it. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| // 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); | ||
|
|
||
| //const means that the variable age is a constant and cannot be reassigned. When we try to reassign the value of age by adding 1 to it, we get an error. | ||
| //This is because we cannot change the value of a constant variable once we have created it. | ||
| //// The error is a TypeError: Assignment to constant variable. | ||
|
|
||
| //If we want to change the value of age, we need to use let instead of consts when declaring the variable. this then allows us to reassign the value of age by adding 1 to it. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| // 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}`); | ||
| // The error is a ReferenceError: Cannot access 'cityOfBirth' before initialization | ||
|
|
||
| //console.log(`I was born in ${cityOfBirth}`); | ||
| //const cityOfBirth = "Bolton"; | ||
|
|
||
| //The reason there is an error is because we are using the wrong order, we are telling Javascript to print the value of cityOfBirth before creating a variable. | ||
| //So we need to create the variable first, this is because Javascript runs code from top to bottom, so a variable that is declared with const needs to be declared before using it. | ||
|
|
||
| //right way | ||
|
|
||
| const cityOfBirth = "Bolton"; | ||
| console.log(`I was born in ${cityOfBirth}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,24 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = String(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 | ||
|
|
||
| console.log(last4Digits); | ||
|
|
||
| //I think that Javascript will give an error because .slice() is normally used with strings, and cardNumber is currently a number. | ||
| //const cardNumber = 4533787178994213; does not have any quotation marks, so Javascript will treat it as a number. | ||
|
|
||
| //When run with console.log(last4Digits); we get --> TypeError : cardNumber.slice is not a function | ||
|
|
||
| //.slice is a method used with strings and we need to make cardNumber into a string by adding String before cardNumber. | ||
|
|
||
| //we can change the expression by making it into a string by doing --> const last4Digits = String(cardnumber).slice(-4); | ||
| //String(cardNumber) changes the number into a string and .slice(-4) takes the last 4 characters and stores it in last4Digits. | ||
| //const cardNumber = 4533787178994213 , const last4Digits = cardNumber.slice(-4); change that to --> const last4Digits = String(cardNumber).slice(-4); | ||
| //I added String before cardNumber to change the original cardNumber into a string, so we can use the slice() method. | ||
| //Now cardNumber is a string, then we can re run the code with console.log(last4Digits); to get the correct code. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,10 @@ | ||
| const 12HourClockTime = "8:53pm"; | ||
| const 24hourClockTime = "20:53"; | ||
| const Hour12ClockTime = "8:53pm"; | ||
|
|
||
| console.log(Hour12ClockTime); | ||
|
|
||
| const hour24ClockTime = "20:53"; | ||
|
|
||
| console.log(hour24ClockTime); | ||
|
|
||
| //Variable names cannot start with a number as Javascript doesn't allow it, it can contain a number inside the variable but not at the beginning. | ||
|
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. Good reasoning. A variable name cannot start with a number. You also need to name the error type. JavaScript has different error types. The prep covered three: TypeError, ReferenceError and SyntaxError. The error type will be obvious from the error itself. Which one is this? Please add it to your explanation of the error.
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. The error type is SyntaxError. It is the first word of the error message node showed when you ran the original file. Please add this line above your explanation on line 12: // The error is a SyntaxError: Invalid or unexpected token |
||
| // The error is a SyntaxError: Invalid or unexpected token | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| const movieLength = 8784; // length of movie in seconds | ||
| const movieLength = 90.5; // length of movie in seconds | ||
|
|
||
| const remainingSeconds = movieLength % 60; | ||
| const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
|
|
@@ -12,14 +12,31 @@ 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? | ||
| //>6 variable Declarations, every const creates a new variable. | ||
|
|
||
| // b) How many function calls are there? | ||
| //>1 function calls is console.log() as it is a function being called and told to do the job | ||
|
|
||
| // 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 means finding the remainder after dividing movieLength by 60. movieLength = 8784 so we need to do: 8784/60 = 146.4 and the nearest whole integer is 146. | ||
| //So there are 146 complete groups of 60. we then multiply; 146 x 60= 8760 --> 8784 - 8760 = 24 so 8784 % 60 = 24 and therefore remainingSeconds becomes 24. | ||
| //8784/60 = 146.4. and 8784 % 60 = 24 as this is the remainder. | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| //>const totalMinutes = (movieLength - remainingSeconds) / 60 ---> if we break it down -> movieLength = 8784 remainingSeconds = 24 | ||
| //we then subtract remainingSeconds from movieLength= 8784 -24 = 8670. we then have to divide it by 60 which gives us 8760 / 60 = 146. we divide because there is 60 seconds in one minute, and right now we are converting seconds into minutes. | ||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
| //>The result variable will show us he complete results from our calculations of = totalHours:remainingMinutes:remainingSeconds. | ||
|
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 answer to the first part of e) is right. The question also asks for a better name for the variable The name Can you suggest a better name? Pick one that explains what this value is.
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. A good name says what the value is. |
||
| //result = 2:26:24 | ||
| // A better name for result variable would be const movietime = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; | ||
| //console.log(result); | ||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| //>I tested differnet codes: 7965 and I got the result 2:12:45 and i also tried : 2654 and got 0:44:14. | ||
|
|
||
| //> suggestions to run code - 59 gives us 0:0:59 > less than 60 seconds | ||
| //-90 gives us 0:-1:-30 - not valid as the code works properly for non-negative whole numbers | ||
| //90.5 gives us 0:1:30.5 > this tells us 60 goes into 90.5 once and 30.5 is the remainder | ||
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.
Good reasoning. You explain that JavaScript runs from top to bottom, so the variable must be declared first.
You also need to name the error type. JavaScript has different error types. The prep covered three: TypeError, ReferenceError and SyntaxError. The error type will be obvious from the error itself. Which one is this? Please add it to your explanation of the error.
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.
The error type is ReferenceError. It is the first word of the error message node showed when you ran the original file.
Please add this line above your explanation on line 9:
// The error is a ReferenceError: Cannot access 'cityOfBirth' before initialization