given var num = 123456
how can I find the sum of its digits (which is 21 in this case) without using arrays?
Asked
Active
Viewed 155 times
0
-
Is this a homework question? – TylerY86 Sep 29 '16 at 05:51
-
Why _without using arrays_ ? – Rayon Sep 29 '16 at 05:54
-
@TylerY86 its not duplicate, the other question has no mention of Arrays. I'm certain that other people will want to know if doing this, without the use of arrays is possible. – Sep 29 '16 at 05:54
-
@Rayon I am solving algorithm challenges, and i needed to know if this is possible, – Sep 29 '16 at 05:55
-
@theNeighbourhoodGhost Then why didn't you search for "sum of the digits", find the other question, see that it was done without arrays, and move on? – TylerY86 Sep 29 '16 at 05:55
-
1I've closed this as a duplicate even though the other question didn't include the "no arrays" requirement, because the other question's accepted (and only) answer gives two possible solutions, the first of which doesn't use arrays. – nnnnnn Sep 29 '16 at 05:56
-
@nnnnnn ok I understand, but if someone is looking for this explicitly, then I believe my question would be of better aid to them. – Sep 29 '16 at 05:57
-
@TylerY86 don't be upset because I didn't select your answer for my other questions man, you were right, but the other ones were more suitable. – Sep 29 '16 at 05:58
-
1Sure. And with your title other people should still be able to find your question from Google or the Stackoverflow search. If I'm outvoted and people think it should be reopened I won't try to close it again... – nnnnnn Sep 29 '16 at 05:59
-
@theNeighbourhoodGhost You asked other questions? Wait, do you have multiple accounts? – TylerY86 Sep 29 '16 at 06:00
2 Answers
1
var num = 123456, sum = 0;
while ( num > 0 ) { sum += (num % 10)|0; num /= 10; }
document.write(sum);

TylerY86
- 3,737
- 16
- 29
-
-
-
If you use `console.log()` the result will appear right there in the snippet output window (as well as in the browser's actual dev console). By the way, "quaint" does mean "*charmingly* unusual or old-fashioned"... – nnnnnn Sep 29 '16 at 06:03
-
Works for me in Firefox, but for some reason doesn't work in my Chrome. Think I have it turned off for frames or something. – TylerY86 Sep 29 '16 at 06:04
-
-
@nnnnnn Anyway, pretty sure this question shouldn't have been asked. This is just golfing. – TylerY86 Sep 29 '16 at 06:06
-
Well, I may have dragged you off course (see what I did there?) in the comments, but obviously your answer works and I did vote for it. – nnnnnn Sep 29 '16 at 06:08
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124492/discussion-between-tylery86-and-nnnnnn). – TylerY86 Sep 29 '16 at 06:09
0
Hope this helps.!
console.log(sumofdigits(123456));
function sumofdigits(number) {
var sum = 0;
while (number > 0) {
sum += number % 10;
number = Math.floor(number / 10);
}
return sum;
}

sinsuren
- 1,745
- 2
- 23
- 26
-
-
*Nice copy with no credit there.* lol You could have at least used a different way to make it an int. Anyway, this is a common dupe of a question. Probably somebody's homework. :) – TylerY86 Sep 29 '16 at 05:53
-