Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

258. Add Digits #3

Open
jzhangnu opened this issue Jun 23, 2016 · 0 comments
Open

258. Add Digits #3

jzhangnu opened this issue Jun 23, 2016 · 0 comments

Comments

@jzhangnu
Copy link
Owner

jzhangnu commented Jun 23, 2016

Solution 1

var addDigits = function(num) {
    while(num >= 10){
        num = parseInt(num/10) + num%10;
    }
    return num
};

Solution 2

var addDigits = function(num) {
    var rst = 0;
    Array.prototype.forEach.call(num + '', function (v) { rst += +v; });
    return rst < 10 ? rst : addDigits(rst);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant