-
-
Notifications
You must be signed in to change notification settings - Fork 546
Cape Town | 27-ITP-September | Sima Nongawuza | Sprint 2 | Coursework-sprint 2 #1609
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
df5f817
48ae851
c31d05d
ec03354
e652c13
89f36e0
72b8724
a66fb77
b0f2d1e
8d227cd
b4c386b
127829d
fbabe03
a040e64
4c87f27
d39bec0
094f842
2623dbd
7282345
a5ef875
5b6ddbd
4be47f2
7d4af4e
87f5865
9d0e267
3195b56
4a49b92
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 |
|---|---|---|
|
|
@@ -2,8 +2,14 @@ const minimum = 1; | |
| 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 | ||
|
|
||
| // ANSWER | ||
| // num represents a random whole number in the range 0 - 100. | ||
| // math.random gives a random decimal between 0 and 1.math floor rounds the value to a whole number. | ||
| // (maximum - minimum + 1) evaluates the total count of integers. (+ minimum )adds 1 to the outcome. | ||
| // The code starts by evaluating math.random then multiply that by the maximum, math.floor will then round that value into a whole number then finally adding minimum to get num. | ||
|
Comment on lines
+11
to
+15
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. While your description is correct, it would be better if having a bit of explanation. For example, what does it mean by |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| // 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); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| // 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}`); | ||
| //Answer | ||
| // The console.log will be execued first because it is before the declared city variable. | ||
|
|
||
| const cityOfBirth = "Bolton"; | ||
| console.log(`I was born in ${cityOfBirth}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = String(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 | ||
|
|
||
| // Explanation | ||
| // The string slice method does not apply to number variables, therefore the code will not run. | ||
| // The number must be changed to a string first. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| const 12HourClockTime = "8:53pm"; | ||
| const 24hourClockTime = "20:53"; | ||
| const twelveHourClockTime = "8:53pm"; | ||
| const twentyFourhourClockTime = "20:53"; | ||
|
|
||
| // A variable name can not start with a number. |
| 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,18 @@ 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 | ||
| // line 4, number() and replaceAll() | ||
| // line 5, number() and replaceAll() | ||
| // 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? | ||
| // The was a missing comma in line 5. | ||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
| // line 4 and line 5 | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| // line 1,line 2, line 7, line 8 | ||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| // The expression changes the string to a number and removes the comma. This makes it possible for Javascript to perform subtraction since it is no longer a string | ||
|
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 bit of English presentation here, should be |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,15 @@ 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, line1,3,4,6,7,9 | ||
| // b) How many function calls are there? | ||
|
|
||
| // 1, Line 10: console.log() | ||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
|
|
||
| // The modulo operator divides the movieLength in seconds by 60 and give the remainder | ||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
|
|
||
| // It calculates the total number of whole minutes in the movie by subtracting the remainingSeconds from the total movie then divides the answer by 60 to covert seconds to minutes. | ||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
|
|
||
| // This variable represents the movie duration in the form of hours:minutes:seconds. better name id duration. | ||
|
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.
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| // The code will run for all non negative whole number. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,19 @@ console.log(`£${pounds}.${pence}`); | |
|
|
||
| // To begin, we can start with | ||
| // 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
| // 2. const penceStringWithoutTrailingP = penceString.substring( | ||
| // 0, | ||
| //penceString.length - 1 :Removes the letter p from the end of the string. | ||
| //); | ||
| // 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); : Makes sure that the string has at least 3 characters by adding zeros at the beginning if required. | ||
|
|
||
| // // const pounds = paddedPenceNumberString.substring( | ||
| // 0, | ||
| // paddedPenceNumberString.length - 2 :This takes the first digit of the number in pence since 100 pence = 1 pound therefore coverting pence to pounds | ||
| // ); | ||
|
Comment on lines
+34
to
+37
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. Again, this may just be English presentation. It "takes out (or remove) the last two digits" rather than "takes the first digit". There is a minor difference between the two angles to present the same thing. While the end result is the same, the explanation can be more accurate. |
||
|
|
||
| // const pence = paddedPenceNumberString | ||
| // .substring(paddedPenceNumberString.length - 2): Takes the pence which is the 2 numbers after the 1st number and ensures that it is exactly 2 digits long. | ||
| // .padEnd(2, "0"); | ||
|
|
||
| // console.log(`£${pounds}.${pence}`); : Prints the final output string using the literal to combine currency symbol, pounds and the pence. | ||
|
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 not to modify the question formatting. This file is in markdown format so that the '##' and '`' should be preserved. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,19 @@ | ||
| ## Objects | ||
|
|
||
| In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course. | ||
|
|
||
| Open the Chrome devtools Console, type in `console.log` and then hit enter | ||
| Open the Chrome devtools Console, type in console.log and then hit enter | ||
|
|
||
| What output do you get? | ||
| It gives the expression : ƒ log() { [native code] } | ||
|
|
||
| Now enter just `console` in the Console, what output do you get back? | ||
|
|
||
| Try also entering `typeof console` | ||
| Now enter just console in the Console, what output do you get back? | ||
| It gives the expression: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} with all the methods or functions that can be used for debugging. | ||
| Try also entering typeof console | ||
| It gives the string "object" | ||
|
|
||
| Answer the following questions: | ||
|
|
||
| What does `console` store? | ||
| What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? | ||
| What does console store? | ||
| It stores a list of methods that can used in the browser's developer tools. | ||
|
|
||
| What does the syntax console.log or console.assert mean? In particular, what does the . mean? | ||
| Console.log means that you are accessing the log method of the console object. The dot (.) is called the member access operator or dot notation, it is used to access methods inside an object. it tells javascript to look inside the object on the left to get the method on the right. |
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.
According to the definition in line 7, the
.should also be part of file extension. Please fix it. Thank you.