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

**URL:** <https://forum.kirupa.com/t/reduce-accumulator-is-not-working-as-i-understand-reduce-javascript/644006>\
**Category:** Uncategorized\
**Created:** [August 28, 2020, 9:17pm UTC](https://forum.kirupa.com/t/reduce-accumulator-is-not-working-as-i-understand-reduce-javascript/644006 "2020-08-28T21:17:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Umair\_Ashraf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/umair_ashraf/32/11705_2.png) [@Umair\_Ashraf](https://forum.kirupa.com/u/Umair_Ashraf)\
**Post date:** [August 28, 2020, 9:17pm UTC](https://forum.kirupa.com/t/reduce-accumulator-is-not-working-as-i-understand-reduce-javascript/644006/1 "2020-08-28T21:17:28Z")

</div>

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

```auto
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`

```auto
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`

```auto
hellois odd 
is even
is odd
is even

```

---

<div class="post-metadata">

**Author:** ![Umair\_Ashraf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/umair_ashraf/32/11705_2.png) [@Umair\_Ashraf](https://forum.kirupa.com/u/Umair_Ashraf)\
**Post date:** [August 28, 2020, 9:29pm UTC](https://forum.kirupa.com/t/reduce-accumulator-is-not-working-as-i-understand-reduce-javascript/644006/2 "2020-08-28T21:29:36Z")

</div>

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**

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [August 29, 2020, 12:13pm UTC](https://forum.kirupa.com/t/reduce-accumulator-is-not-working-as-i-understand-reduce-javascript/644006/3 "2020-08-29T12:13:25Z")

</div>

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

```auto
1- hellois odd \n
is even \n
is odd \n
is even \n

```

---

<div class="post-metadata">

**Author:** ![Umair\_Ashraf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/umair_ashraf/32/11705_2.png) [@Umair\_Ashraf](https://forum.kirupa.com/u/Umair_Ashraf)\
**Post date:** [August 30, 2020, 2:51pm UTC](https://forum.kirupa.com/t/reduce-accumulator-is-not-working-as-i-understand-reduce-javascript/644006/4 "2020-08-30T14:51:05Z")

</div>

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 🙂
