-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (69 loc) · 1.98 KB
/
index.js
File metadata and controls
79 lines (69 loc) · 1.98 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
/**
* @typedef Freelancer
* @property {string} name
* @property {string} occupation
* @property {number} rate
*/
// === Constants ===
const NAMES = ["Alice", "Bob", "Carol", "Dave", "Eve"];
const OCCUPATIONS = ["Writer", "Teacher", "Programmer", "Designer", "Engineer"];
const PRICE_RANGE = { min: 20, max: 200 };
const NUM_FREELANCERS = 100;
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generateFreelancer() {
const name = NAMES[getRandomInt(0, NAMES.length - 1)];
const occupation = OCCUPATIONS[getRandomInt(0, OCCUPATIONS.length - 1)];
const rate = getRandomInt(PRICE_RANGE.min, PRICE_RANGE.max);
return { name, occupation, rate };
}
function averageFreelanceRate(freelancers) {
if (freelancers.length === 0) return 0;
const totalRate = freelancers.reduce((sum, f) => sum + f.rate, 0);
return totalRate / freelancers.length;
}
function Freelancer({ name, occupation, rate }) {
return (
<div className="freelancer">
<h3>{name}</h3>
<p>Occupation: {occupation}</p>
<p>Rate: ${rate}/hr</p>
</div>
);
}
function FreelancerList({ freelancers }) {
return (
<div className="freelancer-list">
<h2>Available Freelancers</h2>
{freelancers.map((freelancer, index) => (
<Freelancer
key={index}
name={freelancer.name}
occupation={freelancer.occupation}
rate={freelancer.rate}
/>
))}
</div>
);
}
function AverageRate({ freelancers }) {
if (freelancers.length === 0) {
return <p>No freelancers available.</p>;
}
const average = averageFreelanceRate(freelancers).toFixed(2);
return <h2>Average Rate: ${average}/hr</h2>;
}
export default function App() {
const freelancers = Array.from(
{ length: NUM_FREELANCERS },
generateFreelancer
);
return (
<div>
<h1>Freelancer Dashboard</h1>
<AverageRate freelancers={freelancers} />
<FreelancerList freelancers={freelancers} />
</div>
);
}