# Reduce-Accumulator is not working as i understand reduce- Javascript

I dont understand how the accumulator is working here in this simple reduce function. i understand accumulator is that after each iteration the accumulator value will be passed to the next iteration, i was expecting some thing like this … Any idea please if i am missing something here.

• `Expected output` as i understand reduce function
``````hellois odd // this return will be the accumulator for the next iteration
hellois odd  is even // this return will be the accumulator for the next iteration
hellois odd  is even is odd // this return will be the accumulator for the next iteration
hellois odd  is even is odd is even // this return will be the accumulator for the next iteration

``````
• `Reduce function on array17`
``````const array17 = [1, 2, 3, 4];

function Evenodd(acc, element) {
if (element % 2 == 0) return `\${acc}is even \n`;
else if (element % 3 == 0 && element % 2 != 0) {
return `\${acc}is odd \n`;
} else if (element == 1) {
return `\${acc}is odd \n`;
}
}

console.log(array17.reduce(Evenodd, "hello"));

``````
• `Actual output in console`
``````hellois odd
is even
is odd
is even
``````

so if i do in step wise.

1- `Even odd` the callback function is called with at first with accumulator as “hello” and element as `1` and the return is `hello is odd`.

2-the return in the above statement will be as accumulator in this iteration and here the accululator is `hello is odd` and the return will be `hello is odd is even`
… this is my understanding of reduce function… if i am not missing something here…

but the output is coming as
1- hello is odd
2- is odd
3- is odd
4- is even

What you’re thinking about the iterations is right. I think what you’re missing is that the strings include a new line character (`\n`). This means your output isn’t 4 different logs, its one log with 4 new lines

``````1- hellois odd \n
is even \n
is odd \n
is even \n
``````
2 Likes

Thats the point i missed was all of the output is actually one log after it finish with last iteration.
Thanks Senocular, you are always helpful

1 Like