Birmingham| ITP-Jan | Ahmad Roman Sanaye| Sprint 1| Structuring and Testing Data#958
Birmingham| ITP-Jan | Ahmad Roman Sanaye| Sprint 1| Structuring and Testing Data#958RomanSanaye wants to merge 6 commits intoCodeYourFuture:mainfrom
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Sprint-1/1-key-exercises/3-paths.js
Outdated
| const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; | ||
| const lastSlashIndex = filePath.lastIndexOf("/"); | ||
| const base = filePath.slice(lastSlashIndex + 1); | ||
| const base = filePath.slice(-8); |
There was a problem hiding this comment.
We have updated the provided lastSlashIndex with a magic number like -8, what would happen if we now renamed our file.txt to best-file-ever.txt? Would this solution still work?
There was a problem hiding this comment.
Thank you for the feedback! @hkavalikas 🙏
I removed the magic number and updated the logic to use lastIndexOf("/") to dynamically locate the last slash, combined with substring() to safely extract the file name.
With this approach, the code will continue to work correctly even if the file name changes .
| // 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 variable that will be assigned an integer number after the operators are done. | ||
| // Math.floor() rounds a decimal to its nearest number to make it an integer. ex = 1.2 => 1. |
There was a problem hiding this comment.
What would happen if we did Math.floor(1.9);?
There was a problem hiding this comment.
Thanks for the feedback!
Math.floor() always rounds a number down to the nearest integer by removing the decimal part. Here it will return 1.
There was a problem hiding this comment.
Thank you for the feedback!
The + minimum shifts the random value into the desired range.
Without it, the formula would generate numbers between 0 and (maximum - minimum).
By adding minimum, the result is shifted to fall within the range of minimum to maximum (for example, 1 – 100).
| //solution: Simply comment the lines. | ||
| // 1- single line comment like: // example ... | ||
| // 2- multiline comment like : /* a, b, c */ | ||
| // comments are for instruction of guidance of who will read our codes, but the computer wont read them. No newline at end of file |
There was a problem hiding this comment.
Explanation is spot on, let's take it a step further and execute it! I want to run this file without a syntax error. Can we make it possible?
There was a problem hiding this comment.
Thank you for the feedback!
I have updated the file by commenting out the instructional lines so they are no longer executed by the interpreter.
| age = age + 1; | ||
| //age = age + 1; | ||
|
|
||
| // we can't do that with const variable, because JS locks the reference. |
There was a problem hiding this comment.
Also spot on, let's take the approach and fix the broken code over declaring additional variables.
There was a problem hiding this comment.
Thank you for the feedback!
I have updated the code by fixing the original variable instead of introducing additional variables.
|
|
||
| // we have variable declarations at lines 1, 2, 7 and 8. | ||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| // the purpose of that expression is to turn into number the string and replace all commas with an empty space. No newline at end of file |
There was a problem hiding this comment.
This comment is mostly correct, but I do want to highlight the second part of your answer. Is there anything we can do to improve it slightly?
There was a problem hiding this comment.
Thank you for the feedback!
We improved the explanation: the expression Number(carPrice.replaceAll(",", "")) first removes all commas from the string (for example, "1,000" becomes "1000"), and then converts the cleaned string into a number (1000).
| // movieLength % 60 expression represents the remaining seconds. | ||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
|
|
||
| // that expression turns the seconds into minutes. |
There was a problem hiding this comment.
You are right, but let's try to break it down step by step, how is the expression doing that?
There was a problem hiding this comment.
Thank you for the feedback!
I broke it down step by step:
movieLength % 60 gives the leftover seconds that don’t fit into a full minute.
movieLength - remainingSeconds removes those leftover seconds.
Dividing by 60 converts the remaining seconds into total minutes.
This shows clearly how the expression turns the total seconds into minutes while keeping track of leftover seconds.
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
|
|
||
| // the result variable represents the movie length in Hour, minutes and seconds. | ||
| // a better name can be "movieLength". |
There was a problem hiding this comment.
Can you see any problem if we were to name it movieLength?
There was a problem hiding this comment.
Thank you for the feedback!
If we name the final result variable movieLength, it could be confusing because we already have a variable movieLength holding the total seconds.
Using a different name, like movieDuration, makes the code clearer and avoids accidentally overwriting the original value.
| // the result variable represents the movie length in Hour, minutes and seconds. | ||
| // a better name can be "movieLength". | ||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| // as I changed the value of the movieLength the results has changed too.It means this code works with any value. No newline at end of file |
There was a problem hiding this comment.
There are a few cases that might break this. Can you spot and mention a few?
There was a problem hiding this comment.
Thank you for the feedback!
Some cases that could break the code include:
Negative numbers (a movie cannot have negative seconds)
Decimal numbers (like 8777.22)
Invalid inputs (like strings, null, or undefined)
Handling these cases ensures the program works safely and produces correct results.
There was a problem hiding this comment.
Optional: There are a few questions before the "Answer the following questions" too, if you want to add an answer to those too for completeness.
There was a problem hiding this comment.
Thank you for the feedback!
I have added answers to the optional questions for completeness.
…ll, random min, and safe movie length calculations with edge case handling
Self checklist
Changelist