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

6
  • just try this: my_arr = my_arr.filter(child => child !== valueThatDoesntWant); Commented Jun 7, 2020 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. Commented Jun 7, 2020 at 12:45
  • Does this answer your question? How can I remove a specific item from an array? Commented Jun 7, 2020 at 12:46
  • Yea there is bunch of correct answers for this, i've just sended the one i use mostly =) Commented Jun 7, 2020 at 12:46
  • my_arr.pop[i] attempts to access the ith property of Array.prototype.pop, which doesn't exist so returns undefined. Commented Jun 7, 2020 at 12:48

3 Answers 3

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]
Sign up to request clarification or add additional context in comments.

Comments

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));

Comments

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 ))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.