Every story and comment from the Hacker News API, in a layout borrowed from 2001.
A comment on Array Iteration Interview Problem.
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]
var add1 = function(arr, val, n){
}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]