From the course: Learning Functional Programming with JavaScript ES6+

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Convert array

Solution: Convert array - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript ES6+

Start my 1-month free trial

Solution: Convert array

(upbeat music) - So I'm going to show you how I solve this now. First of all the tricky part here is that we want this function to be independent of the names that are actually in the array. As I mentioned before, we're not allowed to hard code any of these names into the function. So what I'm going to do for this function is use reduce. So we're going to say return votes.reduce and remember that reduce takes two arguments the accumulator, which will be the votes that we've tallied so far and the current element, which will be the name of a certain candidate. And now before we write out our function body we have to think about what we want the starting value to be for our reduce function. What do we want it to start out with? Well since our expected output is an object, it would be a pretty good guess to say that we want our starting value to be an object. And in my case that's exactly what I'm going to do. So now let's write the body of the function that we're passing to reduce…

Contents