Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda