Ad
  • Custom User Avatar

    I'm 7 years late but appreciate the dialogue here!

  • Custom User Avatar

    Used the same kind of "flood" technique.
    I guess the only other way is memoïsation? (which I just learned about now :) )
    (edit: which I just finished now)

  • Custom User Avatar

    As far as I can see with %timeit function, your solution is somewhat faster than the one above...

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Instead, it adds a variable (which should have been a constant really) to the global namespace, and you have to modify something else if a new operation is needed.

    Not sure if I follow your reasoning.

    I would have preferred

    function logicalCalc() { something }
    logicalCalc.OPS = { functions };
    

    but in this case, there's really no gain from separating OPS and logicalCalc. Just make it monolithic. You can still use the { object } [ index ] construct, which makes it easy enough to add a line if there's a need for a new operation.

    function logicalCalc(array,op) {
      return array.reduce({
        op: function() { operation }, // have one such line per operation
      }[op]);
    }
    

    If you need to change the operations, you know exactly where to look for it. And the global namespace is modified exactly as much as necessary, no more, no less.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    "Clever" tends to denote a smart way of doing it, using shortcuts or different ways of using the language to complete the kata. A less-clever way of doing this would be to loop through every item in the array and build a string by hand. With such a simple kata as this one, the "clever" version is pretty straightforward and obvious to seasoned JS coders, but it's still a much better, more concise solution that any other one people have implemented.