Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!