-
-
Notifications
You must be signed in to change notification settings - Fork 322
Birmingham| ITP-Jan | Ahmad Roman Sanaye| Sprint 1| Structuring and Testing Data #958
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
739e416
f71288b
bb852dd
242fca7
38532cf
c3dd177
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 |
|---|---|---|
|
|
@@ -2,8 +2,22 @@ const minimum = 1; | |
| const maximum = 100; | ||
|
|
||
| const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
| console.log(num); | ||
|
|
||
| // In this exercise, you will need to work out what num represents? | ||
| // 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 | ||
|
|
||
| // 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would happen if we did
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. Thanks for the feedback! |
||
| // Math.random() is a method used to create numbers between (0-1) and usually it creates a decimal like (0,2) or etc. | ||
| // each time I run it, I got different number as Math.random() generates different number each time we run it. | ||
|
|
||
| //to respond to the question "What would happen if we did Math.floor(1.9);?" | ||
| // ===> the method Math.floor() rounds the decimal number down always, in this case the final output would be ==> 2 as our 1.9 would be 1 after our method round it and add it to our minimum number which is 1 so ==> 1 + 1 = 2. | ||
|
|
||
| // In addition our formula works we wont have 191, if we had Math.ceil() then the result would exceed our maximum number. | ||
|
|
||
| // Response to question "What does the + minimum do here?" | ||
| // ===> + minimum shifts the range so that the smallest possible number is minimum instead of 0. so if we don't have it our range would be (0, 99) and this as we have 1 as minimum and 100 as maximum, if we don't add + minimum then our formula wont work as we are expecting. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| 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?*/ | ||
|
|
||
| //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. | ||
|
Comment on lines
+5
to
+8
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. 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?
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 the feedback! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| age = age + 1; | ||
| let age = 33; | ||
| age = age + 1; // ===> or age++ | ||
| console.log(age); // ===> 34 | ||
|
|
||
| // we can't do that with const variable, because JS locks the reference. | ||
|
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. Also spot on, let's take the approach and fix the broken code over declaring additional variables.
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 the feedback! |
||
| // the best choice is to use "let" variable. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| // 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}`); | ||
| //const cityOfBirth = "Bolton"; | ||
|
|
||
| // the problem is that our variable is not declared when we print it. | ||
| // to make it work, we should declare our "cityOfBirth" variable before we print it. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,12 @@ | ||
| /* | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| */ | ||
| // As far as I know we can not start a variable name with a number in JS. | ||
| // Therefore these variables throw error. | ||
| // we can start the name of a variable with letters a-z, A-Z, _ ,and $ . | ||
| // This is how we correctly name our variables: | ||
| const twelveHourClockTime = "20:53"; | ||
| const twenty4HourClockTime = "08:53"; | ||
| console.log(`Twelve-hour clock: ${twelveHourClockTime}`); | ||
| console.log(`24-hour clock: ${twenty4HourClockTime}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,58 @@ | ||
| const movieLength = 8784; // length of movie in seconds | ||
|
|
||
| const remainingSeconds = movieLength % 60; | ||
| const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
| const movieLength = 8777; // length of movie in seconds | ||
| if (typeof movieLength !== "number") { | ||
| throw new Error("Movie length must be a number"); // Checks if it’s a number ==> prevents strings, null, undefined; | ||
| } else if (movieLength < 0) { | ||
| throw new Error("Movie length cannot be negative"); // Checks if it’s negative; | ||
| } else if(!Number.isSafeInteger(movieLength)) { | ||
| throw new Error("Movie length is too large"); // Checks if it’s too large ==> prevents unsafe numbers in JS; | ||
| } | ||
|
|
||
| let safeMovieLength = Math.floor(Number(movieLength)); // Creates clean and positive integer; | ||
|
|
||
| const remainingSeconds = safeMovieLength % 60; | ||
| const totalMinutes = (safeMovieLength - remainingSeconds) / 60; | ||
|
|
||
| const remainingMinutes = totalMinutes % 60; | ||
| const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
|
|
||
| const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; | ||
| console.log(result); | ||
| const movieDuration = `${String(totalHours).padStart(2, "0")}:${String(remainingMinutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; | ||
| console.log(`The movie length is --> ${movieDuration}`); | ||
|
|
||
| // For the piece of code above, read the code and then answer the following questions | ||
|
|
||
| // a) How many variable declarations are there in this program? | ||
| // we have 6 (const) variable declarations. | ||
|
|
||
| // b) How many function calls are there? | ||
|
|
||
| // ===> we have only one, which is console.log() on line 10. | ||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
|
|
||
| // The % operator gives the remainder after dividing two numbers. | ||
| // We divide the total seconds (movieLength) by 60 to see how many full minutes there are. Also the % operator gives the leftover seconds that do not make a full minute, this is what movieLength % 60 expression represents. | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| // ===> First we remove the leftover seconds that don’t make a full minute. Then we divide the remaining seconds by 60 to find how many full minutes there are. For example, 8724 seconds minus 24 leftover seconds gives 8700 seconds, and 8700 / 60 = 145 minutes. So totalMinutes = 145; | ||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
| // ===> The result variable is a string and represents the movie length format in Hour, minutes and seconds. | ||
| // ===> A better name can be " movieDuration or movieLengthFormat". and I replaced the result with movieDuration. | ||
|
|
||
|
|
||
| // 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. | ||
|
|
||
| // ===> Response to PR review question: Can you see any problem if we were to name it movieLength? | ||
| // We should not name the result variable movieLength because that name is already used for the total seconds. Using the same name would cause an error or confusion. It’s better to choose a new name like movieDuration or movieLengthFormat. | ||
|
|
||
| // Respond to question: "There are a few cases that might break this. Can you spot and mention a few?" | ||
|
|
||
| // ===> A few cases might break this code: | ||
| // If movieLength is negative, we could get negative hours, minutes, or seconds. | ||
| // If movieLength is not an integer, the minutes or hours could have decimals. | ||
| // Very large values might not display nicely without padding. | ||
| // Single-digit minutes or seconds are not padded (like 5 instead of 05). | ||
| // If movieLength is not a number, the calculations will fail.” */ | ||
|
|
||
| // to solve the edge cases, we need to write some more lines of code. Which I did add in above codes with comments. |
|
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. 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.
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 the feedback! |
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.
What does the
+ minimumdo here?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 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).