-
-
Notifications
You must be signed in to change notification settings - Fork 239
Birmingham | 25-ITP-Sep | Joy Opachavalit | Sprint 1 | coursework/sprint-1 #773
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
326e5d6
845eefa
9ae7180
49913e8
6efbcb4
c76b3f4
0600c9f
2c4af18
c75419d
ef37add
596785a
5250a3b
60669a0
585c235
85472fe
bb5c61c
42ec1bb
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,9 +1,23 @@ | ||
| const minimum = 1; | ||
| const maximum = 100; | ||
|
|
||
| const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
| const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
| 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 | ||
| // 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 | ||
|
|
||
| // Breaking down what num represents: | ||
| // num generates a random integer between 1 and 100 (inclusive) | ||
|
|
||
| // Step-by-step evaluation: | ||
| // 1. Math.random() → generates a decimal between 0 (inclusive) and 1 (exclusive) | ||
| // 2. (maximum - minimum + 1) → calculates the range size: (100 - 1 + 1) = 100 | ||
| // 3. Math.random() * 100 → gives a decimal between 0 and 99.999... | ||
| // 4. Math.floor(...) → rounds down to get integers from 0 to 99 | ||
| // 5. + minimum → shifts the range from 0-99 to 1-100 | ||
|
|
||
| // So num represents a random integer from 1 to 100 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| 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 don't want the computer to run these 2 lines - how can we solve this problem? | ||
| By adding // at the beginning of each line to turn the text into comments. |
| 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,6 @@ | ||
| // Currently trying to print the string "I was born in Bolton" but it isn't working... | ||
| // what's the error ? | ||
| //The variable cityOfBirth is being used before it's declared. To fix the error, we need to declare the variable before using it in the console.log statement. | ||
|
|
||
| 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,11 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| const last4Digits = cardNumber.toString().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 | ||
| // PREDICTION: The code won't work because cardNumber is a number, but .slice() only works on strings and arrays | ||
| // ERROR: TypeError: cardNumber.slice is not a function | ||
| // SOLUTION: Convert the number to a string first using .toString() method | ||
| console.log(last4Digits); // Should output: "4213" | ||
| //The issue is that cardNumber is a number, but the .slice() method only works on strings (and arrays). | ||
| // Convert the number to a string first, then use .slice() |
| 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"; | ||
| // Variable names cannot start with a number | ||
| const twelveHourClockTime = "20:53"; | ||
| const twentyFourHourClockTime = "08:53"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,11 +12,29 @@ 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 | ||
| // 5 function calls | ||
| //Line 4: carPrice.replaceAll(",", "") | ||
| //Line 4: Number(...) – wraps the above | ||
| //Line 5: priceAfterOneYear.replaceAll(",", "") ✅ But this line has a syntax error (we’ll get to it) | ||
| //Line 5: Number(...) – wraps the above | ||
| //Line 10: 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? | ||
| //syntax error! On line 5, there's a missing comma in the replaceAll method. | ||
|
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. In the function call 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. arguments |
||
| //correct code should be | ||
| //priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
|
|
||
| // c) Identify all the lines that are variable reassignment statements | ||
| carPrice = Number(carPrice.replaceAll(",", "")); | ||
| priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
|
|
||
| // d) Identify all the lines that are variable declarations | ||
| let carPrice = "10,000"; | ||
| let priceAfterOneYear = "8,543"; | ||
|
|
||
| // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
| // This expression converts a string with commas into a number that can be used for mathematical calculations: | ||
| // 1. carPrice.replaceAll(",", "") - removes all commas from the string "10,000" → "10000" | ||
| // 2. Number(...) - converts the cleaned string "10000" into the number 10000 | ||
| // Purpose: Strings with commas cannot be used in math operations, so we need to clean and convert them to numbers first | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,8 @@ console.log(`£${pounds}.${pence}`); | |
|
|
||
| // To begin, we can start with | ||
| // 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
| // 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): removes the trailing 'p' from the string to isolate the numeric part | ||
| // 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the numeric string with leading zeros to ensure it is at least 3 digits long | ||
| // 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the pounds portion from the padded string | ||
| // 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the pence portion and ensures it is 2 digits long | ||
|
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 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. No, we don't need .padEnd(2, "0") in this script. —Since padStart(3, "0") ensures the string is minimum 3 characters, and we're extracting the last 2, we'll always get exactly 2 characters. The .padEnd(2, "0") is redundant. |
||
| // 6. console.log(`£${pounds}.${pence}`): outputs the final formatted string representing the price in pounds | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,11 @@ In the Chrome console, | |
| invoke the function `alert` with an input string of `"Hello world!"`; | ||
|
|
||
| What effect does calling the `alert` function have? | ||
| ->It creat a pop up with the word "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`. | ||
|
|
||
| What effect does calling the `prompt` function have? | ||
| What is the return value of `prompt`? | ||
| -> It displays a dialog box in the browser asking the user to enter some input. | ||
| The user can type a response and click "OK" or click "Cancel". | ||
|
Comment on lines
+20
to
+21
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 value does 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. The prompt() function returns different values depending on what the user does: |
||
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?
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.
@cjyuan increment