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 8852719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:28:18+00:00 2026-06-14T13:28:18+00:00

In Eloquent Javascript , the author asks the reader to write a function countZeroes

  • 0

In Eloquent Javascript, the author asks the reader to write a function countZeroes, which takes an array of numbers as its argument and returns the amount of zeroes that occur in it as another example for the use of the reduce function.

I know

  • that the concept of the reduce function is to take an array and turn it to a single value.
  • what the ternary operator is doing which is the essential portion of the function.

I don’t know

  • where the arguments for the counter function are coming from.

From the book:

function countZeroes(array) {
  function counter(total, element) { // Where are the parameter values coming from?
    return total + (element === 0 ? 1 : 0);
  }
  return reduce(counter, 0, array);
}

Earlier example from the text:

function reduce(combine, base, array) {
 forEach(array, function (element) {
    base = combine(base, element);
  });
  return base;
}
  • 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-14T13:28:19+00:00Added an answer on June 14, 2026 at 1:28 pm

    Looking at the code, there is only one possible answer: Since you the function counter is only referenced once when passed to reduce(), reduce must thus provide the arguments to the function.

    How reduce works

    Here is a visualization of how reduce works. You need to read the diagrom from top to bottom, / and \ denote which parameters (below) are passed to the counter() function above.

                       return value of reduce()
                       /
                     etc ...
                    /
                counter
               /       \
           counter      xs[2]
          /       \
      counter      xs[1]
     /       \
    0         xs[0]
    

    counter() is initially provided with 0 (after all, the initial total amount of seen zeroes when you haven’t processed any elements yet is just zero) and the first element.

    That 0 is increased by one if the first element of the array is a zero. The new total after having seen the first element is then returned by counter(0, xs[0]) to the reduce() function. As long as elements are left, it then passes this value as new pending total to the counter() function, along with the next element from your array: xs[1].

    This process is repeated as long as there are elements in the array.

    Mapping this concept to the code

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

    As can be seen in the illustration, 0 is passed through base to the function initially, along with element, which denotes xs[0] on the first “iteration” within the forEach construct. The resulting value is then written back to base.

    As you can see in the visualization, 0 is the left parameter to the function counter(), whose result is then passed as left parameter to counter(). Since base/total acts as the left paramter within the forEach construct, it makes sense to write that value back to base/total, so the previous result will be passed to counter()/combine() again upon the next iteration. The path of /s is the “flow” of base/total.

    Where element comes from

    From chapter 6 of Eloquent JavaScript, here is the implementation of forEach() (In the comment, I have substituted the call to action with the eventual values.)

    function forEach(array, action) {
      for (var i = 0; i < array.length; i++)
        action(array[i]); // <-- READ AS: base = counter(base, array[i]);
    }
    

    action denotes a function, that is called within a simple for loop for every element, while iterating over array.

    In your case, this is passed as action to forEach():

    function (element) {
        base = combine(base, element);
    }
    

    It is the anonymous function defined upon each call to reduce(). So element is “really” array[i] once for each iteration of the for.

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

Sidebar

Related Questions

I am working through Eloquent Javascript. The function count takes an array and a
Here is a JavaScript example from Eloquent JavaScript : function findSequence(goal) { function find(start,
I have this function (going trough the Eloquent Javascript Tutorial chapter 3): function absolute(number)
So I'm looking at some code from Eloquent Javascript and it says that to
I am working my way through Eloquent Javascript and I came across a code
The following snippet of code is taken from Eloquent JavaScript. var noCatsAtAll = {};
If a function requires an incoming array to have a specific key/index, is there
While going through Eloquent Javascript (Chapter 6) there is a reference to higher-order functions
While reading Eloquent Javascript (Chapter 6) I am trying to grasp fundamental concepts. There
I'm studying Javascript using Marijn Haverbeke's book Eloquent JavaScript and didn't understand the following

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.