Repository navigation
Manchester | 26-ITP-Sep | Monsur Abdulrahman | Sprint 2 | Coursework/sprint 2 #1600
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
1b5ce16
6f2377a
78f922e
1ca86a4
6fd351c
b117813
fa638a8
4c61057
9de5303
2c371d5
c52b17e
2ebfefd
e16a1ca
c9aaa7a
ab1e16c
1421faf
209b644
623bd7d
b381b0d
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,2 @@ | ||
| 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? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| let age = 33; | ||
| age = age + 1; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| // 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}`); | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,22 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const cardNumber = "4533787178994213"; | ||
| const last4Digits = cardNumber.slice(-4); | ||
|
Comment on lines
-1
to
2
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. Suppose you were not allowed to modify the statement
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 could also do same by converting card number to string using cardNumber.toString().slice(-4). I had used the (“”) to convert the digits to String in this code |
||
| 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 | ||
|
|
||
| The code is not working because the "slice" method is being used incorrectly. The "slice" | ||
| method should be called on the string "cardNumber" to extract the last 4 digits. However, | ||
| if there is a syntax error or if the variable "cardNumber" is not defined correctly, it could lead to an error. | ||
|
|
||
| // 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? | ||
| It gives an error because the "slice" method is being called on a string, but if the string is not defined correctly or | ||
| if there is a typo in the variable name, it will result in an error. This is what I predicted, as | ||
| I anticipated that the issue would be related to how the "slice" method is being used on the string. | ||
|
|
||
| // Then try updating the expression last4Digits is assigned to, in order to get the correct value | ||
|
|
||
| I updated the expression to correctly use the "slice" method on the "cardNumber" string. I converted the cardNumber to a | ||
| string with the ("") and then used the slice method to extract the last 4 digits. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| const 12HourClockTime = "8:53pm"; | ||
| const 24hourClockTime = "20:53"; | ||
| const TwelveHourClockTime = "8:53pm"; | ||
| const TwentyFourHourClockTime = "20:53"; | ||
|
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. In JS naming convention, variable names usually begins with a lowercase letter. Names starting with an uppercase letter are used for built-in or custom data types (e.g.,
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. Thank you for pointing that out. I never knew I made such mistake 😀. I have made the correction to reflect the camelCasing convention. |
||
| 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,35 @@ 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 | ||
| There are five function calls in this file. The lines where function calls are made are: | ||
| 1. Line 4: carPrice.replaceAll(",", "") | ||
| 2. Line 5: priceAfterOneYear.replaceAll(",", "") | ||
| 3. Line 7: Number(carPrice.replaceAll(",", "")) | ||
| 4. Line 8: Number(priceAfterOneYear.replaceAll(",", "")) | ||
| 5. 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 error is occurring on line 5 because the "," character was ommited in the replaceAll function, to fix the problem, | ||
| we need to include the "," character in the replaceAll function like this: priceAfterOneYear.replaceAll(",", "") | ||
|
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. We could more precisely describe "A comma is missing between What is this programming term that refers to the values passed to a function? It begins with an 'a'.
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. Thank you so much for this, I have made a correction to that effect, also I now know the difference between argument and a parameter. While they are used interchangeably, an argument is the exact value we passed to the function while parameter is the placeholder value |
||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
|
|
||
| The lines that are variable reassignment statements are: | ||
| 1. Line 4: carPrice = Number(carPrice.replaceAll(",", "")); | ||
| 2. Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| The lines that are variable declarations are: | ||
| 1. Line 1: let carPrice = "10,000"; | ||
| 2. Line 2: let priceAfterOneYear = "8,543"; | ||
| 3. Line 6: const priceDifference = carPrice - priceAfterOneYear; | ||
| 4. Line 7: const percentageChange = (priceDifference / carPrice) * 100; | ||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| The expression "Number(carPrice.replaceAll(",", ""))" is performing two main operations: | ||
| 1. "carPrice.replaceAll(",", "")": This part of the expression removes all commas from the string representation of | ||
| "carPrice". For example, it converts "10,000" to "10000". | ||
| 2. "Number(priceAfterOneYear.replaceAll(",", ""));": This part converts the resulting string (after removing commas) | ||
| into a numeric value. The purpose of this expression is to convert the string representation of a number that | ||
| may contain commas into an actual numeric value that can be used for mathematical calculations. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| const movieLength = 8784; // length of movie in seconds | ||
| const movieLength = -9876; // length of movie in seconds | ||
|
|
||
| const remainingSeconds = movieLength % 60; | ||
| const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
|
|
@@ -11,15 +11,41 @@ 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? | ||
| //There are six variable declarations in this program. The lines where variable declarations are made: | ||
| //1. Line 1: const movieLength = 8784; | ||
| //2. Line 3: const remainingSeconds = movieLength % 60; | ||
| //3. Line 4: const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
| //4. Line 6: const remainingMinutes = totalMinutes % 60; | ||
| //5. Line 7: const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
| //6. Line 9: const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; | ||
|
|
||
| // b) How many function calls are there? | ||
| //There are two function calls in this program. The lines where function calls are made are: | ||
| //1. Line 9: `${totalHours}:${remainingMinutes}:${remainingSeconds}`; | ||
| //2. Line 10: console.log(result); | ||
|
|
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
|
|
||
| //The expression "movieLength % 60" calculates the remainder when the value of "movieLength" is divided by 60. | ||
| //In this context, it is used to determine the number of seconds remaining after converting the total movie | ||
| //length from seconds into minutes. Since there are 60 seconds in a minute, this operation effectively gives us the | ||
| //leftover seconds that do not make up a full minute. | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| //The expression assigned to "totalMinutes" in line 4 calculates the total number of minutes in the movie length. | ||
| //It does this by first subtracting the remaining seconds (calculated in line 3) from the total movie length in seconds. | ||
| //This gives the total number of seconds that can be fully converted into minutes. Then, it divides that value by 60 to | ||
| //convert the total seconds into total minutes. The result is the total number of complete minutes in the movie length, | ||
| //excluding any leftover seconds. | ||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
| // The variable result represents the formatted time string in the format "hours:minutes:seconds". | ||
| // A better name for this variable could be "formattedTime" or "movieDuration" | ||
|
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. Note:
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. Thank you for letting me Know this |
||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| //The code will work for all positive integer values of movieLength, as it correctly calculates the hours, minutes, | ||
| //and seconds based on the total length in seconds. But will not work for negative values of movieLength, | ||
| //as it will produce negative hours, minutes, and seconds, which does not make sense in the context of a movie length. | ||
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.
Note: We could also use the concise and precise interval notation to describe a range of values.
[,]=> inclusion(,)=> exclusionFor example,$x$ is a number in $[1, 10)$ means:
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 for your feedback, I have corrected the explanation to reflect the inclusion and exclusion terms