Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
It's working for me in chrome just now. Try wrapping the output line in a console.log if you're outside a REPL
In the reduce acc is the accumulator, keeping a sum of values as they come up. The reduce returns the sum at the end.
Each c is a character from the string (...str is an array of the chars from str) c == 'x' || c == 'X' is obv a boolean. The code uses the fact that in JS Number(true) == 1 and Number(false) == 0, so it's a shorter way of saying:
Bro, nice solution .cud u explain me what is going inside the reduce. And im not able to reproduce it.
It's working for me in chrome just now. Try wrapping the output line in a
console.log
if you're outside a REPLIn the reduce
acc
is the accumulator, keeping a sum of values as they come up. The reduce returns the sum at the end.Each
c
is a character from the string (...str
is an array of the chars fromstr
)c == 'x' || c == 'X'
is obv a boolean. The code uses the fact that in JSNumber(true) == 1
andNumber(false) == 0
, so it's a shorter way of saying:So each x adds one to the sum, and each o subtracts one. If the sum is zero there are the same number of x's as o's
Cool.its working😃