-
-
Notifications
You must be signed in to change notification settings - Fork 330
Sheffield | 26-ITP-January | Martha Ogunbiyi | Sprint 1 | Coursework #1081
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
577235d
21aec41
089f7cf
46a401b
97ad770
0aceeed
0c3a7bf
5468f43
36ad414
2e76b38
a5691d2
7d1a29e
4ad9366
6b7a4ed
4a965e5
50cc867
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,21 @@ | ||
| 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? | ||
|
|
||
| //The error found when running the code-> SyntaxError: Unexpected identifier 'is'. | ||
| // syntax errors means the grammar rules of the language has not been applied. The instruction is written in plain english in a js file. It is not written in js syntax so js cannot parse it. | ||
|
|
||
| // An identifier just means: | ||
| //A variable name | ||
| //A function name | ||
| // Or any word that isn’t a keyword | ||
| // So "is" is being treated like a variable name — and the parser wasn’t expecting one at that position. | ||
|
|
||
| // //Solution: | ||
| /*In this case, comment out the code by adding two forward slashes to a single line comment or | ||
| a single slash and a multiplication sign to both ends of a double line comment like the one used for this comment. | ||
| Other solutions could be checking: | ||
| Missing quotes | ||
| Missing operator | ||
| Missing comma | ||
| Or misplaced word | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,17 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = cardNumber.toString().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 | ||
| // 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 | ||
|
|
||
| //TypeError: cardNumber.slice is not a function | ||
| //The Number type value stored in cardNumber does not a slice method | ||
| //slice methods only works with arrays, strings or objects that has a callable property named slice | ||
|
|
||
| //Solution: Convert the number value to string | ||
| //Options: use a String Wrapper or toString method |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| const twelveHourClockTime = "20:53"; | ||
| const twentyFourHourClockTime = "08:53"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ let carPrice = "10,000"; | |
| let priceAfterOneYear = "8,543"; | ||
|
|
||
| carPrice = Number(carPrice.replaceAll(",", "")); | ||
| priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
| priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ,"")); | ||
|
|
||
| const priceDifference = carPrice - priceAfterOneYear; | ||
| const percentageChange = (priceDifference / carPrice) * 100; | ||
|
|
@@ -12,11 +12,22 @@ console.log(`The percentage change is ${percentageChange}`); | |
| // Read the code and then answer the questions below | ||
|
|
||
| // a) How many function calls are there in this file? Write down all the lines where a function call is made | ||
| // Line 4 and 4 has two function calls Number() and replaceAll() | ||
| // - line 10 has a function console.log() | ||
|
|
||
| // b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? | ||
| // Line 5- | ||
| // priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
| // ^^^ | ||
| // SyntaxError: missing ) after argument list | ||
|
Contributor
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. Can you give a more specific reason why the error occurs? (You didn't fix the error by adding a |
||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
| // carPrice an priceAfterOneYear - lines 4 and 5 | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| //Lines - 1, 2, 7, 8 | ||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| // replaceAll() removes all occurrence of commas from the string e.g "10,000" becomes "10000" and the Number() wrapper converts | ||
| // the string to number type; "10000" becomes 10000 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,37 @@ | ||
| const movieLength = 8784; // length of movie in seconds | ||
| const movieLength = 9; // length of movie in seconds | ||
|
|
||
| const remainingSeconds = movieLength % 60; | ||
| const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
|
|
||
| const remainingMinutes = totalMinutes % 60; | ||
| const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
|
|
||
| const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; | ||
| const result = `${totalHours}:${String(remainingMinutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; | ||
| 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? | ||
| // 6 | ||
|
|
||
| // b) How many function calls are there? | ||
| // 1 | ||
|
|
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
| //Remainder operator. | ||
| //The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. | ||
| // It always takes the sign of the dividend. | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| // The value stored in totalMinute is calculating the number of minutes that have elapsed in the movie by subtracting the remaining time from the total duration and converting seconds to minutes. | ||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
| //The variable result represents a formatted time string in the format HH:MM:SS. | ||
| // A better name would be "movieDurationDisplay", as these names more clearly describe the purpose and content of the variable. | ||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| // No. Not for all possible values of movieLength, some EdgeCases are: | ||
| //1. when movieLength is of negative value, the maths produces negative time and its illogical for a movie duration | ||
| //2. if movieLength is not a number, its produces "NAN" and this breaks mathematically | ||
| //3. what if movieLength is less than 10? if movieLength is let say 9 , without proper formatting it will look like this 0:0:9 but with 0 padding it looks better like this 0:00:09 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ const penceStringWithoutTrailingP = penceString.substring( | |
| ); | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
|
|
||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
|
|
@@ -24,4 +25,10 @@ console.log(`£${pounds}.${pence}`); | |
| // 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": initializes a string variable with the value "399p" | ||
| 2. const penceStringWithoutTrailingP = This stores the substring of penceString by extracting all characters except the final p. It takes the substring from index 0 up to (but not including) the last character p, so "399p" becomes "399". This removes the unit symbol so only the numeric portion remains. | ||
| 3. const paddedPenceNumberString= This stores the value of penceStringWithoutTrailingP, padded to a total length of 3 characters by adding leading zeros where necessary. This guarantees there are always enough digits to separate pounds and pence correctly. | ||
| 4. const pounds= Stores the substring of paddedPenceNumberString from index 0 to the last 2 index. These leading digits represent the pound portion of the amount. | ||
| 5. const pence= Extracts the final two digits of the padded string to represent the pence portion. padEnd(2, "0") ensures that the pence value always contains exactly two digits. | ||
|
Contributor
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. Could we expect this program to work as intended for any valid |
||
| 6. console.log(`£${pounds}.${pence}`); logs the value of pounds and pence into a formatted currency string using template literals | ||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,10 +9,14 @@ Let's try an example. | |
|
|
||
| In the Chrome console, | ||
| invoke the function `alert` with an input string of `"Hello world!"`; | ||
| alert("Hello world!"); | ||
|
|
||
| What effect does calling the `alert` function have? | ||
| What effect does calling the `alert` function have? | ||
| A popup modal appears with the String -> "Hello world!" | ||
|
|
||
| Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. | ||
| let myName = prompt("What is your name?"); | ||
|
|
||
| What effect does calling the `prompt` function have? | ||
| What is the return value of `prompt`? | ||
|
|
||
| What effect does calling the `prompt` function have?A modal appears with the "What is your name?" with an input field to enter your name. The input is then stored in the variable myName | ||
| What is the return value of `prompt`? My name stored in the variable myName | ||
|
Contributor
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. I think the question is asking what return value can the caller expect from the function
|
||
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.
Operation like
count = count + 1is very common in programming, and there is a programming term describing such operation.Can you find out what one-word programming term describes the operation on line 3?