Why not delete the element splice method?

I get the entire array and not delete anything splice method. It should be delete when matching arguments. Thank you

function destroyer(arr) {
  for (let j = 0; j < arr.length; j++) {   
    for (let i = 1; i < arguments.length; i++) {
      if (arr[j] == arguments[i]) {
        arr.splice(arguments[i], 0);
      }
    }
  }
  return arr;
}
const result = destroyer([1, 2, 3, 1, 2, 3], 2, 3);
console.log(result);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36

Challenge: Seek and Destroy

Link to the challenge:

you may want to review how splice works, the first argument is the index at which it should operate, the second argument says how many elements to remove - you are removing 0 elements at indexes 2 and 3

also, splice changes the array on which it is used, and iterating on a mutating array gives unexpected results, so it’s highly suggested you do not do that

2 Likes
function destroyer(arr) {
  for (let j = 0; j < arr.length; j++) {   
    for (let i = 1; i < arguments.length; i++) {
      if (arr[j] == arguments[i]) {
        const prevElement = arr[j];
        arr.splice(arguments[i], 0);
        console.log(prevElement, arr[j])//2,3
//the above example is only for the first time it enters the if
      }
    }
  }
  return arr;
}
const result = destroyer([1, 2, 3, 1, 2, 3], 2, 3);

When you use splice to delete something it shortens your array length, but it does not effect that value of j so j stays the same, but arr[j] now points at the element that used to be arr[j + 1].

This comment was written assuming the splice method is being used how you want it to be not how you are using it.

1 Like

Thank you.
I want write 1, but really mutating array.

You need to use a different way to remove the unwanted elements or keep the wanted ones

1 Like

You can use splice, but you’ll need to know how to use it. The first argument is where in the array you wish to start from, and the second argument is how many to delete from that position, and it is inclusive.

If you want to delete one element at index 2 you would say arr.splice(2, 1) although in your case you will be using a variable rather than the direct number.

Since splice effects the length of the array you are looping through you will need to set i to 0 . This will reset the arguments loop, and due to the nature of splice you will already be pointing at the element you wish to use in the array so j never needs to be directly augmented.

I would say using a mutator on the array you are looping through is not generally what you want to be using a for loop for.

1 Like
function destroyer(arr) {
  let newArr = [];
  for (let i = 1; i < arguments.length; i++) {
    while (arguments[0].includes(arguments[i])) {
      let isNumber = (element) => element == arguments[i];
      let index = arguments[0].findIndex(isNumber);
      arguments[0].splice(index, 1);
    }
  }
  newArr = arguments[0];
  return newArr;
}

const result = destroyer([1, 2, 3, 1, 2, 3], 2, 3);
console.log(result);

it’s best practice to have a function that doesn’t mutate the input array - would you be able to do something like that?

1 Like

I see other solution with delete that itself the value delete then rid null, but I want solve this method :slight_smile: Thank you for your lot of help. :slight_smile:

that one is also changing the input array, it works but it’s not a best practice solution

1 Like

in this situation which answer should i mark “solution” ? :slight_smile:

you can mark as solution the post that answer your question, if there is one

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.