Ad
  • Custom User Avatar

    thanks for ~~ explanations can be really useful

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    I declared an object with the keywords (life, eating, kata etc.) as properties, and set their respective values (0, 1, 5) as laid out in the problem description.

    IF for some reason when .map iterates over the array, and tries to look up a property that doesn't exist in the object, it would return undefined.

    Anything added to undefined would be NaN
    console.log(result + undefined); //NaN
    console.log(100 + undefined); //NaN


    simplified:

    var obj = {
      'hello': '1'
    };
    
    console.log(obj.hello); //'1'
    console.log(obj.goodbye); //undefined
    console.log(~~obj.goodbye); //0
    

    more examples:

    console.log(~~Infinity); //0
    console.log(~~NaN); //0
    console.log(~~false); //0
    console.log(~~null); //0
    console.log(~~undefined); //0
    console.log(~~true); //1
    
    console.log(~~-2.999); //2 ~~ can be substituted for Math.ceil() on negative numbers
    console.log(~~1.2345); //1    or Math.floor() on positive numbers
    

    All in all, I expected some test cases to include arrays that had unexepected elements.

  • Custom User Avatar

    ~ is the bitwise flip operator. For example, the binary number 0110 (6 in decimal) when flipped (each 0 flipped to a 1 and each 1 to a 0) will become 1001 (-7 in decimal for two's complement, 9 for unsigned). If you flip bits twice, they return back to their original value...

    ...So, for a number, ~~ does precisely nothing. It can help guarantee you're working with a numeric value though, which is likely why the author is using it. For example:

    ~undefined == -1
    ~-1 == 0
    ~~undefined == 0

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution