Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
How can I return the average customer balance with the dollar sign removed?

I'm trying to utilize this code to return the average customer balance without the dollar sign or comma. I'm using .reduce because I feel like that's the best option here. Additionally, I'm trying out parseFloat for the first time so I'm just not sure I'm using it correctly. The objective here is

  1. convert string to number and replace dollar sign and comma with nothing
  2. sum all the balances
  3. return the average balance
  4. use .reduce
var people = [
  {
    name: "Courtney",
    age: 43, 
    balance: "$3,400"
  },
  {
    name: "Regina",
    age: 53,
    balance: "$4,000"
  },
  {
    name: "Jay",
    age: 28,
    balance: "$3,000"
  },
]

var averageBalance = function(array){
   var average = people.reduce(function(acc, current, index, array){
     if(index === array.length - 1){
       return acc / array.length; 
     }
     return acc += parseFloat(current.balance.replace(/\$|,/g, ""));
   }, 0); 
 return average;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're trying to do too much inside your .reduce body. .reduce lets you do something to every element of an array - such as add them all up. If you want to do one thing separately (like divide by the length of the array to get the average), that should be put outside the .reduce callback.

var people = [{
    name: "Courtney",
    age: 43,
    balance: "$3,400"
  },
  {
    name: "Regina",
    age: 53,
    balance: "$4,000"
  },
  {
    name: "Jay",
    age: 28,
    balance: "$3,000"
  },
]

var averageBalance = function(array) {
  const sum = people.reduce(function(acc, current) {
    return acc + Number(current.balance.replace(/\$|,/g, ""));
  }, 0);
  return sum / people.length;
}
console.log(averageBalance(people));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda