JavaScript中整数的倒数,无需使用数组或字符串方法
我们需要编写Number.prototype.reverse()函数,该函数返回与其一起使用的数字的反向数字。
例如-
234.reverse() = 432; 6564.reverse() = 4656;
让我们为该函数编写代码。我们将使用像这样的递归方法-
示例
const reverse = function(temp = Math.abs(this), reversed = 0, isNegative =
this < 0){
if(temp){
return reverse(Math.floor(temp/10), (reversed*10)+temp%10,isNegative);
};
return !isNegative ? reversed : reversed*-1;
};
Number.prototype.reverse = reverse;
const n = -12763;
const num = 43435;
console.log(num.reverse());
console.log(n.reverse());输出结果
控制台中的输出将为-
53434 -36721
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短