我们需要编写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