forked from solygambas/html-css-javascript-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
86 lines (75 loc) · 2.32 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const main = document.getElementById("main");
const addUserButton = document.getElementById("add-user");
const doubleButton = document.getElementById("double");
const showMillionairesButton = document.getElementById("show-millionaires");
const sortButton = document.getElementById("sort");
const calculateWealthButton = document.getElementById("calculate-wealth");
let data = [];
async function getRandomUser() {
const res = await fetch("https://randomuser.me/api");
const data = await res.json();
const user = data.results[0];
const newUser = {
name: `${user.name.first} ${user.name.last}`,
money: Math.floor(Math.random() * 1000000),
};
addData(newUser);
}
function addData(user) {
data.push(user);
updateDOM();
}
// forEach()
function updateDOM(providedData = data) {
main.innerHTML = "<h2><strong>Person</strong> Wealth</h2>";
providedData.forEach((person) => {
const element = document.createElement("div");
element.classList.add("person");
element.innerHTML = `<strong>${person.name}</strong> ${formatMoney(
person.money
)}`;
main.appendChild(element);
});
}
// Format number as money - https://stackoverflow.com/questions/149055/how-to-format-numbers-as-currency-string
function formatMoney(number) {
return "$" + number.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, "$&,");
}
// map()
function doubleMoney() {
data = data.map((user) => {
return { ...user, money: user.money * 2 };
});
updateDOM();
}
// sort()
function sortByRichest() {
data.sort((a, b) => b.money - a.money);
updateDOM();
}
// filter()
function showMillionaires() {
data = data.filter((user) => user.money > 1000000);
updateDOM();
}
// reduce()
function calculateWealth() {
const wealth = data.reduce(
(accumulator, user) => (accumulator += user.money),
0
);
const wealthElement = document.createElement("div");
wealthElement.innerHTML = `<h3>Total wealth: <strong>${formatMoney(
wealth
)}</strong></h3>`;
main.appendChild(wealthElement);
}
addUserButton.addEventListener("click", getRandomUser);
doubleButton.addEventListener("click", doubleMoney);
sortButton.addEventListener("click", sortByRichest);
showMillionairesButton.addEventListener("click", showMillionaires);
calculateWealthButton.addEventListener("click", calculateWealth);
// Init
getRandomUser();
getRandomUser();
getRandomUser();