Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7567897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:47:04+00:00 2026-05-30T14:47:04+00:00

I am attempting to go through an array and add up all the numbers.

  • 0

I am attempting to go through an array and add up all the numbers. I used console.log to show me what values the script was using as shown below. I keep trying different variations of things in the if() but nothing seems to be working properly.

    var billycount = 0;
    var billyTotalScore = billyScoreList.reduce(function(score, total) {
        if(score === " ") {
        billycount += 1;
        }
        return +total + +score; 
    });
    console.log(billycount); //0
    console.log(billyTotalScore); //30
    console.log(billyScoreList); // ["12", " ", "18"]
    console.log(billyAverageScore) //10

    var billyAverageScore = billyTotalScore/(billyteamlist.length - billycount);

The answer to billyAverageScore should equal 15 (30/2).

I tried if(score === "0") which gives me the same answers as above and if (score !== true) which gives me a count of 2 and an average of 30. I think reduce() is treating the empty string as a 0. I want to be able to count all the empty strings so I can discount them from the length when finding the average.

I have been wrestling this forever and feel like I’m missing one key concept behind it. Any help would be great! Thanks!

UPDATE:

For anyone who stumbles across this, here is the code I got to work.

var billycount = 0;
    var billyTotalScore = billyScoreList.reduce(function(total, score) {
        if (score === " " || total === " ") {
        billycount++;   
        }
        return +total + +score; 
    });

    var billyAverageScore = billyTotalScore/(billyteamlist.length - billycount);

When I was just checking if (score === " ") I was forgetting that score will never be equal to the first term in the array. I just added || total === " ". the only time this would break down would be if the first element was ” ” and the second element was 0. I would want to billycount++ for the first element but not for the second. I’ll have to give that some more thought.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:47:06+00:00Added an answer on May 30, 2026 at 2:47 pm

    The callback function of reduce should be function(total, score) instead of function(score, total).

    see MDN:

    previousValue
        The value previously returned in the last invocation of the callback, or initialValue, if supplied. (See below.)
    currentValue
        The current element being processed in the array.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I'm attempting to access an array of length 165888. I'm looping through, calculating
running UNIX through putty, and using vi and gcc. I am attempting to read
I am attempting to use a recursive function to search through a multidimensional array,
Using PHP, I'm attempting to route email through AuthSMTP (a hosted SMTP service). The
I have a device on a network that I am attempting to ping through
I am attempting to publish a 3.5 MVC website on my build server through
Attempting to print out a list of values from 2 different variables that are
I'm having some trouble with a word add-in. I'm attempting to create a new
I am attempting to load up an entire array of NSManagedObjects into an NSArray,
I'm attempting to set up a simple Mac-based 2D tiling engine, using a 2D

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.