-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
130 lines (119 loc) · 3.28 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const Url = "https://randomuser.me/api";
const aside = document.querySelector(".aside");
const persons = document.querySelector(".persons");
const add = document.getElementById("add");
const double = document.getElementById("double");
const show = document.getElementById("show");
const sort = document.getElementById("sort");
const result = document.getElementById("result");
let data = [];
///Functionallity
async function getUser(URL) {
const {
results: [
{
name: { first, last, title },
},
],
} = await fetch(URL).then((data) => data.json());
displayUser(first + " " + last + " " + title, true);
}
//////////
function displayUser(name, add = false, num) {
const div = document.createElement("div");
let rndNumber = rndNum();
div.classList.add("person");
div.innerHTML = `
<h4>${name}</h4>
<span>${num ? num : rndNumber} </span>
`;
if (add) {
data.push({ name, number: rndNumber });
}
persons.appendChild(div);
}
function rndNum() {
let rndNumber = Math.round(Math.random() * 1_000_000);
return format(rndNumber);
}
function format(nmb) {
return nmb.toLocaleString("en-US", {
style: "currency",
currency: "USD",
});
}
///Event Listener
add.addEventListener("click", () => {
getUser(Url);
removeH2();
});
show.addEventListener("click", () => {
let milionaire = data.filter((num) => formatToNumber(num.number) > 1_000_000);
persons.innerHTML = "";
milionaire.forEach((person) =>
displayUser(person.name, false, person.number)
);
removeH2();
if (!milionaire.length) {
persons.innerHTML = "";
let h2 = document.createElement("h2");
h2.innerText = "No Millionaire Availaible";
persons.appendChild(h2);
setTimeout(() => {
persons.removeChild(h2);
data.forEach((person) => displayUser(person.name, false, person.number));
}, 2000);
}
console.log(milionaire);
});
sort.addEventListener("click", () => {
// data = data.sort((a, b) => b.number - a.number);
removeH2();
data = data.sort((a, b) => {
if (a.number > b.number) {
return -1;
} else if (formatToNumber(a.number) < formatToNumber(b.number)) {
return 1;
} else {
return 0;
}
});
console.log(data);
persons.innerHTML = "";
data.forEach((person) => displayUser(person.name, false, person.number));
});
double.addEventListener("click", () => {
const numberHtml = document.querySelectorAll(".person span");
numberHtml.forEach((numHtml, i) => {
let numb = formatToNumber(data[i].number);
data[i].number = format(numb * 2);
numHtml.innerText = `${data[i].number}`;
});
});
function formatToNumber(num) {
return +num.replace(/[",",$]/gi, "");
}
result.addEventListener("click", () => {
let some = data.reduce((values, acc) => {
console.log(acc);
values += formatToNumber(acc.number);
return values;
}, 0);
let h2 = document.createElement("h2");
h2.innerHTML = `Result <strong>${format(some)} </strong>`;
persons.innerHTML = "";
data.forEach((person) => displayUser(person.name, false, person.number));
persons.appendChild(h2);
});
///Callls
getUser(Url);
function removeH2() {
let h2 = document.querySelector("h2");
let h3 = document.querySelector("h3");
if (h2) {
persons.removeChild(h2);
}
if (h3) {
persons.removeChild(h3);
}
}