London | 26-ITP-May | Alex Jamshidi | Sprint 1 | Coursework#1314
London | 26-ITP-May | Alex Jamshidi | Sprint 1 | Coursework#1314Alex-Jamshidi wants to merge 13 commits into
Conversation
illicitonion
left a comment
There was a problem hiding this comment.
LGTM - I'll mark this as complete, but please do think about the comments I've left :)
| const lastDotIndex = filePath.lastIndexOf("."); | ||
|
|
||
| // https://www.google.com/search?q=slice+mdn No newline at end of file | ||
| const dir = filePath.slice(1, lastSlashIndex); |
There was a problem hiding this comment.
I realise the diagram above is a bit ambiguous, but the dir actually includes the leading / as well
|
|
||
| // https://www.google.com/search?q=slice+mdn No newline at end of file | ||
| const dir = filePath.slice(1, lastSlashIndex); | ||
| const ext = filePath.slice(lastDotIndex); |
There was a problem hiding this comment.
This works for this particular path.
What would happen if I gave you a filePath that didn't contain a . at all? What would you expect it to return? What will your solution return?
| // I can make a hypothesis that this returns a random number of some kind, hence the inconsistent result | ||
|
|
||
| // Let's find out... | ||
| // Code in inner-most brackets is evaluated first |
There was a problem hiding this comment.
Not quite, actually - it doesn't matter here, but the Math.random() will actually be evaluated before the inner parens.
The order of execution is generally top-to-bottom, left-to-right, with a few exceptions:
- When we call a function, we evaluate the arguments left-to-right before we can call the function (so we need to evaluate
Math.random() * (maximum minimum + 1) before we can callMath.floor`). - Things in parentheses get evaluated when we hit the opening parenthesis. So
1 + (2 * 3)will evaluate 1, then evaluate(2 * 3)before it does the1 + (3).
The point of the parentheses here is to say "do maximum - minimum before *", not "do maximum - minimum before Math.random()" :)
| // The function Math.floor appears to be rounding down | ||
| // Checking the documentation, this is true, it returns the largest integer <= the given number | ||
|
|
||
| // Therefore Math.floor(Math.random() * (maximum - minimum + 1)) must give random numbers between 0 and 99 |
There was a problem hiding this comment.
One precision thing here - when talking about ranges, we'd generally describe whether the bounds are inclusive of exclusive. Here "between 0 and 99" it isn't clear whether that includes 0 or starts from 1. I'd generally write this "between 0 and 99, inclusive" (or in other contexts, perhaps "between 0 (inclusive) and 100 (exclusive)")
Learners, PR Template
Self checklist
Changelist
Completed solutions to sprint 1 coursework