This site does not allow itself to be embedded. You can still read it on the original site — the toolbar below keeps your place in the directory.
Algorithm to sum the digits of a number. function digitSum(n) { if ( ! ( typeof n === 'number' || n instanceof Number )) { return 0 ; } if (n <= 0 ) { return 0 ; } else if (n < 10 ) { return n; } else if (n === Infinity ) { return Infinity ; } return (n % 10 ) + digitSum((n / 10 ) >> 0 ); } Usage: console.log(digitSum( 1234 )); // 10 console.log(digitSum( 3890 )); 20 console.log(digitSum( Infinity…
Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.