|
1 | 1 | // generate fake data |
2 | | -const options = [] |
3 | | -let p = 5 // price |
4 | | -let q = 120 // quantity |
5 | | -const e = 200 // expenses |
| 2 | +const options = []; |
| 3 | +let p = 5; // price |
| 4 | +let q = 120; // quantity |
| 5 | +const e = 200; // expenses |
6 | 6 | while (p >= 1) { |
7 | 7 | options.push({ |
8 | 8 | price: p, |
9 | 9 | quantity: q, |
10 | | - profit: (p * q) - e |
11 | | - }) |
12 | | - p -= 0.5 |
13 | | - q += 26 |
| 10 | + profit: p * q - e |
| 11 | + }); |
| 12 | + p -= 0.5; |
| 13 | + q += 26; |
14 | 14 | } |
15 | 15 |
|
16 | 16 | // render fake data |
17 | | -const tbody = document.querySelector('tbody') |
| 17 | +const tbody = document.querySelector("tbody"); |
18 | 18 | options.forEach(option => { |
19 | | - const row = document.createElement('tr') |
20 | | - const values = Object.values(option) |
| 19 | + const row = document.createElement("tr"); |
| 20 | + const values = Object.values(option); |
21 | 21 | for (const value of values) { |
22 | | - const cell = document.createElement('td') |
23 | | - const cellContent = document.createTextNode(`${value}`) |
24 | | - cell.appendChild(cellContent) |
25 | | - row.appendChild(cell) |
| 22 | + const cell = document.createElement("td"); |
| 23 | + const cellContent = document.createTextNode(`${value}`); |
| 24 | + cell.appendChild(cellContent); |
| 25 | + row.appendChild(cell); |
26 | 26 | } |
27 | | - tbody.appendChild(row) |
28 | | -}) |
| 27 | + tbody.appendChild(row); |
| 28 | +}); |
29 | 29 |
|
30 | 30 | // find most profitable option |
31 | 31 | // po = profitableOption |
32 | 32 | // co = currentOption |
33 | | -const reducer = (po, co) => co.profit > po.profit ? co : po |
34 | | -const profitable = options.reduce(reducer) |
| 33 | +const reducer = (po, co) => (co.profit > po.profit ? co : po); |
| 34 | +const profitable = options.reduce(reducer); |
35 | 35 |
|
36 | 36 | // render most profitable option |
37 | | -const tblBestOption = document.querySelector('#best-option') |
38 | | -const entries = Object.entries(profitable) |
| 37 | +const tblBestOption = document.querySelector("#best-option"); |
| 38 | +const entries = Object.entries(profitable); |
39 | 39 | for (const [key, value] of entries) { |
40 | 40 | // row |
41 | | - const row = document.createElement('tr') |
| 41 | + const row = document.createElement("tr"); |
42 | 42 | // header cell |
43 | | - const headerCell = document.createElement('th') |
44 | | - const headerCellContent = document.createTextNode(`${key}`) |
45 | | - headerCell.appendChild(headerCellContent) |
46 | | - row.appendChild(headerCell) |
| 43 | + const headerCell = document.createElement("th"); |
| 44 | + const headerCellContent = document.createTextNode(`${key}`); |
| 45 | + headerCell.appendChild(headerCellContent); |
| 46 | + row.appendChild(headerCell); |
47 | 47 | // standard cell |
48 | | - const standardCell = document.createElement('td') |
49 | | - const standardCellContent = document.createTextNode(`${value}`) |
50 | | - standardCell.appendChild(standardCellContent) |
51 | | - row.appendChild(standardCell) |
| 48 | + const standardCell = document.createElement("td"); |
| 49 | + const standardCellContent = document.createTextNode(`${value}`); |
| 50 | + standardCell.appendChild(standardCellContent); |
| 51 | + row.appendChild(standardCell); |
52 | 52 | // insert row into the table |
53 | | - tblBestOption.appendChild(row) |
| 53 | + tblBestOption.appendChild(row); |
54 | 54 | } |
0 commit comments