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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:45:15+00:00 2026-06-17T07:45:15+00:00

I am trying to learn JS better. I have the below code which I

  • 0

I am trying to learn JS better.

I have the below code which I am trying to put into underscore.js but am failing.

I’m hoping you can point out where I’m going wrong.

I’m trying to take a loop that I know works and then use underscores capabilites to refine it. The top test shows the loops, the second test is my attempt to do the same with underscore.js. I’m failing miserably!

Thanks

 products = [
       { name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false },
       { name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false },
       { name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false },
       { name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true },
       { name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true }
    ];


it("should count the ingredient occurrence (imperative)", function () {
    var ingredientCount = { "{ingredient name}": 0 };

    for (i = 0; i < products.length; i+=1) {
        for (j = 0; j < products[i].ingredients.length; j+=1) {
            ingredientCount[products[i].ingredients[j]] = (ingredientCount[products[i].ingredients[j]] || 0) + 1;
        }
    }

    expect(ingredientCount['mushrooms']).toBe(2);
  });

  it("should count the ingredient occurrence (functional)", function () {
    var ingredientCount = { "{ingredient name}": 0 };


    var ffd = _(products).chain()
              .map(function(x){return x.ingredients;})
              .flatten()
              .reduce(function(memo,x){
                if (x===memo) 
                  {
                    return ingredientCount[memo] = ingredientCount[memo]+1;
                  }
                  else
                  {
                    return ingredientCount[memo] = 0;
                  }

                  })
              .value();

        /* chain() together map(), flatten() and reduce() */

    expect(ingredientCount['mushrooms']).toBe(2);
  });
  • 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-17T07:45:16+00:00Added an answer on June 17, 2026 at 7:45 am

    Your reduce is not very functional. Have a look at its documentation! The “memo”, also know as “accumulator”, is the value returned from the previous iteration – that should be your ingredientCount map.

    var ingredientCount = _.chain(products)
      .pluck("ingredients") // don't use map for that
      .flatten()
      .reduce(function (memo, item) {
           memo[item] = (memo[item] || 0)+1;
           /* alternative:
           if (item in memo)
               memo[item]++;
           else
               memo[item] = 1;
           */
           return memo; // to be used in the next iteration!
      }, {/*"{ingredient name}": 0*/})
      .value();
    

    Notice that memo === ingredientCount!

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

Sidebar

Related Questions

I'm trying to learn to structure my code better, and have started using the
I am trying to learn MySQL select statements a little better and I have
I'm trying to learn C# from some YouTube videos, but I have a few
I have been trying to learn php and have been getting better day by
I'm trying to learn Rails better by looking at example applications, and while looking
I am trying to learn Subtyping in Java and I am not an better
Trying to learn Sencha Touch 2 and I can't seem to find out how
Trying to learn about php's arrays today. I have a set of arrays like
I'm trying to learn LINQ to SQL but having a very difficult time. I
I am trying to learn fluent nhibernate better so am doing a basic sample

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.