JSFiddle - React, Tailwind, and code Playground

JavaScript

//# add1 - increments items in an array matching specified value
//#
//# param: arr - array of integers to manipulate
//# param: val - integer, value to increment
//# param: n   - integer, control value specifying behavior of manipulation
//#   n == 0 means increment all occurrences of val
//#
//#   n > 0  means increment up to n occurrences of val
//#          from left-to-right (forward)
//#
//#   n < 0  means increment up to n occurrences of val
//#          from right-to-left (backward)
//#
//# return: arr with proper values incremented
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]