// the first solution by recursion
const numToReverseStr = num => {
if( 'number' !== typeof num ) throw '输入需为int型整数';
if(!Math.floor(num / 10)) return num.toString();
return (num % 10).toString() + numToReverseStr( Math.floor(num / 10) );
}
console.log(numToReverseStr(2169362));
// the second solution not by recursion but JavaScript API
const numToReverseStr_0 = num => {
return num.toString().split('').reverse().join('');
}
console.log(numToReverseStr_0(2169362));
其实这种直接使用JavaScript的API更简单直观。
第 99 题:编程算法题