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)
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.
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.
functionlogicalCalc(array,op) {
returnarray.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.
"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.
I'm 7 years late but appreciate the dialogue here!
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)
As far as I can see with %timeit function, your solution is somewhat faster than the one above...
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
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
but in this case, there's really no gain from separating
OPS
andlogicalCalc
. 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.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.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
"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.