-
-
Notifications
You must be signed in to change notification settings - Fork 546
London | 26-ITP-Sep | Abakar Souleyman | Sprint 2 | Coursework #1530
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
c32281a
5eefab9
689213b
1716943
d8c52f7
52e2cb3
64003b5
7fdd13f
46da3bd
8cc6b17
f33a665
60283cb
fb61446
581eb7b
b5135ac
89a16d1
6067629
a57dcc9
cc7acf9
be74be1
f142f39
304536e
1295cd4
fa97c40
9e45bc7
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 |
|---|---|---|
|
|
@@ -4,6 +4,19 @@ const maximum = 100; | |
| const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
|
|
||
| // In this exercise, you will need to work out what num represents? | ||
| //num represents a randomly generated whole number between 1 and 100. | ||
|
|
||
| // Try breaking down the expression and using documentation to explain what it means | ||
| // Math.random() generates a random decimal number greater than or equal to 0 and less than 1. Multiplying it by 100 gives a number between 0 and 100. | ||
|
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 gives us maximum 100 ?
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. Gives a number between 0 and up to, but not including 100. |
||
| // Math.floor() always rounds down and returns the largest integer less than or equal to a given number in this case between 0 and 99. Finally, adding 1 changes the range to 1–100. | ||
| // Math.floor(Math.random() * (maximum - minimum + 1)) | ||
| // Math.floor(Math.random() * (100 - 1 + 1) | ||
|
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 closing bracket is missing in this line
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. Added the missing closing bracket. |
||
| // Math.floor(Math.random() * 100) | ||
| // Math.random give any random number between 0 to 1 example 0.225 * 100 = 22.5 | ||
| // Math.floor rounds that number to a whole number so 22.5 will become 22 | ||
| // (22) + minimum | ||
| // 22 + 1 | ||
| // 23 | ||
|
|
||
| // 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| This is just an instruction for the first activity - but it is just for human consumption | ||
|
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. You have added the two lines as comments below, which is the right idea. But lines 1 and 2 are still here as they were, so node still reads them as JavaScript and stops. What needs to happen to these two lines?
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. Deleted the two lines.
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. That is it, thanks. |
||
| We don't want the computer to run these 2 lines - how can we solve this problem? | ||
| 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? | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,6 @@ | |
|
|
||
| const age = 33; | ||
| age = age + 1; | ||
|
|
||
| let age = 33; | ||
|
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. This file does not run, check age declaration
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. Corrected. Thank you
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 is right and the file runs now. This section also asks you to interpret the error and explain why it happened, and there is nothing written down here yet. What did node print before you changed it, and why?
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. I didn’t see the section asking me to explain the error, so perhaps I’m looking in the wrong place.
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. You were not looking in the wrong place. This file does not ask for it anywhere, only the section heading in the Sprint-2 README does. No apology needed. Your explanation here is right, so that is done. |
||
| age = age + 1; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| // Currently trying to print the string "I was born in Bolton" but it isn't working... | ||
| // what's the error ? | ||
| //'cityOfBirth' cannot access before initialization | ||
|
|
||
| // console.log(`I was born in ${cityOfBirth}`); | ||
| // const cityOfBirth = "Bolton"; | ||
|
|
||
| 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,15 @@ | ||
| const cardNumber = 4533787178994213; | ||
|
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. This file does not run. Check cardNumber and last4Digits declaration
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. Corrected. Thank you! |
||
| const last4Digits = cardNumber.slice(-4); | ||
|
|
||
| const cardNumber = "4533787178994213"; | ||
| 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 | ||
| slice() not work with number | ||
| // Then run the code and see what error it gives. | ||
| cardNumber.slice is not a function | ||
| // 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| const 12HourClockTime = "8:53pm"; | ||
|
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. This file does not run. Check variable declaration
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. Corrected. Thank you. |
||
| const 24hourClockTime = "20:53"; | ||
|
|
||
| const ClockTime12Hour = "8:53pm"; | ||
|
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 renaming is right and the file runs. Same as
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. Node returned SyntaxError: Invalid or unexpected token. Both variable names started with a number, which JavaScript does not allow.
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. Same here, and your explanation is correct. Nothing else needed on this one. |
||
| const ClockTime24Hour = "20:53"; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,11 +12,21 @@ 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 function calls | ||
|
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 answers from here down are written as code rather than as comments, so node tries to run them and stops. Compare this file with On a) itself: you have found four calls. Look at line 10 as well. 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. I missed that. I’ve corrected it to five calls. Thanks for pointing it out.
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. Five is right, and console.log is the one people miss. |
||
| -Number(carPrice.replaceAll(",", "")) | ||
| -replaceAll(",", "") | ||
| -Number(priceAfterOneYear.replaceAll("," "")) | ||
| -replaceAll("," "") | ||
| // 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? | ||
|
|
||
| -Line 5 | ||
| -missing ) after argument list | ||
| -why: replaceAll() needs two arguments separated by a comma. | ||
| -fix: by adding comma replaceAll(",", "") | ||
| // c) Identify all the lines that are variable reassignment statements | ||
| line 4 and 5 | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
|
|
||
| line 1,2,7 and 8 | ||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| -First removing the comma "10,000" | ||
| -Second convert string into number 10000 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,20 @@ console.log(`£${pounds}.${pence}`); | |
|
|
||
| // To begin, we can start with | ||
| // 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
|
|
||
| 1. const penceString = "399p" // Stores "399p" in a variable called penceString so the program can work with the price. | ||
|
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. Same thing here. Your breakdown from this line down is written as code, so the file stops with an error. The content of the six steps is right, they just need to be comments.
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. Understood. I’ve corrected the six steps and converted them to comments.
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, the file runs now. |
||
| 2. const penceStringWithoutTrailingP = penceString.substring( | ||
| 0, | ||
| penceString.length - 1 | ||
| ); // Removes the "p" from the end of "399p", leaving "399" | ||
| 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| //Adds a "0" at the start if there are less than 3 digits. | ||
| 4. const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2); //gets the digits before the last two to find the pounds. | ||
|
|
||
| 5. const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); //gets the last two digits to find the pence. | ||
|
|
||
| 6. console.log(`£${pounds}.${pence}`); //Combines the pounds and pence and print the final price as £3.99 | ||
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.
Add a semicolon at the end of this line
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.
Thank you, added a semicolon at the end.