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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:33:37+00:00 2026-06-05T20:33:37+00:00

I’m learning javascript. I know we can pass a function to other functions after

  • 0

I’m learning javascript. I know we can pass a function to other functions after the function is defined. But I need help on understanding this example:

function map(func, array) {
  var result = [];
  forEach(array, function (element) {
    result.push(func(element));
  });
  return result;
}

From what I can understand, func is an argument of map. I need to provide a function as func. But in the tutorial I’m reading, it doesn’t mention where this func come from, seems no need to specify this argument? Another example in the tutorial is the same:

 function count(test, array) {
  return reduce(function(total, element) {
    return total + (test(element) ? 1 : 0);
  }, 0, array);
}

This test function is equal to element === 0 ? 1 : 0 , but the tutorial doesn’t say I need to write down the test function. Do I need to write this test function?

  • 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-05T20:33:39+00:00Added an answer on June 5, 2026 at 8:33 pm

    EDIT: In the link to the tutorial you posted, the function passed is a pre-defined function Math.round. My example below shows creating your own function to pass.


    The map example shows the implementation. You’d provide the function (and the Array) when you call map.

    From the looks of it, the map pass the current item in the Array to your function, and your function should do something with it and return the result. The results your function returns are added to the new Array.

    var arr = [1,2,3,4,5];
    var my_func = function(item) { return item * 2; };
    
    var new_arr = map(my_func, arr);
    
    console.log(new_arr); // [2,4,6,8,10]
    
    1. we created an Array (arr),

    2. we created a function (my_func), which takes whatever it’s given, and multiplies it by 2.

    3. we passed both to map

    4. the map function iterates our arr, passing the current item in each iteration to our function.

    5. our function takes the current item, and returns the result of multiplying it by 2.

    6. the map function takes that result, and adds it to the new Array.

    7. when the iteration is done, the map function returns the new Array.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

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.