-
-
Notifications
You must be signed in to change notification settings - Fork 337
London | 26-Jan-ITP | Angela McLeary | Sprint 1|Coursework/sprint 1 #990
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 17 commits
f428d25
db7592c
a66e3fe
ed76a69
2309db3
b7acb73
8af030a
001baba
8348eb6
818acf9
e05bbce
de2a36d
3b4e990
dc05100
7d7305a
a1c659f
a1992d3
e0248f4
f16fcab
5264a59
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 |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| 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?*/ | ||
|
|
||
| //*Answer | ||
| // We comment it out, | ||
| // We can do this by using a two forward slashes ("//")if it is on one line | ||
| //or we use one forward slash with an asterisk at the start("/*") and an asterisk | ||
| // and a forward slash at the end ("*/") for block commenting |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| //const age = 33; | ||
| //age = age + 1; | ||
|
|
||
| //Answer: | ||
| //we cannot re-assign variables with "const". | ||
| // We have use "let". let allows the variable to be re-assigned. | ||
| let age = 33; | ||
| age = age + 1; | ||
| console.log(age); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| // 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"; | ||
|
|
||
| //*Answer | ||
| // The console.log is before the variable and it cannot see it. | ||
| // The console log needs to be below the variable to compute the data. | ||
|
|
||
| const cityOfBirth = "Bolton"; | ||
| console.log(`I was born in ${cityOfBirth}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,24 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| //const cardNumber = 4533787178994213; | ||
| //const last4Digits = cardNumber.slice(-4); | ||
|
|
||
| // 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 | ||
| //*Answer | ||
| //It won't work because cardNumber is not a string and .slice() only works with strings. It will throw an error. | ||
|
|
||
| // Then run the code and see what error it gives. | ||
| //*Answer | ||
| //Uncaught TypeError: cardNumber.slice is not a function | ||
|
|
||
| // Consider: Why does it give this error? Is this what I predicted? If not, what's different? | ||
| //*Answer | ||
| //I was right. cardNumber is a number not a string and .slice() only works with strings | ||
| // so it needs to be converted to a string to work using .toString() | ||
|
|
||
| // Then try updating the expression last4Digits is assigned to, in order to get the correct value | ||
| //*Answer | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.toString().slice(-4); | ||
| console.log(last4Digits); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,11 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| //const 12HourClockTime = "20:53"; | ||
| //const 24hourClockTime = "08:53"; | ||
|
|
||
| //What's wrong with this code? | ||
| //*Answer | ||
| //The variable is not named in line with the Javascript naming convention. | ||
| // When declaring a variable it must not start with a number, space or reserved word. | ||
| // to fix this, we can rename them correctly. | ||
|
|
||
| const twelve_HourClockTime = "20:53"; | ||
|
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. here you have mixed variable naming using snake_case and PascalCase. Can you see the difference?
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. @LonMcGregor thanks for your feedback. |
||
| const twentyFour_HourClockTime = "08:53"; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,47 @@ | ||
| let carPrice = "10,000"; | ||
| let priceAfterOneYear = "8,543"; | ||
|
|
||
| carPrice = Number(carPrice.replaceAll(",", "")); | ||
| priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
| carPrice = Number(carPrice.replaceAll(",", "")); // returns 10000 | ||
| priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",",""));//returns 8543 | ||
| //priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
| // incorrect code missing syntax ie a comma | ||
|
|
||
| const priceDifference = carPrice - priceAfterOneYear; | ||
| const percentageChange = (priceDifference / carPrice) * 100; | ||
|
|
||
| console.log(`The percentage change is ${percentageChange}`); | ||
| console.log(`The percentage change is ${percentageChange}`); //returns The percentage change is 14.57 | ||
|
|
||
| // 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 | ||
| // a) How many function calls are there in this file? | ||
| // Write down all the lines where a function call is made | ||
| //*Answer | ||
| //There are 5 function calls: 2* Number(), 2*.replaceAll() and 1* console.log(). | ||
| //carPrice = Number(carPrice.replaceAll(",", "")); | ||
| //priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",","")); | ||
| //console.log(`The percentage change is ${percentageChange}`); | ||
|
|
||
| // 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? | ||
| // 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? | ||
| //*Answer it is missing a comma on line 5. | ||
| // Add the comma to fix it: ...replaceAll(",","")); | ||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
| //*Answer | ||
| //carPrice = Number(carPrice.replaceAll(",", "")); | ||
| //priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
|
|
||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| //let carPrice = "10,000"; | ||
| //let priceAfterOneYear = "8,543"; | ||
| //const priceDifference = carPrice - priceAfterOneYear; | ||
| //const percentageChange = (priceDifference / carPrice) * 100; | ||
|
|
||
|
|
||
| // e) Describe what the expression | ||
| // Number(carPrice.replaceAll(",","")) is doing - | ||
| // what is the purpose of this expression? | ||
| //It converts the carPrice into a number and removes the spaces and commas. | ||
|
|
||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,37 @@ 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? | ||
| // *Answer | ||
| // There are 6. | ||
|
|
||
| // b) How many function calls are there? | ||
| // *Answer | ||
| // There is 1; console.log() | ||
|
|
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| //*Answer | ||
| // This expression returns the remainder of seconds, after dividing the movieLength by 60, | ||
| // using the modulus operator it returns 24 seconds | ||
| // 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? | ||
|
|
||
| // 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 | ||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| // *Answer | ||
| // It is converting the seconds to minutes and rounding it to the last whole minute. | ||
|
|
||
| // e) What do you think the variable result represents? | ||
| // *Answer | ||
| // The result is the duration of the film in hours, minutes and seconds - 2:26:24. | ||
|
|
||
| // Can you think of a better name for this variable? | ||
| // *Answer | ||
| // movieDuration | ||
|
|
||
| // f) Try experimenting with different values of movieLength. | ||
| // Will this code work for all values of movieLength? Explain your answer, | ||
| // *Answer | ||
| // I changed the value to; | ||
| // 1200 returned 0:20:00 | ||
| // 92383.7 returned 25:39:43.69999999999709 | ||
| // -8784 returned -2:-26:-24 | ||
| // it worked with all numbers including decimals and negative numbers. | ||
|
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. Just note that when you tested these, though it ran without errors, does the output look OK, or are there values where it looks incorrect?
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. @LonMcGregor another good question. Line 47: Negative numbers passed the test. In reality there should not be negative time in the movieLength and it should throw an error. In both cases there was no validation to stop this from occurring. Thank you |
||
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.
Can you figure out the limits more precisely? Think about if it includes or excludes those numbers?
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.
@LonMcGregor Thanks for your question.
I can see it is not very clear. "num" returns a random whole number between 1 and 100 inclusive. It can never go below 1 or over 100.