Mark As Completed Discussion

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 = 4

We would then return 4.

Constraints

  • Input will be in the range between 0 and 1000000000
  • 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?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

We'll now take you through what you need to know.

How do I use this guide?