-
-
Notifications
You must be signed in to change notification settings - Fork 389
Manchester | May-2026-ITP | Monsur Abdulrahman | Sprint 1 | Coursework Exercises #1334
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
8095f51
3fd21e6
688f25b
63c562a
cab937e
7186dfa
27bff53
2f2a720
1cc8d56
89ca1ed
f402c6f
ed8a5e7
9d06c8b
d3193ff
d60c972
b7acf7c
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,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}`); | ||
| const cityOfBirth = "Bolton"; | ||
| console.log(`I was born in ${cityOfBirth}`); | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,21 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = cardNumber.toString().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 | ||
|
|
||
| The code didnt work because the slice method was called on an integer (cardNumber), | ||
| but slice is a method that is only available for strings and arrays. | ||
| Since cardNumber is an integer, it does not have the slice method, | ||
| which will result in an error when the code is run. | ||
|
|
||
| // Then run the code and see what error it gives. | ||
| The error was: TypeError: cardNumber.slice is not a function | ||
|
|
||
| // Consider: Why does it give this error? Is this what I predicted? If not, what's different? | ||
| It gives this error because the slice method is not meant for numbers. This is what I predicted, as I expected that calling slice on an integer would result in a TypeError. | ||
|
|
||
| // Then try updating the expression last4Digits is assigned to, in order to get the correct value | ||
|
|
||
| const last4Digits = cardNumber.toString().slice(-4); |
| 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"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,11 +12,36 @@ 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 4 function calls in this file. | ||
| The lines where a function call is made are: | ||
|
|
||
| 1. carPrice.replaceAll(",", "") | ||
| 2. Number(carPrice.replaceAll(",", "")) | ||
| 3. priceAfterOneYear.replaceAll("," "") | ||
| 4. Number(priceAfterOneYear.replaceAll("," "")) | ||
|
|
||
|
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 work finding these, I think there is one more function call to find
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 taking time to review my work, I appreciate that. I later found out the there is one function call i didnt include which is "console.log" Thank you so much |
||
| // 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 coming from the line: | ||
| priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
|
|
||
| The error is occurring because there is a missing comma in the replaceAll method. The correct syntax should be: | ||
|
|
||
| priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
| The lines that are variable reassignment statements are: | ||
| 1. carPrice = Number(carPrice.replaceAll(",", "")); | ||
| 2. priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| The lines that are variable declarations are: | ||
| 1. let carPrice = "10,000"; | ||
| 2. let priceAfterOneYear = "8,543"; | ||
| 3. const priceDifference = carPrice - priceAfterOneYear; | ||
| 4. const percentageChange = (priceDifference / carPrice) * 100; | ||
| 5. console.log(`The percentage change is ${percentageChange}`); | ||
|
|
||
| // 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 operations: | ||
| 1. It is using the replaceAll method to remove all commas from the string value of carPrice, resulting in a string that represents a number without any formatting (e.g., "10000"). | ||
| 2. It is then converting that string into a number using the Number() function, so that it can be used in mathematical calculations. | ||
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 break down of the individual steps. Can you give a short explanation of what the script does overall?
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 so much for taking time to review my work. Below is the explanation of what the script does as requested.
The script generates a random whole number between 1 and 100, and then stores it in the variable num. It uses Math.random() to produce a random decimal number, make it to the desired range and converts it into a whole number with Math.floor(), and adjusts the results so that the minimum value is 1 and the maximum is 100. Each time the program runs, a different number is likely to be generated