If i would ever seen such code i would instantly decline any pull request this may be contained in.
Do you seriously mean that it make it simpler in any way? I have been programing node.js apps for a 3 years now and seen some horrible things done in sake of simplicity and performance and this may be one of those. What if acumulator have started as -1 for some reason? You would have never change it in the end.
I understand how the ternary and the logical operators work, and still I agree with you that it doesn't make it "simpler". If and else takes some more lines of code, but it's more easy to debug, refactor, document compared to single line expressions.
While using && to actually return the latter is indeed a cool trick, it's probably confusing for beginners. And in my opinion the ternary operator comes in more handy in this case.
Here's a simple approach which uses the ternary operator and should also be pretty easy to understand for beginners:
That = in += is redundant since the reducer only needs to return the new value, not change the old one. I mean, it'll still work, because the correct value is returned, but there's no need in changing the passed-in accumulator.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Liquid syntax error: 'raw' tag was never closed
If i would ever seen such code i would instantly decline any pull request this may be contained in.
Do you seriously mean that it make it simpler in any way? I have been programing node.js apps for a 3 years now and seen some horrible things done in sake of simplicity and performance and this may be one of those. What if acumulator have started as
-1
for some reason? You would have never change it in the end.You are right, when accumulator stars in -1, it'll fail. When I said much simpler, I meant simpler in terms of writing code. :D
Protip: Don't try to be clever with your code:
simplethread.com/dont-be-clever/
I understand how the ternary and the logical operators work, and still I agree with you that it doesn't make it "simpler". If and else takes some more lines of code, but it's more easy to debug, refactor, document compared to single line expressions.
Has no one mentioned filter!?
I think a better example of
reduce
would be something that can't be done withfilter
.I'll just briefly explain what that
&&
does, for everyone not understanding what you mean:&&
returns the value of the right-hand expression when both are truthy.Same with
||
: It returns the left-hand expression if it's truthy, or the right-hand one of the first one was falsey.See the descriptions of the logical operators (and the examples) on MDN for more information.
So
&&
can be used like 'when the left expression is truthy, return the right-hand (or run a function)'.E.g.
And
||
can be used to provide a default value (or also run a function or something).Hope this was not too confusing.
While using
&&
to actually return the latter is indeed a cool trick, it's probably confusing for beginners. And in my opinion the ternary operator comes in more handy in this case.Here's a simple approach which uses the ternary operator and should also be pretty easy to understand for beginners:
That works, because
is basically the same as
(Think of
?
and:
as 'then' and 'else')And since we can leave away the curly braces in this case (since it's just one expression), we can simply use this arrow function:
So this was a brief explanation (hopefully someone understands what I'm saying) of the ternary operator, see MDN to learn more
return accumulator + Number(item === "Bob");
return (accumulator += (item === "Bob" ?1:0));
this way(using "?:" or "&&||" after equal sign) works better in lots of situation
That
=
in+=
is redundant since the reducer only needs to return the new value, not change the old one. I mean, it'll still work, because the correct value is returned, but there's no need in changing the passed-in accumulator.