Good evening! Here's our prompt for today.
We're provided a positive integer num. Can you write a method to repeatedly add all of its digits until the result has only one digit?
Here's an example: if the input was 49, we'd go through the following steps:
SNIPPET
1// start with 49
24 + 9 = 13
3
4// since the previous addition was 13,
5// move onto summing 13's digits
61 + 3 = 4We would then return 4.
Constraints
- Input will be in the range between
0and1000000000 - Expected time complexity :
O(log n) - Expected space complexity :
O(1)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx30
var assert = require('assert');​function sumDigits(num) { return num;}​try { assert.equal(sumDigits(1), 1);​ console.log('PASSED: ' + 'sumDigits(1) should return 1');} catch (err) { console.log(err);}​try { assert.equal(sumDigits(49), 4);​ console.log('PASSED: ' + 'sumDigits(49) should return 4');} catch (err) { console.log(err);}​try { assert.equal(sumDigits(439230), 3);​ console.log('PASSED: ' + 'sumDigits(439230) should return 3');} catch (err) { console.log(err);}OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's how we would solve this problem...
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

