-
-
Notifications
You must be signed in to change notification settings - Fork 305
London | 26-ITP-January | Karla Grajales | Sprint 1 | coursework/sprint-1 #910
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
6c59bf1
4ce90b9
f1b26c1
89a42ab
8c269ba
bd483c1
9be9270
982f3b3
f7799bb
b9b2ba8
850edaa
1795109
924741c
188692d
0870089
aaaaf02
f00b454
2bfaf27
9a8bc46
d1fffc7
5ddfa8d
9127036
29f3107
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 |
|---|---|---|
|
|
@@ -12,12 +12,23 @@ | |
| 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 = ; | ||
| const lastDot = filePath.lastIndexOf("."); | ||
| console.log(`The indexDot is ${lastDot}`); | ||
|
|
||
| const ext = filePath.slice(lastDot); | ||
|
|
||
| const fileName = filePath.slice(lastSlashIndex + 1, lastDot); | ||
| const dir = filePath.slice(1, lastSlashIndex + 1); | ||
|
|
||
|
|
||
|
|
||
| console.log(`The dir is 👉 ${dir}`); | ||
| console.log(`The nameFile is 👉 ${fileName}`); | ||
|
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's good practice to infuse an additional variable into your task. Well done. |
||
| console.log(`The extension is 👉 ${ext}`); | ||
|
|
||
|
|
||
|
|
||
| // https://www.google.com/search?q=slice+mdn | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,17 @@ | ||
| // 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 | ||
|
|
||
| const minimum = 1; | ||
| const maximum = 100; | ||
|
|
||
| const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
| //this line will generate a random number between minimum and maximum (inclusive) | ||
| // and math.floor will remove decimal part | ||
|
|
||
| // 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 | ||
| Math.floor() //removes decimal part and returns whole number | ||
| Math.random() //needs to values between minimum and maximum to generate a random number | ||
|
|
||
| console.log(maximum - minimum + 1 ) + minimum; // this is same as 100 - 1 + 1 = 100 | ||
| console.log(`The random number is ${num}`); | ||
|
Comment on lines
+13
to
+17
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. You need to do more in following the given task instruction. Is there a way you could have broken down the flow of the code more? Rather than focusing on just highlighting the math object, why not expatiate on how the equation is solved to arrive at the value in the num variable. |
||
| 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 | ||
| //to write comments in JavaScript, we use two forward slashes like this so, for this exercise, | ||
| // we can comment out the two lines above to prevent them from being executed. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| age = age + 1; | ||
| let age = 33; | ||
| age += 1; | ||
| console.log(age); | ||
|
|
||
| // We need to use 'let' to reassign a variable; 'const' does not allow reassignment. |
| 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}`); | ||
|
|
||
| // explanation: the error was that the variable cityOfBirth was not defined before it was used in the console.log statement. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,20 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const numberString = cardNumber.toString(); | ||
| const last4Digits = numberString.slice(-4); | ||
| const num = Number(last4Digits); | ||
|
|
||
| console.log(num); | ||
|
Comment on lines
+2
to
+6
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. Very good implementation and variable naming convention. |
||
|
|
||
| // 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 | ||
|
|
||
|
|
||
|
|
||
| // explanation: to see the numbers correctly we need to convert the cardNumber to a string | ||
| // first before slicing the last 4 digits. The original code was trying to use slice | ||
| // a number, which is not valid. By converting it to a string first, | ||
| // we can then slice the last 4 characters correctly. Then we convert it back to a number. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,11 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
|
|
||
| // explanation: the first lines will throw an error because js detect a number instead of a variable name, | ||
| // so we move the number to the end and now it works fine. | ||
| // const 24hourClockTime = "08:53; | ||
| // const 12HourClockTime = "20:53"; | ||
|
|
||
|
|
||
| const clock12h = "20:53"; | ||
| const clock24h = "08:53"; | ||
| console.log(clock12h); | ||
| console.log(clock24h); |
| 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; | ||
|
|
@@ -13,10 +13,51 @@ console.log(`The percentage change is ${percentageChange}`); | |
|
|
||
| // a) How many function calls are there in this file? Write down all the lines where a function call is made | ||
|
|
||
| // There are 6 function calls in this file: | ||
| // Line 5: carPrice.replaceAll(",", "") | ||
| // Line 5: Number(carPrice.replaceAll(",", "")) | ||
| // Line 6: priceAfterOneYear.replaceAll(",", "") | ||
| // Line 6: Number(priceAfterOneYear.replaceAll(",", "")) | ||
| // Line 10: console.log(`The percentage change is ${percentageChange}`) this is also | ||
| // a function call to log the output as string to the console, then call the var percentageChange | ||
|
Comment on lines
+16
to
+22
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 have a look at the number of function calls again and if you are sure, provide a little textual explanation for your reasoning. |
||
|
|
||
|
|
||
| // 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? | ||
|
|
||
| // The error was line 5, carPrice.replaceAll("," ""); missing the comma between the arguments | ||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
|
|
||
| //line 4, line 5, | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
|
|
||
| // line 1, line 2, line 7 and line 8 | ||
|
|
||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
|
|
||
| // This expression will work in the parenthesis first, replacing all commas in the string carPrice | ||
| // with an empty string, so basically returning the string 10000 instead of "10,000". | ||
| // Then the Number() function will convert that string "10000" into a number 10000. | ||
| // secondly, js can not treat strings with commas as numbers, so we need to remove the commas | ||
| // now with the number cleaned we can convert this string in number with the method Number(). | ||
|
|
||
|
|
||
|
|
||
| // ---------- console.log each step to see the results ---------- | ||
|
|
||
| // let carPrice = "10,000"; | ||
| // let priceAfterOneYear = "8,543"; | ||
|
|
||
| // carPrice = Number(carPrice.replaceAll(",", "")); | ||
| // console.log(carPrice); // the commas are removed and the string is converted to a number | ||
|
|
||
| // priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
| // console.log(priceAfterOneYear); // the commas are removed and the string is converted to a number | ||
|
|
||
| // const priceDifference = carPrice - priceAfterOneYear; | ||
| // console.log(`The price difference is ${priceDifference}`); | ||
|
|
||
| // const percentageChange = (priceDifference / carPrice) * 100; | ||
| // console.log(`The percentage change is ${percentageChange}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| const movieLength = 8784; // length of movie in seconds | ||
| const movieLength = 5; // length of movie in seconds | ||
|
|
||
| const remainingSeconds = movieLength % 60; | ||
| const remainingSeconds = movieLength % 60; console.log(remainingSeconds);//24 | ||
| const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
| console.log(totalMinutes);//146 | ||
|
|
||
| const remainingMinutes = totalMinutes % 60; | ||
| const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
|
|
@@ -13,13 +14,36 @@ console.log(result); | |
|
|
||
| // a) How many variable declarations are there in this program? | ||
|
|
||
| // There are six variable declarations in this program: | ||
| // movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result. | ||
|
|
||
| // b) How many function calls are there? | ||
|
|
||
| //There is one function call in this program: console.log(result); | ||
| //In JavaScript, a function call almost always involves parentheses () immediately following a name. | ||
|
|
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
|
|
||
| //the expression movielength % 60 calculates the remainder when movieLength is divided by 60. | ||
| //this means this calculation 8784/60 = 146.4 but we are looking for the seconds | ||
| //we take the integer part 146 * 60 = 8760 - 8784 = 24 seconds remaining | ||
|
|
||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
|
|
||
| // Line 4 calculates the total number of whole minutes in the movie length. | ||
| // It subtracts the remaining seconds from the total movie length to get a value that is a multiple of 60, | ||
| // and then divides that value by 60 to convert it from seconds to minutes. | ||
| // So, it gives us the total number of complete minutes in the movie length. | ||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
|
|
||
| //result stores the variables of totalHours, remainingMinutes and remainingSeconds in a string format readable time clock "H:M:S" | ||
| //A better name for this variable could be MovieDuration | ||
|
Comment on lines
+42
to
+43
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. Great contextual naming convention |
||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
|
|
||
|
|
||
| //Yes, this code will work for all values of movieLength as it correctly calculates hours, minutes, and seconds | ||
| //but if the movieLength is less than 5 seconds, will be 0:0:5, we need to use the padStart method to add leading zeros for better formatting. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,9 +10,41 @@ Let's try an example. | |
| In the Chrome console, | ||
| invoke the function `alert` with an input string of `"Hello world!"`; | ||
|
|
||
| To invoke an alert that lives in the engine v8; we need to | ||
| run `alert("Hi there!")` | ||
|
|
||
|
|
||
| What effect does calling the `alert` function have? | ||
|
|
||
| In short: alert() interrupts the page to show a message and waits for the user to acknowledge it. | ||
|
|
||
| 📢 The message you pass to alert() is displayed to the user. | ||
|
|
||
| ⏸️ JavaScript execution pauses until the user clicks OK. | ||
|
|
||
| 🚫 The user can’t interact with the page while the alert is open (it blocks the UI). | ||
|
|
||
|
|
||
|
Comment on lines
+21
to
+27
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. Great and detailed response |
||
|
|
||
| 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("hello what is your name") | ||
| alert("hi " + myName) | ||
|
|
||
| The prompt function displays a modal dialog that captures user input. The value entered is stored in myName, and alert then displays a greeting that includes that value. | ||
|
|
||
|
|
||
| What effect does calling the `prompt` function have? | ||
| What is the return value of `prompt`? | ||
|
|
||
| Calling the prompt() function displays a modal pop-up dialog that: | ||
|
|
||
| * Shows a message to the user | ||
|
|
||
| * Includes a text input field | ||
|
|
||
| * Has OK and Cancel buttons | ||
|
|
||
| * Blocks interaction with the page until the user responds | ||
|
|
||
|
Comment on lines
29
to
+50
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. Are there ways to improve your implementation to follow the acceptance criteria for the tasks? Read through it and practise again. I'm sure you will get new insights |
||
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.
Nicely done. It might be better practice to stick with the variable declaration format already provided.