Back to all solutions

#258 - Add Digits

Problem Description

Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

Solution

/**
 * @param {number} num
 * @return {number}
 */
var addDigits = function(num) {
  return num < 10 ? num : num % 9 === 0 ? 9 : num % 9;
};