-
-
Notifications
You must be signed in to change notification settings - Fork 340
London | 26-ITP-May | Alex Jamshidi | Sprint 1 | Coursework #1314
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
ee778eb
df4c41b
3815976
591da7e
4003837
adbf420
5308baa
170c5b8
3db266c
3ac364c
cf671bc
51c7a88
299436c
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 |
|---|---|---|
|
|
@@ -17,7 +17,14 @@ console.log(`The base part of ${filePath} is ${base}`); | |
| // Create a variable to store the dir part of the filePath variable | ||
| // Create a variable to store the ext part of the variable | ||
|
|
||
| const dir = ; | ||
| const ext = ; | ||
| const lastDotIndex = filePath.lastIndexOf("."); | ||
|
|
||
| // https://www.google.com/search?q=slice+mdn | ||
| const dir = filePath.slice(1, lastSlashIndex); | ||
| const ext = filePath.slice(lastDotIndex); | ||
|
Member
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. This works for this particular path. What would happen if I gave you a filePath that didn't contain a |
||
|
|
||
| // Checks | ||
|
|
||
| console.log(`The directory part of ${filePath} is ${dir}`); | ||
| console.log(`The extension part of ${filePath} is ${ext}`); | ||
|
|
||
| // https://www.google.com/search?q=slice+mdn | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,44 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | |
| // Try breaking down the expression and using documentation to explain what it means | ||
| // 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 | ||
|
|
||
| console.log(num); | ||
|
|
||
| // num returns a value of 60, running it again returns a value of 15, looking at the code I see a .random() function | ||
| // 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 | ||
|
Member
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. Not quite, actually - it doesn't matter here, but the The order of execution is generally top-to-bottom, left-to-right, with a few exceptions:
The point of the parentheses here is to say "do |
||
| // I would expect (maximum - minimum + 1) to be evaluated first (100 - 1 + 1) would equal 100 | ||
|
|
||
| console.log(maximum - minimum + 1); | ||
|
|
||
| // the result was 100 | ||
| // this part of the code is then multiplied by Math.random() | ||
|
|
||
| console.log(Math.random()); | ||
|
|
||
| // running this code many times, it appears Math.random() returns a random number between 0 and 1 | ||
| // checking documentation online, this is correct (actually between 0 and 0.99999999999) | ||
| // therefore Math.random() * (maximum - minimum + 1) should return a random number between 1 and 100 | ||
|
|
||
| console.log(Math.random() * (maximum - minimum + 1)); | ||
|
|
||
| // It does, it returns it with many decimal places, the result of num didn't have any decimal places | ||
| // The next part of the code that is run is Math.floor, let's see what this does | ||
|
|
||
| const number = Math.random() * (maximum - minimum + 1); | ||
| console.log(number); | ||
| console.log(Math.floor(number)); | ||
|
|
||
| // because Math.random() creates a new random number every time it is run, I had to set the | ||
| // result to a variable so I can use the same number in multiple functions | ||
| // When number is 87.52, Math.floor is 87, when number is 70.02, math.floor is 70 | ||
| // 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 | ||
|
Member
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. 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)") |
||
| // The final part is the addition of minimum (+1) | ||
|
|
||
| // Therefore I can see this code produces a random whole integer between minimum and maximum | ||
| // Which in this case is an integer between 1 and 100 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| 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? | ||
|
|
||
| // I commented out the code so it is not run by the computer using '//' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,21 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = String(cardNumber).slice(-4); | ||
|
|
||
| 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 | ||
|
|
||
| // .slice needs to act on a string not a number, so I would expect something like 'incorrect input' as 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? | ||
|
|
||
| // TypeError: cardNumber.slice is not a function | ||
| // This is not exactly the words I expected the error to say but is correctly identifying the same error | ||
| // I think it is saying that cardNumber.slice is not a function of a number, hence the TypeError | ||
|
|
||
| // Then try updating the expression last4Digits is assigned to, in order to get the correct value | ||
|
|
||
| // I converted cardNumber to a string and error resolved and it return the correct output |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| const 24hourClockTime = "08:53"; | ||
|
|
||
| // The error: SyntaxError: Invalid or unexpected token | ||
| // This occurs as a variable cannot start with a number (it can include a number) | ||
| // Renaming to something like ClockTime12hour would solve this |
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.
I realise the diagram above is a bit ambiguous, but the dir actually includes the leading
/as well