-
-
Notifications
You must be signed in to change notification settings - Fork 195
LONDON | ITP_MAY | Mirabel Adom | Module-Structuring-and-Testing-Data | Sprint-1 #619
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
12ecc11
943fc31
a175003
18f41e7
86b2701
9370786
6761369
375e58e
0899733
f92399c
94f85ba
1f5dd67
bba94cd
3d2fa70
1095ff7
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 |
---|---|---|
|
@@ -3,7 +3,44 @@ const maximum = 100; | |
|
||
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
|
||
console.log(num); | ||
/* | ||
* Explanation of 'num': | ||
* The 'num' variable represents a random whole integer generated within the range defined by 'minimum' and 'maximum', inclusive. | ||
* | ||
* A step-by-step walkthrough of the expression: | ||
* | ||
* 1. (maximum - minimum + 1): | ||
* - Calculates the size of the desired range. For minimum=1, maximum=100, this is (100 - 1 + 1) = 100. | ||
* - The '+ 1' is crucial to make the range inclusive of both the minimum and maximum values. | ||
* | ||
* 2. Math.random(): | ||
* - Returns a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). | ||
* - E.g., 0.000... to 0.999... | ||
Comment on lines
+18
to
+19
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. We can also use the concise and precise interval notation to describe a range of values.
|
||
* | ||
* 3. Math.random() * (maximum - minimum + 1): | ||
* - Scales the random number to fit the desired range. | ||
* - E.g., if Math.random() is 0.75, then 0.75 * 100 = 75. | ||
* - This results in a floating-point number between 0 (inclusive) and the range size (exclusive). | ||
* | ||
* 4. Math.floor(...): | ||
* - Rounds the result down to the nearest whole integer. | ||
* - E.g., Math.floor(75.99) = 75. | ||
* - This ensures we get an integer, effectively mapping the 0 to (range-1) values. | ||
* | ||
* 5. ... + minimum: | ||
* - Shifts the integer to the desired starting point. | ||
* - E.g., if the result so far is 75, adding 'minimum' (1) gives 75 + 1 = 76. | ||
* - This transforms the 0 to (range-1) integer into a 'minimum' to 'maximum' integer. | ||
* | ||
* In summary, 'num' will be a random integer chosen uniformly from the set {1, 2, 3, ..., 99, 100}. | ||
*/ | ||
|
||
|
||
|
||
// 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 | ||
|
||
|
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,4 +1,4 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
let age = 33; | ||
age = age + 1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// 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 variable must be defined it's used in the console.log statement |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
const twelveHourClockTime = "08:53"; | ||
const twentyFourHourClockTime = "20:53"; | ||
|
||
// Variable names must start with a letter, underscore symbol or , but cannot start with a number. |
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,41 @@ 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 | ||
//Answer: There are 5 function calls: | ||
// - carPrice.replaceAll(...) | ||
// - Number(...) : Line 3 | ||
// - priceAfterOneYear.replaceAll(...) | ||
// - Number(...) : Line 5 | ||
// - 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? | ||
//Answer: | ||
//There was an error in line 5 because a comma was missing. | ||
// Instead of the original example in line, which was: replaceAll(", " "")). I inserted a comma in order to fix the code: replaceAll(", ", "")); | ||
Comment on lines
+24
to
+26
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. A comma is missing between "," and "" in the function call. |
||
|
||
|
||
// c) Identify all the lines that are variable reassignment statements | ||
//Answer: | ||
// Lines 4 and 5 are variable reassignments: | ||
//In Line 4 the value changes from "10,000" (a string) to 10000 (a number): carPrice = Number(carPrice.replaceAll(",", "")); | ||
//In line 5 the value changes from "8,543" (a string) to 8543 (a number): priceAfterOneYear = Number(priceAfterOneYear.replaceAll(", ", "")); | ||
|
||
|
||
// d) Identify all the lines that are variable declarations | ||
//Answer: | ||
// These variable declarations are identified by these keywords: let and const: | ||
|
||
// Line 1: let carPrice = "10,000"; | ||
|
||
// Line 2: let priceAfterOneYear = "8,543"; | ||
|
||
// Line 7: const priceDifference | ||
|
||
// Line 8: const percentageChange | ||
|
||
|
||
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
//Answer: | ||
//carPrice.replaceAll(",", ""): removed commas from the string "10,000", which became "10000" | ||
//Number(...): converted the string "10000" into a number, 10000 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,26 @@ 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); | ||
// The purpose of line 3 is to remove the trailing 'p' from the string to leave just the numeric part. | ||
// As a result, "399p" (as seen in line 1) becomes "399". | ||
|
||
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
//The purpose of line 8 is to ensure that the string is at least 3 characters long. | ||
// It adds zeros at the start if the string is shorter than 3 digits. | ||
// In this case, "399" is already 3 digits long, so nothing is added. | ||
|
||
//4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); | ||
// This takes the last two digits to get the pounds part. | ||
//In this scenario, "399", we get "3" as pounds. | ||
|
||
//5. const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); | ||
// This gets the last two digits from the string to show the pence. | ||
// If the result is only one digit, it adds a zero at the end to make it two digits. | ||
// In this case, "399" gives us "99", so nothing additional is added. | ||
Comment on lines
+42
to
+45
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}`); | ||
// This line prints out the final price in pounds and pence. | ||
// It combines the pounds and pence with a £ symbol and a dot in between. | ||
// In this example, pounds = "3" and pence = "99"; it will print "£3.99". |
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 + 1
is 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?