News for hackers. Stuff that matters.
Re:JS1k demo: “Highway at Night” by chrismorgan on Sunday November 23, 2014 @05:50PM (#8650253)
~x.indexOf(…) would be “starts with” rather than “contains”, for negative numbers such as -2 (from ~1) are truthy.
Re:JS1k demo: “Highway at Night” by nmjohn on Sunday November 23, 2014 @07:44PM (#8650541)
> ~x.indexOf(…) would be “starts with” rather than “contains”, for negative numbers such as -2 (from ~1) are truthy.

Nope, here's an example you can run it run in your console:

    function contains(needle, haystack) {
      return Boolean(~haystack.indexOf(needle));
    }
    
    var haystack = "The quick brown fox jumps over the lazy dog";
    console.log(contains("The", haystack));
    console.log(contains("dog", haystack));
    console.log(contains("brown fox jumps", haystack));
Here are three potential cases:

    haystack.indexOf('dog') = 40 -> truthy
    ~40 = -41 -> truthy
    
    haystack.indexOf('The') = 0 -> falsey (WRONG)
    ~0 = -1 -> truthy (FIXED)
    
    haystack.indexOf('nope') = -1 -> truthy (WRONG)
    ~-1 = 0 -> falsey (FIXED)
Re:JS1k demo: “Highway at Night” by Igglyboo on Sunday November 23, 2014 @09:14PM (#8650722)
indexOf will only return -1, 0, or a positive integer