Jolly Jumper

http://codegolf.stackexchange.com/questions/3610/jolly-jumper-sequence

by briguy37

JavaScript

function isJolly(list){
    //Iterate over i to list.length-1
    for(r=i=1;i<(length=list.length);i++){
        //Check the differences between all consecutive elements squared minus i squared.  Set t to true if one was found.
        for(j=t=0;j+1<length;)t+=(diff=list[j]-list[++j])*diff==i*i;
        
        //if t is not true, return value is 0
        t||(r=0)
    }
    return r
}

alert(isJolly([1,3,0,-1,-5]));//jolly
alert(isJolly([1,2,4,7]));//jolly
alert(isJolly([1,3,4,7]));//jolly
alert(isJolly([4,3,4,7]));//not jolly