r/learnjavascript • u/OsamuMidoriya • Jul 11 '24
debugging problem
My teacher was telling us about debugging, and how it's basically figuring out why a code is not working and that's what we will be spending most of our time on the job doing. He gave us the example below. we come into work and a coworker gives us this code because its not working(it works),together we was worked on it step by step what it does. NOTE this is not original we renamed something to make it easier to read.
I understand what this code does, the problem I am having is the [] at the end of the function.
const flattend = [[0, 1], [2, 3], [4, 5]].reduce{
(accumulator, array) => accumulator.concat(array), []);he said that the code is saying the accumulator should start off as an empathy array [] and it basically saying
(accumulator, array) => [].concat(array), []);I'm not sure why or how the [] is the accumulator, because of this I'm now unsure how to tell what the perimeter will be in any code
View all comments
Show parent comment
1
u/tapgiles Jul 12 '24
Well it is the initial value the accumulator will have. That value passed to the callback the first time around. That’s all.
That’s simply what the second parameter is for the reduced method.
Sorry, it’s hard to understand the idea that you totally get how it all works… but are struggling with the idea of an argument being passed into a function. I’m not sure how deep to go or what to even answer—it’s just so unexpected. So I explained everything.
…And that still didn’t make sense to you, I guess. So… 🤔