1

I need to write a function that will delete the number or item entered in the input box. Heres what I have so far...

const my_arr = [1, 2, 3, 9]

function my_set_remove(my_arr, value) {
  let result = [];
  for (let i = 0; i < my_arr.length; i++) {
    result = my_arr[i];
    if (my_arr[i] == value) {
      let new_arr = my_arr.pop[i];

      return new_arr;
    }
  }
}
console.log(my_set_remove(my_arr, 9));

When I console.log it says undefined. Thanks in advance for your help

  • just try this: `my_arr = my_arr.filter(child => child !== valueThatDoesntWant);` – halilcakar Jun 07 '20 at 12:40
  • There's a bunch of issues: result is never used anywhere, and your loop doesn't run at all because you need `i < my_arr.length` as condition, plus `.pop()` is a function but you have square brackets there. It also ignores any arguments because it will remove (and return) the last element. – Chris G Jun 07 '20 at 12:45
  • Does this answer your question? [How can I remove a specific item from an array?](https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array) – Chris G Jun 07 '20 at 12:46
  • Yea there is bunch of correct answers for this, i've just sended the one i use mostly =) – halilcakar Jun 07 '20 at 12:46
  • `my_arr.pop[i]` attempts to access the ith property of [*Array.prototype.pop*](https://tc39.es/ecma262/#sec-array.prototype.pop), which doesn't exist so returns *undefined*. – RobG Jun 07 '20 at 12:48
  • This is for a class, so I am limited on what I can use. Im only allowed .length, .pop and .push, nothing else. Thats whats making me hit a wall – guywhogames Jun 07 '20 at 13:10

3 Answers3

1

From your comment you say:

This is for a class, so I am limited on what I can use. Im only allowed .length, .pop and .push, nothing else

So with that in mind, I try to stick to what you started with and make it work (although there are plenty of other ways to do it too):

you just need to push all items from your input array to your output/result array that do not equal your value you want to remove, and then at the end return that output/result array. Your input array remains unchanged.

Any questions, let me know.

const my_arr = [1, 2, 3, 9]

function my_set_remove(my_arr, value) {
  let result = [];
  for (let i = 0; i < my_arr.length; i++) {
    if (my_arr[i] != value) {
      result.push(my_arr[i]);
    }
  }
  return result;
}
console.log(my_set_remove(my_arr, 9));

Output:

[1, 2, 3]
Alex L
  • 3,643
  • 1
  • 7
  • 19
0

ES6 reduce for solution

const my_arr = [1, 2, 3, 9]

function my_set_remove(my_arr, value){
  return my_arr.reduce((acc, rec) => {
    if (rec !== value) {
      return acc.concat(rec)
    }
    return acc
  },[])
}
console.log(my_set_remove(my_arr, 9 ))
Alex L
  • 3,643
  • 1
  • 7
  • 19
0

Using filter seems to be what you need:

const my_arr = [1, 2, 3, 9]

function my_set_remove(my_arr, value) {
    return my_arr.filter((original_val) => original_val !== value)
}

console.log(my_set_remove(my_arr, 9));
Alex L
  • 3,643
  • 1
  • 7
  • 19