-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (32 loc) · 928 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Title : add-digits
// Date : 2017-03-20
// Author : Daguo
/*****************************************
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
*****************************************/
/**
* @param {number} num
* @return {number}
*/
var addDigits = function(num) {
var splitNum = function(n) {
var strArr = ''.split.call(n, '');
num = strArr.reduce((pre, cur) => {
return (+pre) + (+cur);
})
};
while (num > 9) {
splitNum(num);
};
return num;
};
// without loop
var addDigits2 = function(num) {
var res = num % 9;
return (res != 0 || num == 0) ? res : 9;
}
module.exports = [addDigits,addDigits2];