Repository navigation
London | 26-ITP-Sep | Mahir Shah | Sprint 2 | Sprint 2 Coursework #1550
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 15 commits
079ec48
f5838b7
23d7203
fc7d149
7ca09e8
eee3689
ea56b8e
a56a087
a20ac92
ced7487
6d1fdd2
1de4780
c9b584a
8e63d7e
715631a
dfceb22
710a9bd
3ea71eb
83b9be5
9cc571d
42695b2
c837210
744d864
3efcedd
d839a56
59faf6f
e2994d8
4166f54
28690ca
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,8 @@ 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(0, filePath.indexOf(base)); | ||
|
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 Also 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. It prints
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 now. The |
||
| const ext = filePath.slice(-(filePath.length - filePath.indexOf("."))); | ||
| console.log(ext); | ||
|
|
||
| // https://www.google.com/search?q=slice+mdn | ||
| // https://www.google.com/search?q=slice+mdn | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,12 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | |
| // 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 random, rounded down number between 100 and 1. | ||
| //math.floor rounds down. | ||
| //math.random gives a random number between 0 and 1. It can be equal to 0, but not equal to 1, | ||
| //(maximum - minimum + 1) gives in this case 100 | ||
| //So then Math.random() * (maximum - minimum + 1) gives a number between 0 and 99 | ||
| //Math.random() * (maximum - minimum + 1) gives a number between 1 and 100 | ||
|
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. This line says the same expression as line 15 gives 1 to 100. Both cannot be true. Which part of the full expression on line 4 shifts the range from 0..99 up to 1..100?
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's the one. |
||
| //and thus Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; gives a whole rounded | ||
| //down number | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| 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? | ||
| //Make it code |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,8 @@ | |
|
|
||
| const age = 33; | ||
| age = age + 1; | ||
|
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 Same in
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 run now. Good. |
||
|
|
||
| //TypeError: Assignment to constant variable. | ||
| //Age is constant so can't be reassigned | ||
| //To fix, change const to let, so line 3 becomes: | ||
| //let age = 33; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = cardNumber % 10000; | ||
| 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 | ||
|
|
||
| //TypeError: cardNumber.slice is not a function | ||
| //cardNumber is an integer, and slice is a string method, so slice doesn't work on cardNumber |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| const 12HourClockTime = "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. This file still has the original names, so nothing in it runs. Rename both variables so the file runs, and keep your explanation underneath.
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. Runs now. Good. |
||
| const 24hourClockTime = "20:53"; | ||
|
|
||
| //SyntaxError: Invalid or unexpected token | ||
| //In javascript, variable names cannot start with a number. | ||
| //To fix, either put number at the end or start with _ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,11 +12,17 @@ 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 | ||
| //5 function calls. 2 in line 7, 2 in line 8, 1 in line 10 | ||
|
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 count of 5 is right. Look again at the lines you listed. What is on line 7 and line 8, and is anything being called there? Then look at lines 4 and 5.
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 now. |
||
|
|
||
| // 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? | ||
| //Error is from line 5, no comma separating arguments for replace all. fix by adding comma after "," | ||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
| //4 and 5 | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| //1, 2, 7, 8 | ||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| //takes the string 10,000 and replaces all the commas with empty string, effectively removing the comma | ||
| //makes the string into number format so it can be converted to a number | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| const movieLength = 8784; // length of movie in seconds | ||
| const movieLength = 5; // length of movie in seconds | ||
|
|
||
| const remainingSeconds = movieLength % 60; | ||
| const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
|
|
@@ -12,14 +12,24 @@ 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? | ||
| //5 | ||
|
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. Count the lines above that 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. That's it. |
||
|
|
||
| // b) How many function calls are there? | ||
| //1 | ||
|
|
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
| //% is the modulo operator, which gives the remainder after operand. | ||
| //so in this case, the remainder of movieLength/60, | ||
| // the number of seconds not in a whole minute | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| //Take the total number of seconds in the move, minus the remainder of movieLength/60 to give | ||
| //a number that divides exactly by 60, then divide that number by 60. it converts the | ||
| // time into minutes | ||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
| //The number of whole minutes in the movie. numFullMinutes | ||
|
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.
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 name. |
||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| //Yes the math is fine and works with all numbers. | ||
|
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. 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 answer. |
||
| 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, | ||
| ); | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| paddedPenceNumberString.length - 2, | ||
| ); | ||
|
|
||
| const pence = paddedPenceNumberString | ||
|
|
@@ -25,3 +25,29 @@ 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 | ||
| //); | ||
| // finds a substring starting at 0 index to 1 before the last index, and assigns the | ||
| // substring to penceStringWithoutTrailingP. This removes the p and leaves us with just | ||
| // the number | ||
|
|
||
| // 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| // Makes it so that the string is always at least 3 characters, by adding leading 0 until | ||
| // it is 3 characters long. | ||
|
|
||
| // 4. const pounds = paddedPenceNumberString.substring( | ||
| // 0, | ||
| // paddedPenceNumberString.length - 2, | ||
| //); | ||
| // Finds the substring of number paddedPenceNumberString, with the last 2 digits cut off | ||
| // basically finds the number of pounds by cutting off the pence | ||
|
|
||
| // 5. const pence = paddedPenceNumberString | ||
| // .substring(paddedPenceNumberString.length - 2) | ||
| // .padEnd(2, "0"); | ||
| // Takes the substring starting at the total length-2, to the end. Essentially taking the | ||
| // last 2 digits | ||
| // Then adds 0 to the beginning so it is at least 2 characters long | ||
|
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. Two things here. And there is no step 6 yet for line 18; what does that last line do?
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 fixed. |
||
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
console.log(initials);under this line and run the file. What does it print? Have a look at what the backticks are doing to the three expressions inside them.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.
Fixed. It prints CKJ now.