search

Home  >  Q&A  >  body text

javascript - The repeat function reports an error Invalid count value under ES6 specifications

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:

迷茫迷茫2791 days ago925

reply all(1)I'll reply

  • 欧阳克

    欧阳克2017-06-07 09:26:02

    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('.')  // <------ 这里

    reply
    0
  • Cancelreply