-
-
Notifications
You must be signed in to change notification settings - Fork 546
Manchester | 26-ITP-Sep | Rahana Suleiman | Sprint 2 | Complete Sprint 2 Coursework #1563
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
f2e91c5
94e0982
0cad20d
14ba89d
aeabc99
814abb5
dc97183
8ada20c
936de83
55fa5e7
2fc4870
4922e00
bb34ab4
23673ee
5f58ebd
e8de7c3
4f6289b
88beb1f
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 |
|---|---|---|
| @@ -1,9 +1,17 @@ | ||
| const minimum = 1; | ||
| const maximum = 100; | ||
| const minimum = 1; // Sets the lowest possible number that can be generated | ||
| const maximum = 100; // Sets the highest possible number that can be generated | ||
|
|
||
| const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
|
|
||
| // 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 | ||
|
|
||
| const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
| // Math.random() returns a random decimal between 0 and 1 | ||
| // Math.random() * maximum(100) returns a random decimal between 0 and 99.999 | ||
| // Math.floor() rounds up the decimal to the nearest whole number | ||
| // maximum and minimum calculates thee total output of the number(100) | ||
|
|
||
| // 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 represents a random whole number between 1 and 100 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| 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?*/ | ||
|
|
||
|
|
||
| //ANSWER: | ||
|
|
||
| // We comment them out |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| // The problem here is that 'const' creates a constant variable which cannot be reassigned, attempting to reassign (age = age + 1) throws a TypeError: Assignment to constant variable. | ||
|
|
||
|
|
||
| //change 'const' to 'let' so the value can be updated | ||
| // const age = 33; | ||
| let age = 33; | ||
| age = age + 1; | ||
|
|
||
| // console.log(age); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| // 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}`); | ||
| // The error is a ReferenceError: Cannot access 'cityOfBirth' before initialization | ||
| // In JavaScript variables declared with 'const' or 'let' cannot be accessed before they are declared or initialized | ||
|
|
||
|
|
||
| //SOLUTION | ||
| // move the variable declaration above the 'console.log()' | ||
| const cityOfBirth = "Bolton"; | ||
|
|
||
| console.log(`I was born in ${cityOfBirth}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,20 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
|
|
||
| // 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 | ||
|
|
||
| //Prediction: The code won't work because cardNumber is stored as a Number and not a string, this is because '.slice()' is a string method | ||
| // Numbers in JavaScript don't work with '.slice()' | ||
|
|
||
|
|
||
| // Then run the code and see what error it gives. | ||
| //This is the error it gives: TypeError: cardNumber.slice is not a function | ||
|
|
||
|
|
||
| // Consider: Why does it give this error? Is this what I predicted? If not, what's different? | ||
|
|
||
| //Explanation: Yes, my prediction was accurate. JavaScript threw a TypeError because '.slice()' is a string method | ||
| // Then try updating the expression last4Digits is assigned to, in order to get the correct value | ||
| const cardNumber = '4533787178994213'; | ||
| const last4Digits = cardNumber.slice(-4); | ||
|
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. thats one way to do it, but can we do it without changing the original value? |
||
|
|
||
| // console.log(last4Digits); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| const 12HourClockTime = "8:53pm"; | ||
| const 24hourClockTime = "20:53"; | ||
| // JavaScript variable names cannot start with a number. They must start with an alphabet(A-Z), an underscore(_), or a dollar sign($). JavaScript is also case sensitive so they have to use camelCase, PascalCase, among others. | ||
|
|
||
| const HourClockTime = "8:53pm"; | ||
|
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 fixes the issue, but are the variable names descriptive enough of the values they hold? |
||
| const hourClockTime = "20:53"; | ||
|
|
||
| // console.log(HourClockTime); | ||
| // console.log(hourClockTime); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,28 @@ console.log(result); | |
| // 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? | ||
| // ANSWER: There are 6 variable declarations. | ||
| // (movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result — all declared using const) | ||
|
|
||
| // b) How many function calls are there? | ||
| // ANSWER: There is only function call | ||
| // console.log(result) | ||
|
|
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
|
|
||
| //ANSWER: The expression movieLength % 60 uses the remainder (%) operator to calculate the number of seconds remaining that can't form a full seconds (8784 % 60 = 24 seconds) | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| // ANSWER: The expression assigned to totalMinutes means division | ||
|
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. the expression does include division, but please elaborate more what the whole expression is about. |
||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| // ANSWER: It subtracts the remainder seconds from movieLength to get an exact multiple of 60, then divides by 60 to convert the seconds into whole minutes (146 minutes). | ||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
| // ANSWER: A better name for the variable would be 'formattedDuration' because it clearly describes that the variable stores the movie duration in a formatted hours, minutes, and seconds format. | ||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
|
|
||
| // ANSWER: No. It breaks if movieLength is negative (giving negative time) or a string. | ||
| // Additionally, single-digit minutes or seconds (e.g., 4 minutes, 8 seconds) output as "2:4:8" instead of standard "02:04:08" formatting. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,8 @@ console.log(`£${pounds}.${pence}`); | |
|
|
||
| // To begin, we can start with | ||
| // 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
| // 2. const penceStringWithoutTrailingP: uses substring to remove the trailing "p" from "399p", leaving "399" | ||
| // 3. const paddedPenceNumberString: padStart() makes sure the string has at least 3 characters, adding "0" if required. If we have "5p" and we remove the "p" we would be left with "5", so padding it to 3 numbers will give us the output ("005") | ||
| // const pounds: removes everything except the final two digits, giving "3". | ||
|
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. it does give 3, however please review the statement again. Think about what the code does and does it match your explanation? |
||
| // 4. const pence = paddedPenceNumberString: extracts the final two digits, giving "99", and ensures they have at least two characters and adds zero if necessary. | ||
| // 5. console.log(`£${pounds}.${pence}`): combines the pound and pence values into the formatted price with the template literals to give "£3.99" | ||
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.
good start, however the following expression can be explained more.
(maximum - minimum + 1))+ minimum