Accumulate the charAt(*) for each n This is a helper function, along with three others. It should be called as a .filter(); to return only prime numbers. It is for one of the exercises at FreeCodeCamp.com, "Sum all Primes".

I'm just trying to accumulate the charAt(*) for each n, so 10 would return 1, being 1+0 21 would return 3, being 2+1 and 977 would return 23, being 9+7+7.

I've commented out some of my attempts before thinking of charAt(0) and some n's will have as many as 3 digits, maybe more if I don't have to limit it, but the largest test case stops at 997 I think, but an extra decimal for margin of error. I finally got something to work, but it feels like I'm going the long way to get there.. Surely there's something already built into to JavaScript I'm overlooking to reduce the value of a string. Or should I be trying to turn it into an array of digits?

function isItTHREED(n) {
  //  https://www.thoughtco.com/how-to-determine-number-is-prime-2312518

   //  Try 3.  Take the number, and add the digits up, when those digits are divisible by 3, the number is not prime.
  //          Take 2469, those digits add up to 21, and 21 is divisible by 3, therefore 2469 is not a prime number.
console.log('3s',n);
  n = n.toString();
  var digitONE = Number(n.charAt(0));
  var digitTWO = Number(n.charAt(1));
  var digitTHREE = Number(n.charAt(2));
  var digitFOUR = Number(n.charAt(3));
  console.log('digits',digitONE);
  console.log('digits',digitTWO);
  console.log('digits',digitTHREE);
  console.log('digits',digitFOUR);
  var testing = digitONE + digitTWO +digitTHREE + digitFOUR
  console.log(testing);
  //if(testing % 3 != 0)

  /*
  n = n.toString();
  n = n.split('');
  //num = num.split('');
  n = [n];
  console.log("num", n);
  */
   n = testing;
  return n;
} //end of isItTHREED

isItTHREED(10); // should return 1
isItTHREED(21); // should return 3
isItTHREED(977); // should return 23.


CodePen
As others have mentioned, you are attempting to find the digital sum of a given number. In JavaScript, you can do the following:

function digitalSum(number) {
  return (
    number
      .toString()
      .split('')
      .map(c =&gt; parseInt(c))
      .reduce((a, b) =&gt; a + b)
  )
}


As it intuitively appears, the code above does the following:


convert the number into a string
split the string by the empty string, thus each digit/character of the string is an individual element of the new list
converts each digit from a string to an actual number
Reduces the list by the sum function


There are other ways of accomplishing this task, but I believe the above is rather easy to understand.