-
-
Notifications
You must be signed in to change notification settings - Fork 546
London | 26-ITP-Sep | Diana Ausiejute | Sprint 2 | JavaScript Fundamentals #1571
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
3aed680
6fa81f6
7456ec0
950078c
b181b7c
9977d32
e18f838
efc61f0
01624f5
902061e
5b83030
e6a26fd
4f42d5d
26d443e
2dd06dd
04e573d
f1cf999
e773dae
f267ff6
5daf6bf
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? | ||
|
|
||
| // the answer: by using "//" on each line |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| // the error is in line 4. It's a "TypeError: Assignment to constant variable", which was thrown because the value to | ||
| // a specific constant can only be assigned once (which is done in line 3 already). | ||
| // This wouldn't throw an error if instead of const, the let was used | ||
|
|
||
| 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 ? | ||
| // an error in line 4, "ReferenceError: Cannot access 'cityOfBirth' before initialization", which proves my assumption that | ||
| // the value of cityOfBirth should have been assigned prior to trying to print the string | ||
|
|
||
| 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,9 +1,17 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = cardNumber.toString().slice(-4); | ||
| // alternatively, const last4Digits = (cardNumber + '').slice(-4); | ||
| console.log(last4Digits); | ||
|
|
||
| // 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 thought it was something to do with the slice "-4" (but i turned out i forgot that negative indices start at -1, not 0) | ||
| // The actual error indicates that on the 3rd line there is a typeError: "cardNumber.slice is not a function" | ||
| // Checked the error reference and decided to look more closely. Noticed that the card number is used as a number, | ||
| // so it answers why the function couldn't be called - because they can be only called on Arrays and Strings. | ||
| // Therefore, I'll add parentheses to turn the card number into a string (so that the function could be called) | ||
|
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 explanation of the error is good. But look at line 1. You changed
Author
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. Interesting. And yes, I need to focus more on what the exercise asks me to do. Fixed it by converting the number into string programmatically and then used the same method to extract the last 4 numbers
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 fix on line 2 is right now. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| const 12HourClockTime = "8:53pm"; | ||
| const 24hourClockTime = "20:53"; | ||
| // SyntaxError: Invalid or unexpected token. | ||
| // it turns out that in JS variable names cannot begin with a number | ||
|
|
||
| const HourClockTime12 = "8:53pm"; | ||
| const HourClockTime24 = "20:53"; | ||
| console.log(HourClockTime24); |
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 range is right. The exercise also asks you to break the expression down. What does
Math.random()give? What doesMath.floordo to it? Which part makes the smallest value 1?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.
Math.random()gives a decimal between 0 < 1. I multiply it by 100 to stretch it out, thenMath.floor()chops off the decimals to make it a whole number. That gives me 0 to 99. The minimum at the end just adds 1 to the whole thing, so now it goes from 1 to 100 instead of 0 to 99There 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.
Clear now, thanks.