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/OsamuMidoriya Jul 12 '24
I usually don't like going to MDN because the why they explain things leave me more confused. I usually watch videos because the usually easier for me to under stand I watched this to understand reduce. But I did go there first I will play with there example and that will normally help me understand what's what's. they gave this example which was straight forward I know it was just adding all the numbers up
but I was changing the
accumulatorandcurrentValuein both inside and outside the parameter and I didn't get the expected result. when i change it to1 + currentValue,oraccumulator + 1,I thought the number would go from 10 to 11 but the number for both came out as lower then 10.After reading your reply I realized that I was reading it the wrong way. I thought
arraywas the second parameter of reduce. the way MDN and the example from the teacher is different from the guy's video.I now know that its
initialValuethat I needed to change to make the output 11, but this is different from the video he said that theaccumulator was the starting number so it shouldn't [] be in the first parameter, and the second parameter the first number/ thing in the array. can you explain why it different and thank you for your time