How can I make this calculation be performed in all the Objects of the array and return me the value multiplied by the length of the table?

Asked

Viewed 37 times

0

I need the calculation to be performed in all Bjects before returning the value to me by the length of the table (OBS: it’s just a challenge I’m participating, I’m not using database).

function desafio() {

  let tabela = [

    {
      preco: 10,
      qtd: 5,
      peso: 5
    },

    {
      preco: 2,
      qtd: 8,
      peso: 4
    },

    {
      preco: 25,
      qtd: 15,
      peso: 2
    }

  ]

  let clientes = [

    {
      email: "[email protected]"
    },
    {
      email: "[email protected]"
    },
    {
      email: "[email protected]"
    }

  ]

  let calculo = ((tabela[0].preco * tabela[0].qtd) * tabela[0].peso) / clientes.length

  form = Math.round(calculo)

  let total = form * tabela.length

  if (clientes.length === 0) {
    console.log("Digite o email dos clientes")
  } else {

  }

  for (let i = 0; i < clientes.length; i++) {

    console.log(`${clientes[i].email} = ${total}`)
  }
}
desafio()

  • I did not understand the question. I could clarify?

  • I need this calculation that I declared to be done on all objects in the array before returning the multiplication by the total of items within the array. When I refer to the array objects, I refer to the items that are between { } and the calculation that I want to return from each of them is represented in "Let calculus =", just below table and client. This code I sent is returning me the calculation of only 1 object, and in the code has 3, again, represented by { }, it is interpreting that it needs to do the calculation only of the first and already multiply by table.length.

  • Put that in the question.

1 answer

1

Well, I don’t quite understand your question, but I believe that’s what you want to do.

function desafio() {

  let tabela = [

    {
      preco: 10,
      qtd: 5,
      peso: 5
    },

    {
      preco: 2,
      qtd: 8,
      peso: 4
    },

    {
      preco: 25,
      qtd: 15,
      peso: 2
    }

  ]

  let clientes = [

    {
      email: "[email protected]"
    },
    {
      email: "[email protected]"
    },
    {
      email: "[email protected]"
    }

  ]

  if (clientes.length > 0) {

    clientes.forEach((element, i) => {

      let calculo = Math.round(((tabela[i].preco * tabela[i].qtd) * tabela[i].peso) / clientes.length);

      console.log(element.email + " - total: " + calculo)

    })

  } else {
    console.log('por favor digite um email')
  }



}
desafio()

  • I think you’re right, but the question is very difficult to understand.

  • Almost that, only I need the 3 Objects add up the result of the calculation and then divide equally between emails.

Browser other questions tagged

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