Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
  • I'm new to JS, so could you explain me, why are you using Function Expression instead of Function Declaration? Is there any profit of using it or it's just a code style?

  • It's better to add in JavaScript tests a test with a huge number (e.g. "12345678912345"), because some solutions that use bitwise operations:

    var stringToNumber = function(str){
    return str|0; // or ~~str (or str^0)
    }

    may work incorrectlly on huge numbers.

    For example:

    str = "12345678912345"
    str|0 // 1942903641

    This happens because in JavaScript bitwise operations convert it arguments to 32-bit integers. In this example binary representation of number in str is more than 32 bit, therefore it is truncated.

  • The error you mentioned above occures in Python3.x, while in Python2.x this code executes correctly.

    The behaviour of map function is different in Python2.x and Python3.x:
    "map" in Python2.x: https://docs.python.org/2/library/functions.html#map

    "map" in Python3.x: https://docs.python.org/3/library/functions.html#map

    Thank you for rising this question! Now I know that this code is tricky one and not compatible with version 3.x

  • This code may work incorrectlly on some inputs.
    For example:

    str = "12345678912345"
    ~~str // 1942903641
    

    This happens because bitwise operations convert it arguments to 32-bit integers. In this example binary representation of number in str is more than 32 bit, therefore it is truncated.

    So you should use this code only, if you are sure, that all numbers that you pass in your function fit 32-bit.

  • This code may work incorrectlly on some inputs.
    For example:

    str = "12345678912345"
    str|0 // 1942903641
    

    This happens because bitwise operations convert it arguments to 32-bit integers. In this example binary representation of number in str is more than 32 bit, therefore it is truncated.

    So you should use this code only, if you are sure, that all numbers that you pass in your function fit 32-bit.

  • Thank you for your remark! I've rectified this mistake.

  • Thank you for your suggestions! I've edited the description and tests. Hope I've fixed all that you had mentioned before.