小编ice*_*lip的帖子

为什么尾随零和前导零会导致我的函数返回我不想要的值?

我提供的这组代码应使用我输入的任意数字并将其取反。当前,该函数在n = 21.365或n = 5698时起作用,但是当n = 0456456456时,它返回41422397而不是6546546540,或者在n = 056985时,它返回58965而不是589650,而在n = 016540时,它返回257而不是45610。

这是我到目前为止写的

//rules & limitations
//negative numbers should remain negative
//leading zeros should be removed
//the function can accept floats or integers
//the function will return integers as integers

function reverseNumberWithBuiltInFunctions(n) {
    return (
        parseFloat (
            n
            //convert the number to a string
            .toString()
            //convert to array of characters
            .split('')
            //reverse array of characters
            .reverse()
            //join reversed array of characters
            .join('')
        ) * Math.sign(n)
    )
}
Run Code Online (Sandbox Code Playgroud)

我想如果n = 001,则返回100;或者,如果n = …

javascript function

4
推荐指数
1
解决办法
33
查看次数

标签 统计

function ×1

javascript ×1