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

  • SEARCH
  • Home
  • 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 8771025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:41:02+00:00 2026-06-13T17:41:02+00:00

I am working through Eloquent Javascript. The function count takes an array and a

  • 0

I am working through Eloquent Javascript. The function count takes an array and a test function (equals(x)) as arguments, and returns the amount of elements in the array for which the test function returned true.

I understand the broad way that these functions are working, and that logically the total argument to the anonymous function passed to reduce has a value of zero.

Can someone help me to see where the value for total is coming from specifically though? I want to have a clearer picture in my mind.

function count(test, array) {
  return reduce(function(total, element) { // Where is the value for total coming from?
    return total + (test(element) ? 1 : 0);
  }, 0, array);
}

function equals(x) {
  return function(element) {return x === element;};
}

function countZeroes(array) {
  return count(equals(0), array);
}

Reduce Function from earlier:

function reduce(combine, base, array) {
  forEach(array, function (element) {
    base = combine(base, element);
  });
  return base;
}

forEach Function from earlier:

function forEach(array, action) {
  for (var i = 0; i < array.length; i++)
    action(array[i]);
}
  • 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-06-13T17:41:03+00:00Added an answer on June 13, 2026 at 5:41 pm

    The 3 arguments you passed to reduce were:

    {
        combine:function(total, element){...},
        base:0,
        array:array
    }
    

    The function then takes base and passes it to the combine function as the total argument:

    base = combine(base, element);
    

    Basically, what is happening here is that for each element in the array you just passed (as the 3rd argument array) the function takes the argument base and increments it using the anonymous function you have provided (which first checks if the element passes test). Finally, after it has iterated over all the elements, it returns the final value of base.

    Perhaps this will help explain:

    function count(test, testarray) {
      var anon = function(total, element) { // Where is the value for total coming from?
        return total + (test(element) ? 1 : 0);
      };
      //now anon is a function.
      return reduce(anon, 0, testarray);
    }
    

    Let us look at the function invocation and definition closely:

    return   reduce(anon   , 0   , testarray);
                      |      |     |
                      v      v     v
    function reduce(combine, base, array) {
        combine;    //the function that is passed in as the first argument
        base;       //the number that is passed in as the second argument
        array;      //the array that is passed in as the third argument
    

    The values of each of anon,0, and testarray, get passed into the function. Within the function, their values can be accessed by the parameter names in the function definition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working my way through Eloquent Javascript and I came across a code
I'm working my way through the Eloquent JavaScript Book and in it there is
I've been working through the javascript remote debug tutorials for WebStorm / IntelliJ(numbers.js) I've
I am working through a problem which I was able to solve, all but
So I'm working through the code in this tutorial which has a small button
Working through Learning Python came across factory function. This textbook example works: def maker(N):
I'm working through an algorithm problem set which poses the following question: Determine if
I'm working through this build-a-JavaScript-framework tutorial . Why in the following code is $super
Still working through JQuery to get data and repopulate portions of a page. Right
In working through the issues with another question, I've found text files with embedded

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.