176. Debugging
Debugging
const flattened = [[0, 1], [2, 3], [4, 5]].reduce(
(a, b) => accumulator.concat(b), [])
a is accumulator
b is array [[0, 1], [2, 3], [4, 5]] here.
const flattened = [[0, 1], [2, 3], [4, 5]].reduce(
(accumulator, array) => accumulator.concat(array), []);
the 'accumulator' start off with empty array.
The 'accumulator' is going to be an empty array when you start off.
const flattened = [[0, 1], [2, 3], [4, 5]].reduce(
(accumulator, array) => [].concat(array), []);
'concat' attaches the contents of an array into whatever's being concated.
const flattened = [[0, 1], [2, 3], [4, 5]].reduce(
(accumulator, array) => accumulator.concat(array), []);
One thing I can do is open up this function,
make like this.
const flattened = [[0, 1], [2, 3], [4, 5]].reduce(
(accumulator, array) => {
console.log('array', array);
console.log('accumulator', accumulator);
return accumulator.concat(array)
}, []);
^We're returning this
because we removed it from a single line which already implicitly means return, to making a two lines.
- [0, 1] accumulator: empty
- [2, 3] accumulator: [0, 1]
After the first loop through, I've added the zero and one. - [4, 5] accumulator: [0, 1, 2, 3]
We're grabbing each array and adding it into the accumulator.
Using the concat function to join the arrays,
we can find an array like below.
Debugger
Instead of using console.log, you can use debugger.
const flattened = [[0, 1], [2, 3], [4, 5]].reduce(
(accumulator, array) => {
debugger;
return accumulator.concat(array)
}, []);
It stops in the middle of its execution.
'Accumulator' is an empty array.
Using console and debugger you're able to understand what a function does.
Author And Source
이 문제에 관하여(176. Debugging), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@bianjy/176.-Debugging저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)