We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Your understanding is correct for the most part. The thing that is throwing you off is the 'comma' operator I guess.
expression1,expression2,expression3
When you combine expressions with the comma, they are all evaluated from left to right, but only the last one's result is returned.
So (m=v, c+1) is first evaluationg m=v and then c+1 (which does nothing) but then return the result of c+1 to the outer context.
arr.reduce((c,v)=>v>m?(m=v,c+1):c,0)
Reduce takes a function as its first parameter. So all that
(c,v)=>v>m?(m=v,c+1):c
is the first parameter. That function has two paremeters. You are right the first parameter of the function in reduce is usually called the 'accumulator' but I am using it more like a counter here, so I happened to name it as 'c', and 'v' is the current value.
Reduce can take another (second) parameter after the function. That parameter is the start value of the 'accumulator' or 'count' which is 0 here. So reduce(myfunction, startValue) is the structure here. If we omit the startValue parameter the start value of accumulator will be the first element and it will continue with the second one.
arr.reduce((c,v)=>v>m?(m=v,c+1):c,0)
In the end, what this does is counting the times v > m (starting from zero) and pass that value as the accumulator of reduce while also updating the m (max) value using the comma operator.
For a person new to Javascript you understood it quite good I think. The fact is such obfuscated code should not be ever written in a professional setting to be honest. It is just for fun and practice here :)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Gaming Array 1
You are viewing a single comment's thread. Return to all comments →
Your understanding is correct for the most part. The thing that is throwing you off is the 'comma' operator I guess.
When you combine expressions with the comma, they are all evaluated from left to right, but only the last one's result is returned.
So (m=v, c+1) is first evaluationg m=v and then c+1 (which does nothing) but then return the result of c+1 to the outer context.
Reduce takes a function as its first parameter. So all that
is the first parameter. That function has two paremeters. You are right the first parameter of the function in reduce is usually called the 'accumulator' but I am using it more like a counter here, so I happened to name it as 'c', and 'v' is the current value.
Reduce can take another (second) parameter after the function. That parameter is the start value of the 'accumulator' or 'count' which is 0 here. So reduce(myfunction, startValue) is the structure here. If we omit the startValue parameter the start value of accumulator will be the first element and it will continue with the second one.
In the end, what this does is counting the times v > m (starting from zero) and pass that value as the accumulator of reduce while also updating the m (max) value using the comma operator.
For a person new to Javascript you understood it quite good I think. The fact is such obfuscated code should not be ever written in a professional setting to be honest. It is just for fun and practice here :)