News for hackers. Stuff that matters.
Re:Array Iteration Interview Problem by geuis on Wednesday August 01, 2012 @09:52PM (#4326794)
My javascript version, http://jsfiddle.net/kjqTg/

var add1 = function(arr, val, n){

    var x = n,
        end = 0;
    
    if( n < 0 ){
        arr.reverse();
        x = Math.abs(n);
    }

    for(var i=0, len=arr.length; i<len; i++){
            
        if( arr[i] === val )
            arr[i]++;
            
        if( x > 0 ){
            end++;
            if( end >= x+1)
                break;
        }
            
    }

    if( n < 0 ) arr.reverse();

    return arr;    
}

console.log( add1( [1,4,1,5,1], 1, 0 ) ); // [2,4,2,5,2] console.log( add1( [1,4,1,5,1], 1, 2 ) ); //[2,4,2,5,1] console.log( add1( [1,4,1,5,1], 1, -2 ) ); //[1,4,2,5,2]​

Re:Array Iteration Interview Problem by jazzychad on Wednesday August 01, 2012 @09:54PM (#4326806)
another double reverse :)