-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (77 loc) · 2.8 KB
/
Copy pathscript.js
File metadata and controls
82 lines (77 loc) · 2.8 KB
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
const main = document.getElementById('main');
const addUserBtn = document.getElementById('add-user');
const doubleBtn = document.getElementById('double');
const showMillonairiesBtn = document.getElementById('show-millionaires');
const sortBtn = document.getElementById('sort');
const showMale = document.getElementById('show-male');
const showFemale = document.getElementById('show-female');
const calculateWealthBtn = document.getElementById('calculate-wealth');
let data = [];
//function to fetch data from the api
async function getRandomUser(){
const response = await fetch('https://randomuser.me/api');
const data = await response.json();
const user = data.results[0];
const {first:firstName,last:lastName} = user.name;
const {gender} = user;
const newUser = {
name:`${firstName} ${lastName}`,
gender,
money:Math.floor(Math.random() * 1000000)
};
console.log(newUser);
addData(newUser);
}
//function to double the money of user
function doubleMoney(){
data = data.map(user=>{
return {...user,money:user.money * 2};
})
updateDom();
}
//function to show only millonare
//function to sort user by richest
function sortByRichest(){
data.sort((a,b)=>{
return b.money - a.money;
})
updateDom();
}
function showMillonairies(){
data = data.filter((aUser)=> aUser.money > 1000000);
updateDom();
}
//function to calculate method
function calculateWealth(){
// const total = array.reduce((acc,num) => (acc + num),0);//acc is the accumulated number and the last parameter is the starting number
const wealth = data.reduce((acc,aUser)=> (acc += aUser.money),0);
const wealthEl = document.createElement('div');
wealthEl.innerHTML = `<h3>Total wealth:<strong>${formatMoney(wealth)}</strong></h3>`;
main.appendChild(wealthEl);
}
//function to add fetched user to the data array
getRandomUser();
function addData(obj){
data.push(obj);
updateDom();
}
//function to update the list
function updateDom(providedDate = data){
main.innerHTML = '<h2><strong>Person</strong>Wealth</h2>';
providedDate.forEach((aUser)=>{
const element = document.createElement('div');
element.classList.add('person');
element.innerHTML = `<strong>${aUser.name}</strong>${formatMoney(aUser.money)}`;
main.appendChild(element);
})
}
//simple function which adds $ sign and commas to the number and give the format of monay
function formatMoney(number) {
return '$' + number.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
}
//event listners
addUserBtn.addEventListener('click',getRandomUser);
doubleBtn.addEventListener('click',doubleMoney);
sort.addEventListener('click',sortByRichest);
showMillonairiesBtn.addEventListener('click',showMillonairies);
calculateWealthBtn.addEventListener('click',calculateWealth);