0
That is the question:
Sum of expenditure and revenue
Create a program that calculates the sum of revenue and expenses of users and at the end returns the balance (revenue - expenses).
Use the user array below:
const usuarios = [
{
nome: "Salvio",
receitas: [115.3, 48.7, 98.3, 14.5],
despesas: [85.3, 13.5, 19.9]
},
{
nome: "Marcio",
receitas: [24.6, 214.3, 45.3],
despesas: [185.3, 12.1, 120.0]
},
{
nome: "Lucia",
receitas: [9.8, 120.3, 340.2, 45.3],
despesas: [450.2, 29.9]
}
];
Scroll through the array of users and for each user call a function called calculaSaldo
which receives as a parameter the user’s income and expenses:
function calculaSaldo(receitas, despesas) {}
Create a second function that takes as a parameter an array of numbers and returns the sum of them and use it to calculate the sum of revenues and expenses within the function calculaSaldo
:
function somaNumeros(numeros) {
// Soma todos números dentro do array "numeros"
}
The function calculaSaldo
should use the function somaNumeros
to calculate the sum of revenues and expenses and in the end return the balance of the user, ie receitas - despesas
.
Researching, I saw that the best way to do this is with reduce. However, the challenge is to make using Funções
and Loops
.
I’ve been trying to fix it for two days, but I couldn’t. I was only able to do a function that calculates the total value of the array.
const usuarios = [
{
nome: "Salvio",
receitas: [115.3, 48.7, 98.3, 14.5],
despesas: [85.3, 13.5, 19.9]
},
{
nome: "Marcio",
receitas: [24.6, 214.3, 45.3],
despesas: [185.3, 12.1, 120.0]
},
{
nome: "Lucia",
receitas: [9.8, 120.3, 340.2, 45.3],
despesas: [450.2, 29.9]
}
];
function somaNumeros(numeros) {
let soma = 0;
for(let i = 0 ; i < numeros.length ; i++) {
soma += numeros[i]
} return soma
}
console.log(somaNumeros(usuarios[0].receitas))
But I couldn’t move on to the other steps.
I’d appreciate it if someone would shed some light.
somaNumeros(usuarios[0].receitas) - somaNumeros(usuarios[0].despesas)
? Then put it inside a loop with access via "dynamic" index to make for each user.– Luiz Felipe