A lot to unpack

Tell us what’s happening:
Describe your issue in detail here.

A couple of things before I get to my question:

  1. I believe this is probably not how I need to go at this problem in the first place, and I suspect this may be a roundabout way of tackling it.
  2. the console.log on line 10 is there just so I can see the result.

Looking at the results of my code, I’m left with an array [1, 2]. How does the 2 not get weeded out in the second or fifth iterations of the first for loop? Shouldn’t arguments[0][i] (when i == 1 or 4) be 2? So when arguments[j] is 2, wouldn’t that allow the second part of the if statement to splice out the 2 from the original array? Not sure if this is making sense, but I just don’t understand why the 2 is still in play. Never mind why the second 1 was removed as there should never be an arguments[j] == 1. Let me know if this is too convoluted a question. I’m confusing myself with it.
Thanks in advance.

Additionally I have changed:
let j = 0
to:
let j = 1
with no change to the result.

  **Your code so far**

function destroyer(arr) {
let argsLength = arguments.length;
for (let i = 0; i < arguments[0].length; i++){
  for(let j = 0; j < argsLength; j++){
    if(arguments[0][i] == arguments[j]){
      arguments[0].splice(arguments[0][i]);
    }
  }
}
console.log(arguments[0])
return arguments[0];
}

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

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

Challenge: Seek and Destroy

Link to the challenge:

hello there @TheRev this challenge would be a little frustrating if its a first time for you.

The logic is to check if any item in the arguments is present in the arr either it appears once or multiple times.

Now first thing I’d say is you need to get the items in the arguments and convert into an array to work with it for this I can suggest using the [...] spread operator.

Instead of getting the length, you should get the items.
Like this:

let args = [...arguments].slice(start, end) 

You need to determine at what index you would need to slice this array to get all the arguments without including the arr given in the function.

Learn more about the slice() method here

Then going back to your loop:

This is kinda starting wrong.

To use the for loop you’d need to loop over each item in the arr and each item in the arguments using their respective length.

Then in the second iteration(loop) you can check if the item in the arr is present in the arguments array and to do this you can make use of the array.indexOf(item).

For example you have two arrays:

let arr1 = ['apple', 'banana', 'mango'];
let arr2 = ['guava', 'watermelon', 'apple', 1];

arr2.indexOf(arr1[2]) // returns -1 (item is not present)
arr2.indexOf(arr1[0]) // returns 2 (item is present at index 2 of arr2)

Then if the item is not present you can push that item into a new array and break out of the loop. If you don’t break out the items will repeat twice because you have two loops. To break out you can use the break; statement. Learn more about the “break” statement

Lastly at the very end you want to return the new array with the items not present both in the arr and the arguments.

This was quite a long explanation and I hope you can derive a solution from it. Although there are more simple ways to solve this challenge with one or two liners but having your own algorithm is strongly advised which is why I explained how you can make use of the for loop.

Let me know if you need more help or clarification. Happy coding :+1:!

1 Like

Sorry for the delay in my response. I’ve recently hit a frustrating wall in my studies and things were starting to sound like Charlie Brown’s teacher (if that reference makes sense). I had to take a break. I came back to your response, and while I haven’t completed my solution, your direction makes sense to me. Back to the fray! A genuine thank you for your assistance.

1 Like

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