Ad
  • Default User Avatar

    When data input is the following:

    [{"name":"Peter","profession":"teacher","age":20,"maritalStatus":"married"},
    {"name":"Michael","profession":"teacher","age":50,"maritalStatus":"single"},
    {"name":"Peter","profession":"teacher","age":20,"maritalStatus":"married"},
    {"name":"Anna","profession":"scientific","age":20,"maritalStatus":"married"},
    {"name":"Rose","profession":"scientific","age":50,"maritalStatus":"married"},
    {"name":"Anna","profession":"scientific","age":20,"maritalStatus":"single"},
    {"name":"Anna","profession":"politician","age":50,"maritalStatus":"married"}]
    

    Test assertion is the following:

    Test.assertSimilar(query().select(professionCount).from(persons).groupBy(profession).execute(),  [["teacher",3],["scientific",3],["politician",1]]);
    

    I get this test rejection...
    Expected: '[[\'politician\', 1], [\'scientific\', 3], [\'teacher\', 3]]', instead got: '[[\'teacher\', 3], [\'scientific\', 3], [\'politician\', 1]]'

    I don't know what to do :/

  • Custom User Avatar

    Yeah, but... not in the spirit of the exercise, or that doesn't matter?

  • Default User Avatar

    some() returns a boolean, forEach() does not return a value. A return can be added as a seccond line.

  • Default User Avatar

    Array.prototype.each = [].some;
    How about that.

  • Custom User Avatar

    I definitely suggest you make this an iterative process instead of a recursive process. The recursive process will deplete the stack very quickly due to the way the number of steps and memory consumption increases with the number. This might be a shorter (code wise) recursive solution but if you use recursion with an iterative process it will ensure that the stack is never depleted.