-
-
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 9 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 |
|---|---|---|
|
|
@@ -7,3 +7,7 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | |
| // 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 | ||
|
|
||
| console.log(num); | ||
| // This expression uses a function that returns a random number between (min)1 and (max)100. | ||
|
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 range is right. The exercise also asks you to break the expression down. What does
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.
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. Clear now, thanks. |
||
| // So every time I ran the program, it generated a different result (between 1 and 100) | ||
| 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 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,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?
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. Yes, five. Your answer is right now. Your debugging line 6, |
||
| // 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
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 problem is on line 5 of the original code: Node says a
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. Oh, it was a comma between two arguments in line 5. |
||
| // 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
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. Yes, lines 7 and 8 create new variables. So they are declarations. A declaration starts with A reassignment has no keyword. It is only a name, |
||
| // 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?
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. Oh I see. So only the console.log(result) then
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. Yes, only one. Your new answer is right. |
||
| // 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
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.
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 new answer on lines 23 and 24 is right. |
||
| // 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
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. oh, i see, so the result could be replaced into something like totalMovieLength
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. Yes. It is the whole movie length, shown as hours:minutes:seconds. Your new answer on line 27 is right. |
||
| // 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
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. 0:0:59 , 0:-1:-30 , 0:1:30.5. Definitely not. I obviously haven't checked the edge cases
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. Those three results are right. Please write them in answer f) in the file. Add one short reason for each. For example, Then delete "and not messy ()" on line 29. It is not clear. |
||
|
|
||
| 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
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
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 new line 32 is right. Line 29 says the value becomes a number. But There is still no step for line 18. Line 18 uses a template literal. What does it join together, and what does it print?
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. it joins pounds and pence and prints a price |
||
| // 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.
Log
dirand compare it with the diagram. Yourdirends with a/. Is that last/part of dir? Or is it the separator between dir and base?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.
The last trailing slash is part of dir, because from what I've found, directory path variables should end with a trailing slash to clearly indicate that they represent directories. Edit. I see, for Unix it's different. Will fix it.
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.
Fixed, thanks.