-
-
Notifications
You must be signed in to change notification settings - Fork 275
West Midlands | ITP Sept 25 | Georgina Rogers | Sprint 1 | Structuring and Testing Data #761
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
c826f5e
d3921b1
467717b
b25d953
2e44be5
21f66d6
567de86
29a9fe7
6ce8a2e
8fb4c57
4229d96
22be92b
6b9b495
46ad0c8
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 |
|---|---|---|
|
|
@@ -10,14 +10,15 @@ | |
| // (All spaces in the "" line should be ignored. They are purely for formatting.) | ||
|
|
||
| const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; | ||
|
|
||
| const lastSlashIndex = filePath.lastIndexOf("/"); | ||
| const base = filePath.slice(lastSlashIndex + 1); | ||
| 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 = ; | ||
| // +1 to not include the slash in the base | ||
| // lastSlashIndex finds position of the last / in the string | ||
| // slice extracts part of the string from that position to the end | ||
| const dir = filePath.slice(0, lastSlashIndex); | ||
|
|
||
| // https://www.google.com/search?q=slice+mdn | ||
| const dotIndex = base.lastIndexOf("."); | ||
| const ext = base.slice(dotIndex); | ||
| // dotIndex finds position of the last . in the base string and slice extracts after this | ||
| console.log(`The base part of the file path is ${base}, the dir part is ${dir}, and the ext part is ${ext}`); | ||
|
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. solid solution. shows that you understand the slice() method and how to use it on strings. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,11 @@ const minimum = 1; | |
| const maximum = 100; | ||
|
|
||
| const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
| // num is an integer between minimum and maximum, between 1 and 100 inclusive | ||
| // math.random() produces a decimal between 0 (inclusive) and 1 (exclusive) | ||
| // math.floor() rounds down to the nearest integer | ||
| // (maximum - minimum + 1)) + minimum chooses a number between minimum and maximum and adds 1 so its inclusive of maximum | ||
|
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. good explainer. |
||
| 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| 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? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| // 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}`); | ||
|
|
||
| // The error is that cityOfBirth is used before it is declared. | ||
| // In JavaScript, variables declared with const or let cannot be accessed before their declaration due to the temporal dead zone. | ||
| // To fix the error, we need to declare and initialize cityOfBirth before using it in the console.log statement. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| 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 | ||
|
|
||
| // slice is a method that works on strings, not numbers. | ||
| // To fix the error, we can convert cardNumber to a string using the toString() method before calling slice. | ||
|
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. good catch. but if the variable is given as a number and you have converted toString(), you should make a conversion back, so you can return a number. when you begin to work with strongly-typed languages, |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| const ClockTime12Hour = "08:53"; | ||
| const ClockTime24Hour = "20:53"; | ||
| // Variables cannot start with a number | ||
| // Can only contain letters, numbers, underscores and $ | ||
| // Variables wre written wrong way around |
| 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,26 @@ 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 | ||
| // There are 5 function calls in this file: lines 3, 4 and 7 | ||
| // Line 4: carPrice,replaceAll(",", "") removes all commas from string | ||
| // Line 4: Number(...) converts string into number | ||
| // Line 5: priceAfterOneYear.replaceAll("," "") removes all commas from string | ||
| // Line 5: Number(...) converts string to number | ||
| // Line 10: console.log(`The percentage change is ${percentageChange}`) prints percentage change to output | ||
|
|
||
| // 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 is missing a comma in the replaceAll function call. This is causing a syntax error because the function expects two arguments, but only one is provided. | ||
|
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. good catch. |
||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
| // Variable reassignment means giving an existing variable a new value. | ||
| // Lines 4 and 5 reassign new values to carPrice and priceAfterOneYear respectively. | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| // Variable declaration means creating a new variable and assigning it a value for the first time. | ||
| // Line 1 declares carPrice with let | ||
| // Line 2 declares priceAfterOneYear with let | ||
| // Line 7 declares priceDifference with const | ||
| // Line 8 declares percentageChange with const | ||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| // It converts the string value of carPrice into a number value and removes the comma. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,29 @@ 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 6 variable declarations in this program on lines 1, 3, 4, 6, 7, 9. | ||
|
|
||
| // b) How many function calls are there? | ||
| // 1: console.log() on line 10. | ||
|
|
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // The remainder (%) operator returns the remainder left over when one operand (dividend) is divided by a second operand (divisor). | ||
| // It always takes the sign (+/-) of the dividend (the first number). | ||
| // So the movie length in seconds is divided by 60 (the number of seconds in a minute). | ||
| // The remainder is the number of seconds left over after all the full minutes have been accounted for. | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| // It subtracts the remaining seconds from total movie length to get the total length in seconds that can be evenly divided by 60. | ||
| // Then divides by 60 to convert the length from seconds to minutes. | ||
| // So totalMinutes represents the total number of full 60-second minutes in the movie. | ||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
| // The variable result represents the total length of the movie in hours, minutes and seconds. | ||
| // A better name for this variable could be movieDuration or movieFormattedLength. | ||
|
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.
having said that, |
||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| // The code will work for all positive integer values of movieLength formatting to H:MM:SS | ||
| // It will also work for 0, returning 0:0:0 | ||
| // It will incorrectly format negative numbers and very small number like < 60 seconds. | ||
| // Single digit minutes and seconds will not be zero-padded to two digits e.g. String(remainingMinutes).padStart(2, "0") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,42 @@ | ||
| const penceString = "399p"; | ||
| // Declares constant variable named penceString and assigns the string 399p | ||
| // This indicates price in pence | ||
|
|
||
| const penceStringWithoutTrailingP = penceString.substring( | ||
| 0, | ||
| penceString.length - 1 | ||
| ); | ||
| // Constant variable is declared to remove the trailing p in 399p to just keep the numeric part | ||
| // penceString.length is number of characters in 399p = 4 so -1 is 3 | ||
| // substring(0, 3) returns the characters from index 0 up to index 3 exclusive, returning 399. | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| // padStart ensures that the string has at least 3 characters by adding leading zeros if necessary | ||
| // This standardises the numeric string to simplify splitting into pounds and pence. | ||
| // In this case, 399 already has 3 characters, so no padding is added. | ||
|
|
||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| ); | ||
| // The last two characters represent pence | ||
| // Const pounds is declared as a variable consisting of a substring of previously declared variable and a newly calculated variable | ||
| // paddedPenceNumberString.length computes index where last two characters start, so that is after 1 character. | ||
| // substring(0, 1) extracts the pounds part, by returning the characters from index 0 up to index 1 exclusive. | ||
| // This is "3" in this case. | ||
|
|
||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
| const pence = paddedPenceNumberString | ||
| // New variable to ensure two-digit pence component is formatted correctly. | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| // returns substring from index to the end inclusive | ||
| // For 399, start index is character 1 (the first 9) so this returns 99. | ||
| .padEnd(2, "0"); | ||
| // Ensures pence string has at least 2 characters by adding trailing zeros if needed | ||
| // So 5p would become 005 and this would return 05. | ||
|
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. excellent explanation. |
||
|
|
||
| console.log(`£${pounds}.${pence}`); | ||
| // Template string that joins pounds and pence parts with a decimal point and logs it | ||
| // This displays the standard and prints it to the console. | ||
|
|
||
| // This program takes a string representing a price in pence | ||
| // The program then builds up a string representing the price in pounds | ||
|
|
||
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.
this simple solution works, so well done! however, there might be a better way to do this. think about if you ever had a lot more name variables and you had to provide a solution that automatically populates the initials.