javascript - The repeat function reports an error Invalid count value under ES6 specifications
迷茫
迷茫 2017-06-07 09:24:02
0
1
895

The program is as follows. It can run and produce results, but it will report an error... It's very strange and I have no idea where to start...

Code:

function pad(str, len) { 

    return '0'.repeat(len-str.length) + str 
}

function numberAndIPaddress(s){

    if (s.indexOf('.')) {

        let numbers = s.split('.').map(x=>{ return pad( parseInt(x).toString(2), 8 ) })
        
        return parseInt( numbers.join(''), 2 )

    } else {

        let number = pad( parseInt(s).toString(2), 32 )

        return     [     parseInt( number.slice(0,      8), 2),
                    parseInt( number.slice(9,     16), 2),
                    parseInt( number.slice(17,     24), 2),
                    parseInt( number.slice(25,     32), 2)     

                ].join('.')
    }
}

console.log( 'result', numberAndIPaddress("10.0.3.193") )
// console.log( numberAndIPaddress("167969729") )

Output:

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
学霸

Give it a try. Currently your code runs without any problem. But if you run the code you commented out, you will get an error very similar to the screenshot. The reason is that len-str.length is a negative number. You can debug it and see.

In addition, although eslint may also report this error, it does not appear from your error message that it comes from eslint.
You can refer to this issue first: https://github.com/eslint/esl...
It may happen in the following line:

return     [     parseInt( number.slice(0,      8), 2),
                    parseInt( number.slice(9,     16), 2),
                    parseInt( number.slice(17,     24), 2),
                    parseInt( number.slice(25,     32), 2)     

                ].join('.')  // <------ 这里
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template