Manchester | ITP-May-2026 | Szidonia Bodo | Sprint 1 | Coursework Exercises #1417
Manchester | ITP-May-2026 | Szidonia Bodo | Sprint 1 | Coursework Exercises #1417bodoszidi wants to merge 15 commits into
Conversation
…lready existing variables
| // Line 3, we reassign a new value to the count variable, | ||
| // the new value is count plus 1. No newline at end of file |
There was a problem hiding this comment.
Operation like count = count + 1 is very common in programming, and there is a programming term describing such operation.
Could you find out what one-word programming term describes the operation on line 3?
There was a problem hiding this comment.
The term would be incrementing ++ or if it's the opposite, decrementing --
| const dir = filePath.slice(1, 16); | ||
| const ext = filePath.slice(-4); | ||
| console.log(`The base part of ${dir} is ${ext}`); |
There was a problem hiding this comment.
Could you make these statements to work for any valid path? For example, they could still work correctly if we changed line 12 to
const filePath = "/Users/jacknguyen448/cyf/Module-2/Sprint-1/package.json";
There was a problem hiding this comment.
Yes, if we use the already created slash index and create a new dot index, the file path length can change at any point. The code below will find the "/" and the "." and we no longer need to count the length of our filepath:
const lastDotIndex = filePath.lastIndexOf(".")
const dir = filePath.slice(0, lastSlashIndex);
const ext = filePath.slice(lastDotIndex + 1);
|
|
||
| // 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 = Math.floor which will remove all the numbers after the decimal point |
There was a problem hiding this comment.
Could you look up the differences among Math.round(), Math.trunc(), Math.floor(), Math.ceil()?
All of these functions remove the decimal part of a number, but in different manner.
There was a problem hiding this comment.
Math.round() - rounds the number to the nearest -> .1 - .4 ⬇️ & .5 - .9 ⬆️
Math.trunc() - removes all the numbers after the decimal point - without rounding the number
Math.floor() - rounds down and returns the largest integer less than or equal to a given number. rounding the number down ⬇️
Math.ceil() - rounds the given number to the smallest integer greater than or equal to a given number. rounding the number ⬆️
The above answer I gave was actually correct for Math.trunc() - Math.floor() will actually round the number to the largest integer less than or equal to the given number - if we have a negative integer, we would get the lower number back, as Math.floor(-3.03) would return -4
There was a problem hiding this comment.
Your approach is correct, and you are right that either Math.trunc() or Math.floor() works. I just want you to be aware of different math functions.
| // (Math.random that will generate a random decimal number | ||
| // which will be times with the value of maximum - minimum + 1 + minimum ) | ||
| // which is 100 * the Math.random generated |
There was a problem hiding this comment.
Math.random() returns a random number within a range.
Could you describe what each of these expressions does, and the range of the numbers it may produce?
Math.random()Math.random() * (maximum - minimum + 1)Math.floor(Math.random() * (maximum - minimum + 1))Math.floor(Math.random() * (maximum - minimum + 1)) + minimum
Note: To describe a range of numbers, we could use the math interval notation:
[,]=> inclusion(,)=> exclusion
For example, [1, 10) means, all numbers between 1 and 10, including 1 but excluding 10.
There was a problem hiding this comment.
- Math.random() - generates a decimal number between [0, 1] - but excluding 1
- Math.random() * (maximum - minimum + 1) - the generated decimal number * 100 - [0, 100]
- Math.floor(Math.random() * (maximum - minimum + 1)) - the generated decimal number * 100 - Math.floor() round the number to the nearest lower number - [0, 99]
- Math.floor(Math.random() * (maximum - minimum + 1)) + minimum
- (the generated decimal * 100) +1 - Math.floor() round the number to the nearest lower number [1, 100]
| const cardNumber = "4533787178994213"; | ||
| const last4Digits = cardNumber.slice(-4); |
There was a problem hiding this comment.
Suppose you were not allowed to modify the statement const cardNumber = 4533787178994213;
(that is, keep the variable's value unchanged).
How would you modify the code (through type conversion) to still being able to use .slice(-4) to extract the last 4 digits from the given number.
There was a problem hiding this comment.
If I can't modify the number would need to be converted to a string:
const cardNumber = 4533787178994213
const cardNumberString = cardNumber.toString()
const last4Digit = cardNumberString.slice(-4)
or
const cardNumber = 4533787178994213
const last4Digit = cardNumber.toString().slice(-4)
| // there is 5 calls: 2* Number() 2* .replaceAll() AND 1* .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? | ||
| // line 5 missing a comma: .replaceAll("," "") |
There was a problem hiding this comment.
We could more precisely describe "A comma is missing between "," and "" in the function call" as:
A comma is missing between the ___________s.
What is this programming term that refers to the values passed to a function? It begins with an 'a'.
There was a problem hiding this comment.
The comma is missing between the two arguments "," and ""
| // to calculate the movie length into minutes | ||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
| // the result represent the movie length - hours minutes and second in a string - movieTime |
There was a problem hiding this comment.
The name movieTime does not quite indicate the value stored in the variable
is a formatted string in the form "2:12:02".
Could you suggest a more descriptive name?
There was a problem hiding this comment.
movieLengthFormatted - would represent the value in a clearer way, also would indicate to the given variable movieLength - but in a formatted output
| What is the return value of `prompt`? | ||
| What effect does calling the `prompt` function have? - the prompt will also display a message usually question - however this time you will have an empty field to enter the value or answer - a cancel and a OK button | ||
|
|
||
| What is the return value of `prompt`? - whatever the user inputs into the empty field No newline at end of file |
There was a problem hiding this comment.
What if the a enters something and then click "Cancel" instead of "OK?
There was a problem hiding this comment.
If the user cancels the prompt, the returned value will be null.
|
Well done addressing all comments, and they are all correct. |
Learners, PR Template
Self checklist
Changelist
to explain the code line by line
Questions