Skip to content

Commit d7fcfa2

Browse files
committed
add DOM array methods
1 parent c848c9c commit d7fcfa2

File tree

5 files changed

+199
-1
lines changed

5 files changed

+199
-1
lines changed

62-exchange rate calculator/style.css

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ body {
1616
align-items: center;
1717
justify-content: center;
1818
height: 100vh;
19-
overflow: hidden;
2019
margin: 0;
2120
padding: 20px;
2221
}
@@ -104,4 +103,7 @@ button:focus {
104103
.currency input {
105104
width: 200px;
106105
}
106+
.money-img {
107+
width: 75px;
108+
}
107109
}

63-DOM array methods/index.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<link rel="stylesheet" href="style.css" />
7+
<title>DOM Array Methods</title>
8+
</head>
9+
<body>
10+
<h1>DOM Array Methods</h1>
11+
<div class="container">
12+
<aside>
13+
<button id="add-user">Add User 👱‍♂️</button>
14+
<button id="double">Double Money 💰</button>
15+
<button id="show-millionaires">Show Only Millionaires 💵</button>
16+
<button id="sort">Sort by Richest ↓</button>
17+
<button id="calculate-wealth">Calculate entire Wealth 🧮</button>
18+
</aside>
19+
20+
<main id="main">
21+
<h2><strong>Person</strong> Wealth</h2>
22+
</main>
23+
</div>
24+
<script src="script.js"></script>
25+
</body>
26+
</html>

63-DOM array methods/script.js

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

63-DOM array methods/style.css

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap");
2+
3+
* {
4+
box-sizing: border-box;
5+
}
6+
7+
body {
8+
background-color: #eef1f8;
9+
color: #807e87;
10+
font-family: "Roboto", sans-serif;
11+
display: flex;
12+
flex-direction: column;
13+
align-items: center;
14+
justify-content: center;
15+
height: 100vh;
16+
overflow: hidden;
17+
margin: 0;
18+
}
19+
20+
.container {
21+
display: flex;
22+
padding: 20px;
23+
margin: 0 auto;
24+
max-width: 100%;
25+
width: 800px;
26+
}
27+
28+
aside {
29+
padding: 10px 20px;
30+
width: 250px;
31+
border-right: 1px solid #bac6e1;
32+
}
33+
34+
button {
35+
cursor: pointer;
36+
background-color: #fff;
37+
color: #46464a;
38+
border: solid 1px #bac6e1;
39+
border-radius: 5px;
40+
display: block;
41+
width: 100%;
42+
padding: 10px;
43+
margin-bottom: 20px;
44+
font-weight: bold;
45+
font-size: 14px;
46+
}
47+
48+
main {
49+
flex: 1;
50+
padding: 10px 20px;
51+
}
52+
53+
h1 {
54+
color: #46464a;
55+
}
56+
57+
h2 {
58+
border-bottom: 1px solid #bac6e1;
59+
color: #9a9da4;
60+
padding-bottom: 10px;
61+
display: flex;
62+
justify-content: space-between;
63+
font-weight: 300;
64+
margin: 0 0 20px;
65+
}
66+
67+
h3 {
68+
background-color: #fff;
69+
color: #9a9da4;
70+
border-bottom: 1px solid #bac6e1;
71+
padding: 10px;
72+
display: flex;
73+
justify-content: space-between;
74+
font-weight: 300;
75+
margin: 20px 0 0;
76+
}
77+
78+
.person {
79+
display: flex;
80+
justify-content: space-between;
81+
font-size: 20px;
82+
margin-bottom: 10px;
83+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
| 60 | [Movie Seat Booking](https://github.com/solygambas/html-css-fifty-projects/tree/master/60-movie%20seat%20booking) | [Live Demo](https://codepen.io/solygambas/full/xxRQEOy) |
6969
| 61 | [Custom Video Player](https://github.com/solygambas/html-css-fifty-projects/tree/master/61-custom%20video%20player) | [Live Demo](https://codepen.io/solygambas/full/mdOQadY) |
7070
| 62 | [Exchange Rate Calculator](https://github.com/solygambas/html-css-fifty-projects/tree/master/62-exchange%20rate%20calculator) | [Live Demo](https://codepen.io/solygambas/full/abBPJBG) |
71+
| 63 | [DOM Array Methods](https://github.com/solygambas/html-css-fifty-projects/tree/master/63-DOM%20array%20methods) | [Live Demo](https://codepen.io/solygambas/full/NWbeXYR) |
7172

7273
Mainly based on 2 courses by Brad Traversy (2020):
7374

0 commit comments

Comments
 (0)