rmurphey / js-assessment-answers

125 stars 84 forks source link

There is no answer for arrays.removeWithoutCopy #9

Closed justinabrahms closed 11 years ago

justinabrahms commented 11 years ago

Been thinking about it. Not entirely sure how to do it without generating my own intermediate array shifting things out of it, then unshifting them back into it. Seems terribly messy so I'm hoping that's not the answer. My awful answer is below.

removeWithoutCopy : function(arr, item) {
  var intermediate = [];
  while (arr.length > 0) {
    var result = arr.shift()
    if (result !== item) {
      intermediate.unshift(result);
    }
  }
  while (intermediate.length > 0) {
    arr.unshift(intermediate.shift());
  }
  return arr;
},