Write a function countColors(balls) that counts the balls of each color and returns the result as one string.
balls is an array of color names, one per ball. Every name is in lower case. A name can have a space inside, for example 'light blue'.'blue 3'.'blue 3, red 5, indigo 7'. There is no comma at the end.balls. Do not sort them.''.A map keeps keys in the order you add them, so it is a good place for the counts.
countColors(['blue', 'red', 'blue'])Returns 'blue 2, red 1'
countColors(['red', 'red', 'green', 'red'])Returns 'red 3, green 1'
countColors([])Returns ''
Asked at emids · Source
countColors([ 'blue', 'red', 'indigo', 'red', 'blue', 'indigo', 'red', 'indigo', 'blue', 'indigo', 'red', 'indigo', 'red', 'indigo', 'indigo' ])'blue 3, red 5, indigo 7'countColors([ 'red', 'blue', 'red' ])'red 2, blue 1'Hidden tests run on Submit.