Recently, I delved into a code kata involving an array of football scores such as:
["3:1", "2:2"] (The total points here would be 4, 3 + 1)
After applying certain rules and summing up the points, I came across an interesting solution:
const points = g => g.reduce((a, [x, _, y]) => a + (x > y ? 3 : x == y), 0)
Just to clarify, the rules dictate that if the 1st value is greater than the 2nd, return 3 points, if they are equal, return 1 point, otherwise return 0. This mimics a traditional football match scoring system.
I'm curious about how the "x == y" part functions in this scenario. According to the rule, if "x == y", a single point should be returned. Can someone simplify this with an example?
Also, if someone could shed light on the "[x, _, y]" part, I would be grateful. I understand that it's supposed to represent the current item in the array, but since the current item is a string and not an array, I'm a bit confused about what's happening here.