function foo(a) { return [].reduce.call(a + "", function(a, b) { return a + +b }, 0) }; alert(foo(12)); alert(foo(13));