News for hackers. Stuff that matters.
Re:Array Iteration Interview Problem by prophetjohn on Wednesday August 01, 2012 @09:38PM (#4326753)
Here's my 10-minutes-of-thought JavaScript implementation. I was going to do Ruby until I realized how long it's been since I wrote any Ruby.

I only got it down to 2 cases

    function add1(arr, val, n) {
      var m = 0, i;
    
      if(n > 0) {
        for(i = 0; i < arr.length && m < n; i++) {
          if(arr[i] === val) {
            arr[i]++;
            m++;
          }
        }
      } else {
        for(i = arr.length; i >= 0 && (m !== n || !n); i--) {
          if(arr[i] === val) {
            arr[i]++;
            m--;
          }
        }
      }
      console.log(arr);
    }

There are no comments on this comment yet.