-
-
Notifications
You must be signed in to change notification settings - Fork 239
London | 25-ITP-SEP | Shaghayegh Shirinfar| Sprint 2 | coursework/sprint-2 #779
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
955e5bf
f3b8cf5
a4c8522
7ed381b
1ff607f
fda7742
c810489
77a80c6
c5d6da1
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? | ||
|
|
||
| // Answer : we put them in comment syntax |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| 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,9 @@ | ||
| // Currently trying to print the string "I was born in Bolton" but it isn't working... | ||
| // what's the error ? | ||
| // what's the error ? | ||
| // const was declared after using it , we have to declare a variable first and then use it. | ||
|
|
||
| // console.log(`I was born in ${cityOfBirth}`); | ||
| // const cityOfBirth = "Bolton"; | ||
|
|
||
| console.log(`I was born in ${cityOfBirth}`); | ||
| const cityOfBirth = "Bolton"; | ||
| console.log(`I was born in ${cityOfBirth}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| // 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 | ||
| // 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 | ||
|
|
||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.toString().slice(-4); | ||
| console.log(last4Digits); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| // const 12HourClockTime = "20:53"; | ||
| // const 24hourClockTime = "08:53"; | ||
|
|
||
|
|
||
| //identifiers cannot start with a number in Javascript. |
| 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; | ||
|
|
@@ -11,12 +11,17 @@ 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 | ||
| // a) How many function calls are there in this file? Write down all the lines where a function call is made : | ||
| //Answer : 5 functions | ||
|
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. What are those 5 function calls? Please identify which lines these function calls are on |
||
|
|
||
| // 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? | ||
| //Answe: line 5 . we need a comma (,) between the arguments.puting (,) between ("," "") | ||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
| // c) Identify all the lines that are variable reassignment statements : | ||
| //Answe: line 4 and line 5 | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| //Answer: line 1, 2, 6, 7 | ||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| //Answer: Remove formatting commas from the price string and then convert it to a number so we can do arithmetic. | ||
|
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 nice! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,29 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
| // =============> Prediction: | ||
| // There will be an error because 'decimalNumber' is declared twice inside the function | ||
| // and console.log(decimalNumber) outside the function will not work. | ||
|
|
||
| // Original code: | ||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const decimalNumber = 0.5; // cannot declare again | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
| console.log(decimalNumber); // not defined | ||
|
|
||
| // =============> Explanation: | ||
| // 1. The function already has a parameter called 'decimalNumber', so using 'const decimalNumber' again is not allowed. | ||
| // 2. The variable 'decimalNumber' only exists inside the function. | ||
| // You cannot use it outside the function. | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> Corrected Code: | ||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = decimalNumber * 100 + "%"; // calculate percentage | ||
| return percentage; | ||
| } | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // Example | ||
| console.log(convertToPercentage(0.5)); // Output: "50%" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,26 @@ | |
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
| // =============> Prediction of the error: | ||
| // The function will give an error because '3' is not a valid variable name. | ||
| // Function parameters must have names, not numbers. | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write the error message here | ||
| // =============> Error message: | ||
| // SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // =============> Explanation of the error: | ||
| // 1. You cannot use a number as a parameter name. | ||
| // 2. Also, 'num' is not defined inside the function, so it will cause another error if we fix the parameter first. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
| // =============> Corrected Code: | ||
| function square(num) { | ||
| return num | ||
|
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. Does this still fulfil the purpose of this function - to "square any number"? |
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,26 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> Prediction: | ||
| // The program will print "The result of multiplying 10 and 32 is undefined" | ||
| // because the function multiply does not return a value, it only prints to the console. | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> Explanation: | ||
| // 1. The function multiply(a, b) uses console.log inside the function, but it does not return anything. | ||
| // 2. When we use ${multiply(10, 32)} inside the string, it tries to use the return value of multiply. | ||
| // 3. Since there is no return, JavaScript treats it as undefined. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // =============> Corrected Code: | ||
| // We need the function to return the result instead of printing it. | ||
|
|
||
| function multiply(a, b) { | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| // Output: "The result of multiplying 10 and 32 is 320" |
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.
Just as an extension - how would you write this if I just wanted the extension type without the . (So txt in this case rather than .txt)?