-
-
Notifications
You must be signed in to change notification settings - Fork 546
Manchester | 26-ITP-Sep | Aklilu Mihtsun | Sprint 2 | Complete Sprint 2 Coursework #1578
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
4c29df2
57d7614
4804923
4c1a754
bc9011c
8f0d2a6
d9ef102
1915855
0821e2b
c5ba16a
8f7878f
a469ac4
da54930
c1a241b
9ff581f
5bbf762
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 |
|---|---|---|
|
|
@@ -3,7 +3,17 @@ 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 is a variable declared to hold (being assigned) the value of the calculated result of the var minimum, var maximum and the Math.random() method | ||
| // This expression can be broken down in to main steps | ||
| // 1. the inner parentheses (maximum - minimum + 1) is calculated because that is the inner most expression | ||
| // 2. then the Math.random() generates random number between 0 & 1 and multiply with the result of (maximum - minimum + 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.
|
||
| // 3. then the Math.floor() rounds down the result of (Math.random() * (maximum - minimum + 1)) to the nearest whole number | ||
| // 4. finally the minimum is being added to the down rounded whole number and the variable num holds the value | ||
| // after running the program the value of num looks like this 51,8,85,8,99,21 random numbers between 1 & 100 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,9 @@ | ||
| 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? | ||
|
|
||
| //OR | ||
|
|
||
| /* 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 can put the 2 lines in comment by adding // before each single line or we can enclose them in /* */ if we want to comment multi-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; | ||
| let age = 33; | ||
| age = age + 1; | ||
| console.log(age); | ||
|
|
||
| /* When we try to run the code it is giving us an error message saying TypeError: Assignment to constant variable, | ||
| this error is telling us that we are trying to perform an operation on a value that is not the correct type, | ||
| in our case reassigning a cons variable. The cons keyword prevents us from reassigning a variable | ||
| We can overcome the error by changing the declaration keyword to "let" | ||
| */ |
| 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}`); | ||
| const cityOfBirth = "Bolton"; | ||
| console.log(`I was born in ${cityOfBirth}`); | ||
| //const cityOfBirth = "Bolton"; | ||
|
|
||
| /* When we try to run our code it is giving us an error saying ReferenceError: Cannot access 'cityOfBirth' before initialization | ||
| and this is because we tried to access the var cityOfBirth before we declared and initialized it*/ | ||
|
|
||
| // We can overcome this error by declaring and initializing the var cityOfBirth before accessing it |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,16 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| //const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = cardNumber.toString().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 am predicting the code wont't work because the slice method does not work for numbers I guess | ||
| // when i run the program it is giving an error saying "TypeError: cardNumber.slice is not a function" | ||
| // It is giving this error because our var cardNumber is number and numbers don't have slice() method & it is what I predicted | ||
| // In order our code to be able to run we need first to change the number type into String so that the slice() method can function |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,13 @@ | ||
| const 12HourClockTime = "8:53pm"; | ||
| const 24hourClockTime = "20:53"; | ||
| //const 12HourClockTime = "8:53pm"; | ||
| //const 24hourClockTime = "20:53"; | ||
|
|
||
| const HourClockTime12 = "8:53pm"; | ||
| const hourClockTime24 = "20:53"; | ||
|
|
||
| console.log(HourClockTime12); | ||
| console.log(hourClockTime24); | ||
|
|
||
| // when we tried to run the code it threw an error saying "SyntaxError: Invalid or unexpected token" | ||
| // this happened because when we were declaring a variable we gave an invalid name to the identifier which started with number | ||
| // variable name must start with letters or _ or $ | ||
| // we can fix it buy removing the front number of the identifiers and may be put them last |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,8 @@ let carPrice = "10,000"; | |
| let priceAfterOneYear = "8,543"; | ||
|
|
||
| carPrice = Number(carPrice.replaceAll(",", "")); | ||
| priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
| //priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
| priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
|
|
||
| const priceDifference = carPrice - priceAfterOneYear; | ||
| const percentageChange = (priceDifference / carPrice) * 100; | ||
|
|
@@ -20,3 +21,21 @@ console.log(`The percentage change is ${percentageChange}`); | |
| // d) Identify all the lines that are variable declarations | ||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
|
|
||
| // Answer | ||
|
|
||
| // a) In this file we can say we have 5 function calls (2 function calls & 3 method calls to be specific) and they are in line 4, 5 and 10 | ||
|
|
||
| // b) When we run the code the error is generating from line 5 and the reason is that when we pass more than 1 argument | ||
| // in a method we need to separate them with comma, but in line 5 there is no comma to separate the arguments | ||
| // we can fix it by adding a comma to separate the arguments | ||
|
|
||
| // c) Lines 4 and 5 are reassignment statements | ||
|
|
||
| // d) Lines 1, 2, 7 and 8 are variable declarations | ||
|
|
||
| // e) The expression Number(carPrice.replaceAll(",","")) | ||
| // 1. carPrice.replaceAll(",","") this method is removing the "," from the String variable carPrice by replacing the "," with empty String which is "" | ||
| // all left then is just the String digits without "," | ||
| // 2. Number("String digit without comma") and this function converts the String digit to number digit | ||
| // and finally Number(carPrice.replaceAll(",","")) expression gives us number | ||
|
Comment on lines
+40
to
+41
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 only describes what the expression is doing, but still not quite described its purpose. Please add the purpose. Thank you. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,19 @@ console.log(result); | |
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
|
|
||
| // Answer | ||
|
|
||
| // a) In the above code there are 6 variable declarations | ||
|
|
||
| // b) There is only 1 function call in the code | ||
|
|
||
| // c) the expression movieLength % 60 is trying to divide the movieLength by 60 then get the remainder | ||
|
|
||
| // d) totalMinutes = (movieLength - remainingSeconds) / 60 in this expression | ||
| // 1. (movieLength - remainingSeconds) the values inside the parenthesis is calculated first then it is divided by 60 | ||
| // then the value of expression is assigned to the var totalMinutes | ||
|
Comment on lines
+36
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. You are almost there. The question is asking "what does ... mean" but not "what does it do". So you also need to include a bit on the meaning here. Thank you. |
||
|
|
||
| // e) the variable result represents the formatted time in hours:minutes:seconds. I would rename it time | ||
|
|
||
| // f) it will not work with negative numbers because we can not have negative time | ||
| 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, | ||
| ); | ||
|
Comment on lines
3
to
6
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. Why a |
||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| paddedPenceNumberString.length - 2, | ||
| ); | ||
|
Comment on lines
9
to
12
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. Why a |
||
|
|
||
| const pence = paddedPenceNumberString | ||
|
|
@@ -25,3 +25,43 @@ console.log(`£${pounds}.${pence}`); | |
|
|
||
| // To begin, we can start with | ||
| // 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
|
|
||
| // 2. From line 3 to line 6 const penceStringWithoutTrailingP is declared and it will hold the value of | ||
| // penceString.substring(0, penceString.length - 1) | ||
| // which is "399". We got this value by the method substring(0, penceString.length - 1) called by penceString | ||
| // Breaking down the expression penceString.substring(0, penceString.length - 1) | ||
| // a) penceString.length - 1 this counts the number of characters in the penceString String and then subtracts 1 from it (4-1=3) | ||
| // then it will look like this penceString.substring(0, 3) | ||
| // b) penceString.substring(0, penceString.length - 1) then the substring method called by penceString | ||
| // is used produce a slice of small string from a big string | ||
| // it takes two arguments the first one tells where to start slicing and the second one tells where to stop | ||
| // (but the last character is not included) | ||
| // Therefore const penceStringWithoutTrailingP will be assigned "399" | ||
|
|
||
| // 3. In line 8 const paddedPenceNumberString is declared and it will hold the value of penceStringWithoutTrailingP.padStart(3, "0") | ||
| // which is still "399". In this expression we used the method padStart called by penceStringWithoutTrailingP and this method is basically | ||
| // adds characters at the start of our String. It takes two arguments the first one is telling how many characters we want in our String | ||
| // and second argument tells the character that needs to be added so that we get out desired number of characters in our String. | ||
| // since our String length is 3 the method will not do anything | ||
|
|
||
| // 4. From line 9 to line 12 const pounds is declared and it will hold the value of paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2,) | ||
| // which is "3". We got this value by the method substring(0, paddedPenceNumberString.length - 2,) called by paddedPenceNumberString | ||
| // Breaking down the expression paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2,) | ||
| // a) paddedPenceNumberString.length - 2 this counts the number of characters in the paddedPenceNumberString then subtracts 2 from it (3-2=1) | ||
| // then it will look like this paddedPenceNumberString.substring(0, 1) | ||
| // b) paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2,) then the substring method called by paddedPenceNumberString is used to | ||
| // produce a slice of small string from a big string | ||
| // it takes two arguments the first one tells where to start slicing and the second one tells where to stop | ||
| // (but the last character is not included) | ||
| // Therefore const pounds will assigned "3" | ||
|
|
||
| // 5. From line 14 to line 16 const pence is declared and it will hold the value of | ||
| // paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0") which is "99" | ||
| // Breaking down the expression | ||
| // a) paddedPenceNumberString.length - 2 this will count the number of characters in paddedPenceNumberString and then subtracts 2 from it (3-2=1) | ||
| // paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) then this substring method will produce small string from it starting from index 1 which will be "99" | ||
| //then finally paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0") the padEnd method will add characters at the end of the String | ||
| // and will take two arguments first will tell how many characters we need in our string and the second will tell the character that needs to be added | ||
| // since our String length is 2 the method will do nothing | ||
|
|
||
| // 6. Finally line 18 will print out the desired format we have added what to be printed inside the back-ticks if they are variables we will put them in ${} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,20 @@ | ||
| Open a new window in Chrome, right click an empty space on the page, select **Inspect** from the dropdown, then locate the **Console** tab. | ||
| Open a new window in Chrome, right click page, select **Inspect** from the dropdown, then locate the **Console** tab. | ||
|
|
||
| Voila! You now have access to the [Chrome V8 Engine](https://www.cloudflare.com/en-gb/learning/serverless/glossary/what-is-chrome-v8/). | ||
| Just like the Node REPL, you can input JavaScript code into the Console tab and the V8 engine will execute it. | ||
|
|
||
| Let's try an example. | ||
|
|
||
| In the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`; | ||
| Click an empty space on tn the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`; | ||
|
|
||
| What effect does calling the `alert` function have? | ||
|
|
||
| // The `alert` function displays a message to the user and it stops js execution until the user dismisses the alert | ||
|
|
||
| Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. | ||
|
|
||
| What effect does calling the `prompt` function have? | ||
| What is the return value of `prompt`? | ||
|
|
||
| // The `prompt` function opens a popup dialog and waits an input from a user. it stops js execution until the user inputs or cancel the popup dialog | ||
| // The return value of `prompt` is a string |
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.
Good try. A file extension does not necessary 4 character long, some are longer and some are shorter. Please fix it.