2

I want to create a function in JavaScript that, given an array and an index, the value in that index is removed. For example: removeAt([1,2,3,4],2) should return [1,2,4]. The only array method I can use is pop().

I came up with this(wondering if there is a more efficient way to do it):

function removeAt(arr, index) {
  var j = 0;
  var arr2 = [];
  for (var i = 0; i < arr.length - j; i++) {
    if (i != index) {
      arr2[i] = arr[i + j];
    } else {
      arr2[i] = arr[i + 1];
      j++;
    }
  }
  return arr2
}

console.log(removeAt([1, 2, 3, 4, 5], 3))
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Erick Vivas
  • 35
  • 1
  • 7
  • 1
    Does this answer your question? [Deleting array elements in JavaScript - delete vs splice](https://stackoverflow.com/questions/500606/deleting-array-elements-in-javascript-delete-vs-splice) – Sagar V Oct 01 '21 at 16:30
  • You can consider using [`array#splice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) – Hassan Imam Oct 01 '21 at 16:30
  • 1
    @HassanImam He's not allowed to use that. – Barmar Oct 01 '21 at 16:30
  • 2
    @SagarV He's not allowed to use either of those methods, only `pop()` – Barmar Oct 01 '21 at 16:31
  • 1
    @Barmar ah I didn't notice that. Only with pop, it's complicated. He could better not use it at all. – Sagar V Oct 01 '21 at 16:41
  • 1
    I didn't know how to use pop since that only delete the last index of the array. But if you come with an idea would be great. Thanks! – Erick Vivas Oct 01 '21 at 16:42
  • 2
    I also can't think of a way to do it with `pop()`. I assumed this was a restriction from using other methods, not a requirement to use this method. – Barmar Oct 01 '21 at 16:43
  • I had an idea of utilizing pop, but it definitely complicates things more (at least in my solution) by adding a third array to push values into, then popping them out of that to get the other values. If you were allowed to use [shift](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift) instead, it would be a lot easier. (Or obviously, [splice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)) – phentnil Oct 01 '21 at 16:47

3 Answers3

2

You should only add to the result array in the if condition, not else. Use j as the index in the result, don't add or subtract it.

function removeAt(arr, index) {
  var j = 0;
  var arr2 = [];
  for (var i = 0; i < arr.length; i++) {
    if (i != index) {
      arr2[j] = arr[i];
      j++;
    }
  }
  return arr2
}

console.log(removeAt([1, 2, 3, 4, 5], 3))
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

If you are not allowed to use any Array function except pop(), you may save memory space by not creating an extra variable using it.

function removeAt(arr, index){ 
    if(index>arr.length-1 || index<0) return arr;
    for(let i=0; i<arr.length; i++) {
        if(i>=index) {
            arr[i] = arr[i+1];
        }
    }
    arr.pop();
    return arr;  
}
-2

You can simply use Array.prototype.splice.

function removeAt(arr, idx){
    return arr.splice(idx, 1);
} 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Array.prototype.pop will only remove the last element and you'd have to do unnecessary operations with the array to achieve it.

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • 2
    cant use any array method but pop – Erick Vivas Oct 01 '21 at 16:43
  • This is common in academic exercises, it's intended to teach basic data structure operations. – Barmar Oct 01 '21 at 16:57
  • I realized it's an academic thing, but I stand by what I said. The correct approach to this is to use `splice`, otherwise you're teaching the person the wrong way to go about it. This is more important to me than the -1 that I get from keeping the answer up. – Dropout Oct 01 '21 at 17:51