-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalance.js
executable file
·39 lines (36 loc) · 1 KB
/
balance.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
const users = [
{
name: "Salvio",
income: [115.3, 48.7, 98.3, 14.5],
outgoing: [85.3, 13.5, 19.9]
},
{
name: "Marcio",
income: [24.6, 214.3, 45.3],
outgoing: [185.3, 12.1, 120.0]
},
{
name: "Lucia",
income: [9.8, 120.3, 340.2, 45.3],
outgoing: [450.2, 29.9]
}
];
function calculateBalance(income, outgoing) {
const sumIncome = sumNumbers(income)
const sumOutgoing = sumNumbers(outgoing)
return sumIncome - sumOutgoing
}
function sumNumbers(numbers) {
let sum = 0
for (let number of numbers) {
sum = sum + number
} return sum
}
for (let user of users) {
const balance = calculateBalance(user.income, user.outgoing)
if (balance > 0) {
console.log(`${user.name} has a positive balance of ${balance.toFixed(2)}`)
} else {
console.log(`${user.name} has a negative balance of ${balance.toFixed(2)} `)
}
}