0

I tried to solve but didn't work :

const SumOf = (N) => {
  var res = N.toString().split("");
  var total = 0;
  for (i = 0; i < N; i++) {
    total += res[i]
  }
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 1
    You're close. Change `i < N` to `i < res.length` and `total += res[i]` to `total += +res[i]` (additional `+` before `res[i]` to convert it to a number). – Ivar Jun 30 '21 at 12:32
  • thank you it is working –  Jun 30 '21 at 12:47

1 Answers1

-1

You can simply write:

const sumN = (number) => {
  const nArray = number.split("").map(n=> +n)
  return nArray.reduce((acc, cur)=> acc+=cur, 0)
}

console.log(sumN("123"))
DoneDeal0
  • 5,273
  • 13
  • 55
  • 114