# Map function - adding arrays as integers, not as strings

**URL:** <https://forum.freecodecamp.org/t/map-function-adding-arrays-as-integers-not-as-strings/334460>\
**Category:** General\
**Created:** [December 19, 2019, 9:11am UTC](https://forum.freecodecamp.org/t/map-function-adding-arrays-as-integers-not-as-strings/334460 "2019-12-19T09:11:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![silvercut](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@silvercut](https://forum.freecodecamp.org/u/silvercut)\
**Post date:** [December 19, 2019, 9:11am UTC](https://forum.freecodecamp.org/t/map-function-adding-arrays-as-integers-not-as-strings/334460/1 "2019-12-19T09:11:05Z")

</div>

Hi for the map function, it is adding the strings onto each other rather than adding them as numbers (0161 rather than 0+1+6+1). what can i do to change this? thanks

> **[Train: Sum of Digits / Digital Root | Codewars](https://www.codewars.com/kata/sum-of-digits-slash-digital-root/train/javascript)**
>
> Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

```auto
function digital_root(n) {

  //create a marker to track the length of the output

  let nLength = n.toString().length;

  //if its greater than >1 continue the loop

  //create a store for the new n if its >1

  let summation = 161;

  let currentAnswer = 0;

  //loop

  while (nLength > 1) {

    summation

      .toString()

      .split("")

      .map(number => parseInt((currentAnswer = currentAnswer + number)))

      .join();

    nLength = summation.length;

  }

  return summation;

  // initially split the digits into its parts and add them together

  //if the length of the new n is > 1, continue the loop/

  //otherwise break out of the loop

}

digital_root(161);

```

---

<div class="post-metadata">

**Author:** ![snigo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snigo/32/128765_2.png) [@snigo](https://forum.freecodecamp.org/u/snigo)\
**Post date:** [December 19, 2019, 9:31am UTC](https://forum.freecodecamp.org/t/map-function-adding-arrays-as-integers-not-as-strings/334460/2 "2019-12-19T09:31:37Z")

</div>

`.map()` is a no-go here. Take a look at `.reduce()`:

> **[Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)**
>
> The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

---

<div class="post-metadata">

**Author:** ![silvercut](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@silvercut](https://forum.freecodecamp.org/u/silvercut)\
**Post date:** [December 19, 2019, 9:33am UTC](https://forum.freecodecamp.org/t/map-function-adding-arrays-as-integers-not-as-strings/334460/3 "2019-12-19T09:33:00Z")

</div>

yep i realised. but still returning string values not integer. i’ll look further into this. (reducer - array into integer haha always forget)

function digital\_root(n) {

//create a marker to track the length of the output

let nLength = n.toString().length;

//if its greater than \>1 continue the loop

//create a store for the new n if its \>1

let summation = 161;

let currentAnswer = 0;

//loop

// while (nLength \> 1) {

// summation

// .toString()

// .split("")

// .map(number =\> parseInt(+(currentAnswer = currentAnswer + number)))

// .join();

// nLength = summation.length;

// }

const reducer = (currentAnswer, currentValue) =\> currentAnswer + currentValue;

while (nLength \> 1) {

```
summation

  .toString()

  .split("")

  .reduce(reducer)

nLength = summation.length;

```

}

return summation;

// initially split the digits into its parts and add them together

//if the length of the new n is \> 1, continue the loop/

//otherwise break out of the loop

}

digital\_root(161);

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [December 19, 2019, 9:46am UTC](https://forum.freecodecamp.org/t/map-function-adding-arrays-as-integers-not-as-strings/334460/4 "2019-12-19T09:46:12Z")

</div>

> [@silvercut](#):
>
> toString()

`split` doesn’t automatically covert to numbers. You convert to string to split them but then you never convert back to numbers, so that’s how strings behave with the `+` concat operator

---

<div class="post-metadata">

**Author:** ![snigo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snigo/32/128765_2.png) [@snigo](https://forum.freecodecamp.org/u/snigo)\
**Post date:** [December 19, 2019, 9:48am UTC](https://forum.freecodecamp.org/t/map-function-adding-arrays-as-integers-not-as-strings/334460/5 "2019-12-19T09:48:54Z")

</div>

You might wanna see this first anyway 😉

---

<div class="post-metadata">

**Author:** ![silvercut](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@silvercut](https://forum.freecodecamp.org/u/silvercut)\
**Post date:** [December 19, 2019, 10:47am UTC](https://forum.freecodecamp.org/t/map-function-adding-arrays-as-integers-not-as-strings/334460/6 "2019-12-19T10:47:42Z")

</div>

Got it - for future reference put a + sign on the string to coerce it to an integer

wonder if ParseInt would also do the same thing

```auto
function digital_root(n) {
  //create a marker to track the length of the output
  let nLength = n.toString().length;
  //if its greater than >1 continue the loop

  //create a store for the new n if its >1

  let summation = 0;

  while (nLength > 1) {
    n = n
      .toString()
      .split("")
      .reduce((sum, digit) => sum + +digit, 0);
    nLength = n.toString().length;
  }

  return n;
  // initially split the digits into its parts and add them together
  //if the length of the new n is > 1, continue the loop/
  //otherwise break out of the loop
}

digital_root(456);

```

---

<div class="post-metadata">

**Author:** ![tunggnu](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tunggnu/32/158707_2.png) [@tunggnu](https://forum.freecodecamp.org/u/tunggnu)\
**Post date:** [December 19, 2019, 11:54am UTC](https://forum.freecodecamp.org/t/map-function-adding-arrays-as-integers-not-as-strings/334460/7 "2019-12-19T11:54:48Z")

</div>

`Number.parseInt(digit)` can convert your `digit` character to a number. But I usually use `Number(digit)` for short.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:30am UTC](https://forum.freecodecamp.org/t/map-function-adding-arrays-as-integers-not-as-strings/334460/8 "2021-01-16T11:30:01Z")

</div>

Closed on January 16, 2021, 11:30am UTC


