learnrx icon indicating copy to clipboard operation
learnrx copied to clipboard

Exercise 19: Reducing with an initial value

Open haveagoodone opened this issue 10 years ago • 3 comments

If I console.log() the result of this exercise, it is not like the expected one:

[
  {
    "65432445": "The Chamber",
    "675465": "Fracture",
    "70111470": "Die Hard",
    "654356453": "Bad Boys"
  }
]

it looks like this:

[
  {
    "654356453": "Bad Boys"
  }
]

Is the expected output wrong or just the solution?

Thanks for the great Exercises at all.

haveagoodone avatar Jun 22 '15 08:06 haveagoodone

I had the same result. It took me a while to figure it out. The expected output is using the implementation of the reduce function from exercise 16, which returns an array of accumulated values. You are using the standard reduce method from the Array.prototype object in Javascript, which only returns a single accumulated value. To get the desired output just add reduce implementation from exercise 16 to the Javascript Array.prototype.

Cheers!

wwwfreedom avatar Aug 04 '15 10:08 wwwfreedom

@wwwfreedom thanks for your explanation:smile: . And btw, what should I do about the different behavior of them in real world?

HaveF avatar Oct 30 '15 01:10 HaveF

#44 Reduce returns an array, not a value which is a very detailed explanation about reduce/fold thing

HaveF avatar Oct 30 '15 16:10 HaveF