Repository navigation
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 14 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 |
|---|---|---|
|
|
@@ -17,7 +17,9 @@ console.log(`The base part of ${filePath} is ${base}`); | |
| // Create a variable to store the dir part of the filePath variable | ||
| // Create a variable to store the ext part of the variable | ||
|
|
||
| const dir = ; | ||
| const ext = ; | ||
| const dir = filePath.slice(1, lastSlashIndex); | ||
|
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. Log
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.
|
||
|
|
||
| const ext = base.slice(45); | ||
|
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. Log
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.
|
||
|
|
||
|
|
||
| // https://www.google.com/search?q=slice+mdn | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,25 @@ const maximum = 100; | |
|
|
||
| const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
|
|
||
| console.log(num); | ||
|
|
||
|
|
||
| // In this exercise, you will need to work out what num represents? | ||
| // 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 | ||
|
|
||
| Num represents a random whole number between the value of 'minimum' (1) and 'maximum' (100) | ||
| By creating a constant variable called 'num' and assigning it the value of the expression, we can generate a random number within the mentioned range. | ||
|
|
||
| When running the program we have to understand how the expression works: | ||
|
|
||
| we have to work out the parentheses first, maximum - minimum + 1, which is equal to 100 - 1 + 1 = 100. | ||
| so: Math.floor(Math.random() *100) + 1 | ||
| Math.random() produces a pseudo-random decimal number that is greater than or equal to 0 but less than 1. | ||
|
|
||
| Next, we need to multiply the result from Math.random() by 100. This may give us the result of a decimal between 0 and 100. | ||
| Next step is to use Math.floor() wich then rounds down the decimal number to the nearest whole integer. This then means that the result will be a whole number between 0 and 99. | ||
| But we want 1-100, so we add 1 to the result of Math.floor() this then changes the range to be inclusive of 1 and 100. | ||
| After adding 1, we need to round down the number to the nearest interger, this is done by using Math.floor(). After this we will have a whole number between maximum and minimum. | ||
|
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 4 again. How many times 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. Line 21 is right. But line 29 still says you round down after adding 1. That is the opposite order. Delete line 29.
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 29 is gone, and the order is right now. |
||
| Therefore, the final result of num will be a random whole number between 1 and 100, inclusive. | ||
| 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? | ||
| We don't | ||
|
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
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. Both lines are comments now, and the file runs. |
||
| // 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 that line 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,15 @@ | ||
| // 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. | ||
|
|
||
| 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,17 @@ | ||
| // Currently trying to print the string "I was born in Bolton" but it isn't working... | ||
| // what's the error ? | ||
|
|
||
| error | ||
|
|
||
| console.log(`I was born in ${cityOfBirth}`); | ||
|
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 6 and 7 are the original code, and they still run. So the file still stops with the same error. Your fixed version on lines 16 and 17 is right. What should happen to lines 6 and 7?
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 file runs now, and your error / right way layout is clear. Good. |
||
| 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,4 +1,4 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const cardNumber = "4533787178994213"; | ||
| const last4Digits = cardNumber.slice(-4); | ||
|
|
||
| // The last4Digits variable should store the last 4 digits of cardNumber | ||
|
|
@@ -7,3 +7,16 @@ const last4Digits = cardNumber.slice(-4); | |
| // 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 quotation marks.I | ||
|
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 prediction and the error message are good. But you changed
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 21 is exactly right. Just check it says You pass Please update your code to use
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 2 uses Some old notes still describe your first fix. Line 19 ends with "by adding quotation marks". Lines 23 and 24 say you put quotes around the number on line 1. You don't do that any more. Please change the end of line 19 to match your new fix, and delete lines 23 and 24. |
||
|
|
||
| const cardNumber = 4533787178994213 to const cardNumber = "4533787178994213"; | ||
| 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,12 @@ | ||
| 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 doesnt allow it, it can contan number inside the variable but not at the beginning. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,11 +12,13 @@ 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 | ||
|
|
||
| There are 5 function calls, two function calls on line 4 = Number(...) and replaceAll(...) two more on line 5 = Number(...) and replaceAll(...) and finally on line 10 = console.log(...) | ||
| // 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? | ||
|
|
||
| SyntaxError: missing a comma to separate the arguments | ||
|
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. Right reason. Which line is the error on? The comma is still missing on line 5, too.
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 comma is fixed and the file runs. The question also asks which line the error was on. Add the line number.
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 5 is right. Done. |
||
| // c) Identify all the lines that are variable reassignment statements | ||
|
|
||
| carPrice = Number(carPrice.replaceAll(",", "")); and priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
| // d) Identify all the lines that are variable declarations | ||
|
|
||
| There are 4 variable declarations- 1) let carPrice = "10,000"; 2) let priceAfterOneYear = "8,543"; | ||
| 3) const priceDifference = carPrice-priceAfterOneYear; 4) const percentageChange = (priceDifferenc/carPrice)* 100; | ||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| Number(carPrice.replaceAll(",", "")); is a string and the expression removes the comma from "10,000" and converts "10000" from a string into the number 10000. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,20 @@ 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 | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| 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. | ||
|
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. 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. Line 25 is right:
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. Fixed, thanks. |
||
| 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. | ||
|
|
||
| // 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. | ||
| result = 2:26:24 | ||
| // 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. | ||
|
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. Both of those work. Now try
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. 59, -90 and 90.5 all answered. |
||
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.
Your answer is right. But run
node 1-count.js. Node stops here with a SyntaxError. It tries to read your sentence as code.Start each answer line with
//. Then the line is a comment, and node skips it. Do this in every.jsfile.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.
All your answers are comments now, and every file runs. Good.