Seek and Destroy - why is arr[0] not printing?

Tell us what’s happening:

Hello - I think I know how to solve this… but in just starting out… i tried looking at arr[0]… I get a print out of 1. Rather than I thought I would get returned the whole first array which is what I think is in arr[0]… arr[1] returns 2 as expected.

Why would arr[0] not return [1, 2, 3, 1, 2, 3] ?

thanks,
Brett

Your code so far


function destroyer(arr) {
  // Remove all the values
  var destroy = [];
  console.log(JSON.stringify(arr[0]));


  var search = arr.slice(1);

  console.log(search);

  arr.filter(element => {
     
  });
  return arr;
}

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy

On same topic -

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

var search = arr.slice(1);
console.log(search);

Why is this slice not returning [2, 3] as I would expect since again, I think array[0] is a full array?

thx

I’m not sure I understand what you mean with the whole “first” array comment, you only pass one array as a parameter when you call the function. If you do this:

function destroyer(arr) {
 console.log(arr[0])
}

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

What do you think the output is going to be?

I mean no offense, but you are trying to solve the Seek and Destroy intermediate algorithm and you don’t know why console.log(arr[0]) will not print the whole array? This is a very basic concept, I don’t think one would be ready to solve the basic algorithms w/o knowing that, let alone the intermediate ones. Again, I apologize if I come off as rude, but it’s weird that you are trying to solve the intermediate algorithms without knowing such core concept.

In any case, if you want to use the whole array just arr would do.

function destroyer(arr) {
 console.log(arr)
}

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

Hi - Sorry, maybe I mis-read it too quickly…or it’s too late…

I thought ([1, 2, 3, 1, 2, 3], 2, 3)

meant an array of
[0] = 1, 2, 3, 1, 2, 3
[1] = 2
[2] = 3

But I guess it’s a flat array, 1, 2, 3, 1, 2, 3, 2, 3… which explains my console of arr[0] being 1.
(yes i get what the [0] element of an array is…)

Oh I see what you are confused about. In this case you only have one array which is [1, 2, 3, 1, 2, 3] the 2,3 are a number of arguments that you are passing when executing the function, it could be 1 or 10 arguments, etc, in this case you are passing only 2 arguments besides the array.

You can try this function destroyer(arr, ...args) { console.log(args) }, by doing this you will have all the arguments that you passed when you call the function in an array.

understood thanks… but why are the [] meaningless?

I don’t understand what you mean, what meaningless brackets [] are you referring to?

Sorry. The call to destroyer.
first 6 numbers have [] around them which i thought was a single argument as an array but i guess not.

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

It is a single argument, as such arr, which is the first parameter of the function, is that array. You are not using the other parameters in the function
Check the difference between arr and the object arguments, print both of them and you will see

the function has only one parameter, so the first argument passed in the function is stored in that parameter, the others are not. You can access the other parameters through the arguments object, or through a second parameter with the rest operator as shown by @Gilbert1391

You are actually very close. Just a small assumption mistake:

[ [1,2,3], 4, 5].slice(1); // [4, 5]

function test(arr){
     console.log(arr);
}

test([1, 2, 3, 1, 2, 3], 2, 3); // [1, 2, 3, 1, 2, 3]

arr is not all 3 elements. arr is only the first argument. (which is [1, 2, 3, 1, 2, 3])

you could do this…

function test(...arr){
     console.log(arr.slice(1));
}

var x = test([1,2,3], 4, 5); // [4, 5]

notice the argument is ...arr and not arr