-
Notifications
You must be signed in to change notification settings - Fork 0
/
freelancer.js
65 lines (56 loc) · 2.01 KB
/
freelancer.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
const initialFreelancers = [
{ name: "Alice", price: 30, occupation: "Writer" },
{ name: "Bob", price: 50, occupation: "Teacher" },
{ name: "Carol", price: 70, occupation: "Programmer" },
];
const additionalFreelancers = [
{ name: "Dr. Slice", price: 25, occupation: "Gardener" },
{ name: "Dr. Pressure", price: 51, occupation: "Programmer" },
{ name: "Prof. Possibility", price: 43, occupation: "Teacher" },
{ name: "Prof. Prism", price: 81, occupation: "Teacher" },
{ name: "Dr. Impulse", price: 43, occupation: "Teacher" },
{ name: "Prof. Spark", price: 76, occupation: "Programmer" },
{ name: "Dr. Wire", price: 47, occupation: "Teacher" },
{ name: "Prof. Goose", price: 72, occupation: "Driver" },
];
let freelancers = initialFreelancers.slice();
let currentIndex = 0;
function calculateAveragePrice() {
const total = freelancers.reduce(
(sum, freelancer) => sum + freelancer.price,
0
);
const average = total / freelancers.length;
return average.toFixed(2);
}
function updateAveragePrice() {
const avgPriceElement = document.querySelector("#avg-price");
avgPriceElement.textContent = calculateAveragePrice();
}
function renderFreelancers() {
const freelancersList = document.querySelector("#freelancers-list");
freelancersList.innerHTML = "";
freelancers.forEach((freelancer) => {
const div = document.createElement("div");
div.className = "freelancer";
div.innerHTML = `<strong>${freelancer.name}</strong> - ${freelancer.occupation}, starting price: $${freelancer.price}`;
freelancersList.appendChild(div);
});
}
function generateRandomFreelancer() {
const randomIndex = Math.floor(Math.random() * additionalFreelancers.length);
return additionalFreelancers[randomIndex];
}
function updateFreelancers() {
for (let i = 0; i < freelancers.length; i++) {
freelancers[i] = generateRandomFreelancer();
}
renderFreelancers();
updateAveragePrice();
}
function init() {
renderFreelancers();
updateAveragePrice();
setInterval(updateFreelancers, 3000);
}
init();