-
-
Notifications
You must be signed in to change notification settings - Fork 241
Birmingham|25-ITP-SEP|Hadi Vahidi|Sprint 1|coursework/sprint-1 #742
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 20 commits
1189fcd
78b6a33
ea977e5
22c0fee
c346f52
ea0fca9
5df448f
ef4f642
efa5aa8
aa951b4
921ba5a
e32fceb
3b87801
24ee904
d51395c
14e15a6
711385f
e82c680
b28011e
6c7a0b7
0cf3390
aa53b91
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,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?*/ | ||
|
|
||
| // we can use comments to ignore these lines of code, in javascript we use // for single line comments and /* */ for multi line comments |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,22 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
|
|
||
|
|
||
| const age = 33; | ||
| age = age + 1; | ||
| console.log(age); | ||
| /*the constant variable cannot be reassigned a new value | ||
| so in lin 6 we will get an error because it can not reassign the age variable with a new value of age + 1 which in this case is 34 | ||
| here is the error message we will get:/home/cyf/CYF/ITP/Module-Structuring-and-Testing-Data/Sprint-1/2-mandatory-errors/1.js:6 | ||
| age = age + 1; | ||
| ^ | ||
|
|
||
| TypeError: Assignment to constant variable. | ||
|
|
||
| to fix this error we can use let instead of const because let variable can be reassigned a new value*/ | ||
|
|
||
| let age2 = 33; | ||
| age2 = age2 + 1; | ||
| console.log(age2); | ||
|
|
||
| // now it will work fine and the output will be 34 and every time we run the code it will increase by 1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,17 @@ | ||
| // 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}`); | ||
| //console.log(`I was born in ${cityOfBirth}`); | ||
| const cityOfBirth = "Bolton"; | ||
|
|
||
| /*/home/cyf/CYF/ITP/Module-Structuring-and-Testing-Data/Sprint-1/2-mandatory-errors/2.js:4 | ||
| console.log(`I was born in ${cityOfBirth}`); | ||
| ^ | ||
|
|
||
| ReferenceError: Cannot access 'cityOfBirth' before initialization | ||
|
|
||
| this error is because we are trying to access the variable cityOfBirth before it is initialized and assigned a value. | ||
| the console.log should be after the variable declaration and assignment | ||
|
|
||
| */ | ||
| console.log(`I was born in ${cityOfBirth}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,18 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = cardNumber.toString().slice(-4); | ||
| console.log(`The last 4 digits of my card number are ${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 | ||
|
|
||
|
|
||
|
|
||
| //it doesn't work because the slice method is used for strings and arrays but cardNumber is a number so it doesn't have the slice method. | ||
|
|
||
|
|
||
|
|
||
| // 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? | ||
| // Then try updating the expression last4Digits is assigned to, in order to get the correct value | ||
| // to fix this error we can convert the number to a string using the toString() method before applying the slice method |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| const twelveHourClockTime = "08:53"; | ||
| const twentyFourHourClockTime = "20:53"; | ||
|
|
||
| //tha variable names are cannot start with a number | ||
| // so the variable name 12HourClockTime is not valid because it starts with a number | ||
| // to fix this error we can change the variable name to twelveHourClockTime and as is not relevant to the time format | ||
| // so we can swap the variable names. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,59 @@ 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? | ||
| /*there are six variable declarations in this program: | ||
| 1. movieLength | ||
| 2. remainingSeconds | ||
| 3. totalMinutes | ||
| 4. remainingMinutes | ||
| 5. totalHours | ||
| 6. result | ||
| */ | ||
|
|
||
| // b) How many function calls are there? | ||
|
|
||
| /*There are no function calls in this code. Everything here is using operators and expressions, not calling functions.*/ | ||
|
||
|
|
||
| // 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 in JavaScript is the remainder operator. It divides the left-hand number by the right-hand number and returns the remainder. | ||
|
|
||
| So in this case: | ||
|
|
||
| movieLength % 60 | ||
|
|
||
| divides movieLength (8784) by 60 and gives the remaining seconds, which is 24.*/ | ||
|
|
||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
|
|
||
| /*The expression | ||
|
|
||
| totalMinutes = (movieLength - remainingSeconds) / 60; | ||
|
|
||
| subtracts the leftover seconds from the total seconds so that we get a whole number of minutes. | ||
| Then it divides by 60 to convert seconds into minutes without any decimal part.*/ | ||
|
|
||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
| /*The variable result represents the length of the movie in the format hours:minutes:seconds. | ||
|
|
||
| A better name could be something like: | ||
|
|
||
| movieDurationFormatted | ||
|
|
||
| formattedDuration | ||
|
|
||
| movieLengthHMS (HMS = hours, minutes, seconds)*/ | ||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer, | ||
| /*Yes, it works for all non-negative values: | ||
|
|
||
| If the movie is less than an hour, totalHours is 0, so we get 0:MM:SS. | ||
|
|
||
| If the movie is less than a minute, both totalHours and remainingMinutes are 0, so we get 0:0:SS. | ||
|
|
||
| For larger movies, it correctly calculates hours, minutes, and seconds. | ||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| The only exception would be negative values, which aren’t realistic for a movie length.*/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,69 @@ | ||
| const penceString = "399p"; | ||
| const penceString = "3679p"; | ||
|
|
||
| const penceStringWithoutTrailingP = penceString.substring( | ||
| 0, | ||
| penceString.length - 1 | ||
| ); | ||
| const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); | ||
|
|
||
| console.log(penceStringWithoutTrailingP); | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| ); | ||
|
|
||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
| console.log(paddedPenceNumberString); | ||
|
|
||
| const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2); | ||
|
|
||
| console.log(pounds); | ||
|
|
||
| const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); | ||
|
|
||
| console.log(pence); | ||
|
|
||
| console.log(`£${pounds}.${pence}`); | ||
|
|
||
|
|
||
|
|
||
| // This program takes a string representing a price in pence | ||
| // The program then builds up a string representing the price in pounds | ||
|
|
||
| // You need to do a step-by-step breakdown of each line in this program | ||
|
|
||
|
|
||
|
|
||
| // Try and describe the purpose / rationale behind each step | ||
|
|
||
| // To begin, we can start with | ||
| // 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
|
|
||
| /*1. const penceString = "399p"; | ||
|
|
||
| We create a variable called penceString and give it the value "399p". | ||
|
|
||
| This is the price in pence, written as text. | ||
|
|
||
| 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); | ||
|
|
||
| We remove the last letter "p" from the string. | ||
|
|
||
| Now we only have the numbers, for example "399". | ||
|
|
||
| **3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
|
|
||
| We make sure the string has at least 3 characters by adding zeros at the start if needed. | ||
|
|
||
| Example: "50" becomes "050" and "5" becomes "005". | ||
|
|
||
| 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); | ||
|
|
||
| We take all characters except the last two. | ||
|
|
||
| These characters are the pounds. Example: "399" → "3" pounds. | ||
|
|
||
| 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); | ||
|
|
||
| We take the last two characters. These are the pence. | ||
|
|
||
| We add a zero at the end if needed to always have two digits. Example: "5" → "05". | ||
|
|
||
| 6. console.log(£${pounds}.${pence}); | ||
|
|
||
| We show the price in pounds and pence. | ||
|
|
||
| Example: "399p" → £3.99, "50p" → £0.50. */ |
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.
Are you sure there are only 4?