How to add values of an object array[JS], with only functions and loop for?

Asked

Viewed 1,695 times

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.

  • 1

    somaNumeros(usuarios[0].receitas) - somaNumeros(usuarios[0].despesas)? Then put it inside a loop with access via "dynamic" index to make for each user.

1 answer

1

"a program that calculates the sum of user income and expenses" - my interpretation is that the intended one is an array with the sum of each user. To be so needed a .map because you want an array with the same number of entries in the same order (the sum for its respective user).

In relation to calculaSaldo and somaNumeros... starting with somaNumeros here is a .reduce classic, very simple... In relation to calculaSaldo the idea is to convert the numbers into values that can be passed to somaNumeros with the right signal (positive/negative).

A suggestion would be so:

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 calculaSaldo(utilizador) {
  const entradas = utilizador.receitas;
  // e agora juntamos as despesas com o sinal certo:
  utilizador.despesas.forEach(nr => entradas.push(nr * -1));

  return somaNumeros(entradas);
}

function somaNumeros(numeros) {
  return numeros.reduce((sum, nr) => sum + nr, 0);
}

const somas = usuarios.map(calculaSaldo);
console.log(somas);
console.log(somas.map((soma, i) => `${usuarios[i].nome}: ${soma}`).join('\n'));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.