What's wrong with my indexOf method?

The console shows that newArr2.indexOf is not a function, what’s wrong with my code?
if I want to use .indexOf() method, what should I do to make it all right?

Quesion:

Seek and Destroy

You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

Note: You have to use the arguments object.

  **Your code so far**

function destroyer(arr) {
let newArr1 = arr.shift();
const newArr2 = [];
for (let i = 0; i < arr[0]; i++){
  if (newArr1.indexOf(arr[0][i]) === -1){
    return newArr2.push(arr[0][i]);
  }
}
return newArr2;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36

Challenge: Seek and Destroy

Link to the challenge:

At this point, what’s in newArr1? Try logging it, it’s not what you think.

1 Like

Thanks, I just found the mistake, too, and could you tell me how I can get the array which has been removed the first element?

I changed it to arr.slice(1), but it doesn’t work, neither.

What do you intend arr to contain? At this point, it is that first array parameter.

I want to creat a new array without the first element, and iterate the first element, then compare them, if arr[0][i] doesn’t exist in the new array(without first element), then push it.

This is my new code, but it doesn’t work.

function destroyer(arr) {
  let newArr1 = arr.slice(1);
  const newArr2 = [];
  for (let i = 0; i < arr[0].length; i++){
    if (newArr1.indexOf(arr[0][i]) === -1){
      return newArr2.push(arr[0][i]);
    }
  }
  return newArr2;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

But destroyer(arr) catches only the first parameter passed, [1,2,3,1,2,3], and ignores the rest (the 2,3).

1 Like

No need for pictures.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

2 Likes

I get what you mean now, thanks

https://devdocs.io/javascript/functions/rest_parameters

This might be a useful read.

I have no idea to figure it out what’s wrong with this :upside_down_face: canyou help me with this?

function destroyer(arr,...theArg) {
  let newArr1 = [...theArg];
  const newArr2 = [];
  for (let i = 0; i < arr.length; i++){
    if (newArr1.indexOf(arr[i]) === -1){
      return newArr2.push(arr[i]);
    }
  }
  return newArr2;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Try console.log() the values in arr and theArg, see what they actually contain.

Next, within that if, why are you returning anything? What do you think a return in there is doing?


so the Object.values() will remove the repeat value? I almost get there, the next step I need to do is let the method keep the repeated value as a element.

Let me ask again. Inside the loop, what do you think that return is doing?

it is creating an array with the elements which not exist in the newArr1

The return immediately exits the function, returning whatever evaluation it just completed. Do you want to break out of the function and the loop at the very first match?

1 Like

get it , so .indexOf() just do one match and stop, right? I need to find another method which won’t stop after the first match.

I use the for loop, why it stops?

You still don’t need to post pictures. They don’t really help.


The behavior of indexOf() and the behavior of return are two separate issues.

Any time your code encounters a return statement, the function stops immediately. Even in the middle of a loop.

2 Likes