-
-
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
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,12 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| // const age = 33; | ||
| // age = age + 1; | ||
|
|
||
| // 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,11 @@ | ||
| // 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"; | ||
|
|
||
| // 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 | ||
|
|
||
| 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); | ||
| 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 grammatically and then used the same method to extract the last 4 numbers |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,9 @@ | ||
| const 12HourClockTime = "8:53pm"; | ||
| const 24hourClockTime = "20:53"; | ||
| // 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); |
| 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,12 @@ 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, 8 | ||
|
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. Look at line 8 again. Is anything called there? And look at line 10. What is
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. 5 in total? |
||
| // 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 was a syntax error, it can be fixed by adding the missing part (1 of 2 parentheses) | ||
|
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. Node says "missing ) after argument list", but a bracket was not the problem. Compare line 5 with line 4 in the original. What was missing between the two arguments? And which line was it?
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. not sure |
||
| // c) Identify all the lines that are variable reassignment statements | ||
|
|
||
| // 4, 5 | ||
| // d) Identify all the lines that are variable declarations | ||
|
|
||
| // 1, 2 | ||
|
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. Lines 1 and 2 are right. A declaration is any line that creates a new variable. Look at lines 7 and 8. Do they create new variables?
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. they sure do. Silly mistake |
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| // It turns/converts a string into a number by removing the comma and quotation marks (since they are non-number values) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,18 @@ 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 | ||
| // b) How many function calls are there? | ||
|
|
||
| // 5 | ||
|
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. Which five? |
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
| // The remainder operator calculates the remainder of movie in seconds (it does it by dividing the number by 60) | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
|
|
||
| // total minutes (in seconds) - remaining seconds = remaining seconds. Then converts the seconds into minutes | ||
|
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 4 starts with |
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
|
|
||
| // It represents how much of the movie is left to watch. Maybe something like remainderOfTheMovie | ||
|
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. Run the file. It prints |
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| // It won't work with all values. Most importantly, the value must be strictly numeric and positive | ||
|
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. Try some values and write down what each one prints. Try |
||
|
|
||
| console.log(result); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,13 @@ const penceString = "399p"; | |
|
|
||
| const penceStringWithoutTrailingP = penceString.substring( | ||
| 0, | ||
| penceString.length - 1 | ||
| penceString.length - 1, | ||
| ); | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| paddedPenceNumberString.length - 2, | ||
| ); | ||
|
|
||
| const pence = paddedPenceNumberString | ||
|
|
@@ -24,4 +24,12 @@ console.log(`£${pounds}.${pence}`); | |
| // Try and describe the purpose / rationale behind each step | ||
|
|
||
| // To begin, we can start with | ||
| // 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
| // 1. const penceString = "399p": initializes a string variable with the value "399p" | ||
| // 3-6. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); | ||
| // : initializes a variable, the value of which is turned to numerical by removing the letter "p". | ||
| // 8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| // Here the padstart function turns "399" string into 399 number (it could add some zeroes in front, but here it serves as a converter) | ||
|
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. Does |
||
| // 9-12. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); | ||
| // Here the goal is to initialize a variable of Pounds (with the value of 3), by splitting it from 99, and it's done using the substring method | ||
| // 14-16. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); | ||
| // Here the pence variable is introduced with the value of 99 and it's done by taking the 399 and removing 3 using substring and padEnd methods. | ||
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 99