Skip to content

Commit 3548ebc

Browse files
committed
Address remaining review feedback on random and percentage-change
1 parent b03e74c commit 3548ebc

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎Sprint-2/1-key-exercises/4-random.js‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,34 @@ console.log(num);
88
// Try breaking down the expression and using documentation to explain what it means
99
// It will help to think about the order in which expressions are evaluated
1010
// Try logging the value of num and running the program several times to build an idea of what the program is doing
11-
// num is a random whole number between 1 and 100
11+
<<<<<<< Updated upstream
12+
// num is a random whole number between 1 and 100
13+
=======
14+
15+
// Breakdown of: Math.floor(Math.random() * (maximum - minimum + 1)) + minimum
16+
//
17+
// 1) (maximum - minimum + 1)
18+
// → 100 - 1 + 1 = 100
19+
// This is the count of whole numbers from minimum to maximum.
20+
//
21+
// 2) Math.random()
22+
// → a decimal number greater than or equal to 0 and less than 1
23+
// Example: 0.374
24+
//
25+
// 3) Math.random() * (maximum - minimum + 1)
26+
// → multiplies that decimal by 100
27+
// → a number from 0 up to 100
28+
// Example: 0.374 * 100 = 37.4
29+
//
30+
// 4) Math.floor(...)
31+
// → rounds down to the nearest whole number
32+
// → gives 0, 1, 2, ... 99
33+
// Example: Math.floor(37.4) = 37
34+
//
35+
// 5) + minimum
36+
// → adds 1, so the range shifts from 0–99 to 1–100
37+
// Example: 37 + 1 = 38
38+
//
39+
// Final meaning:
40+
// num is a random whole number between 1 and 100.
41+
>>>>>>> Stashed changes

‎Sprint-2/3-mandatory-interpret/1-percentage-change.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ const priceDifference = carPrice - priceAfterOneYear;
88
const percentageChange = (priceDifference / carPrice) * 100;
99

1010
console.log(`The price difference is ${priceDifference}`);
11+
<<<<<<< Updated upstream
1112

1213

14+
=======
15+
>>>>>>> Stashed changes
1316
console.log(`The percentage change is ${percentageChange}%`);
1417

1518
// Read the code and then answer the questions below
1619

1720
// a) How many function calls are there in this file? Write down all the lines where a function call is made
21+
<<<<<<< Updated upstream
1822
// Answer: 6 function calls.
1923

2024
// 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?
@@ -35,3 +39,32 @@ console.log(`The percentage change is ${percentageChange}%`);
3539
// Answer: carPrice.replaceAll(",","") remove commas from the string.
3640
// Number(...) changes the text to a number so subtraction and division work correctly.
3741

42+
=======
43+
// Answer: 6 function calls:
44+
// - Line 4: replaceAll(",", "") and Number(...)
45+
// - Line 5: replaceAll(",", "") and Number(...)
46+
// - Line 10: console.log(...)
47+
// - Line 11: console.log(...)
48+
49+
// 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?
50+
// Answer: The error was on the priceAfterOneYear.replaceAll line.
51+
// It was written as replaceAll("," "") which is invalid syntax (missing a comma between the two arguments).
52+
// Fixed by writing replaceAll(",", "") so the arguments are the search string "," and the replacement "".
53+
54+
// c) Identify all the lines that are variable reassignment statements
55+
// Answer:
56+
// - carPrice = Number(carPrice.replaceAll(",", ""));
57+
// - priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
58+
59+
// d) Identify all the lines that are variable declarations
60+
// Answer:
61+
// - let carPrice = "10,000";
62+
// - let priceAfterOneYear = "8,543";
63+
// - const priceDifference = carPrice - priceAfterOneYear;
64+
// - const percentageChange = (priceDifference / carPrice) * 100;
65+
66+
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
67+
// Answer:
68+
// carPrice.replaceAll(",", "") removes all commas from the string (e.g. "10,000" becomes "10000").
69+
// Number(...) converts that string into a number (10000) so we can do subtraction and division.
70+
>>>>>>> Stashed changes

0 commit comments

Comments
 (0)