Seek and Destroy`

Seek and Destroy`
0

#1

Tell us what’s happening:
Hi, I’m not sure what in my code would give me the output that I am getting. This code with this sample produces an output of 13131212. Could someone please point me in the right direction?

Thank you

Your code so far


function destroyer(arr) {
  // Remove all the values
  var newArr = [];
  
  for(var i = 1;arguments.length>i;i++) //goes through extra numbers after the first array in arguments
    {
       for(var j = 0;arr.length>j;j++) ///goes through the array in arguments and removes matching numbers
         {
           if(arguments[i] != arr[j]){newArr += arr[j];}
           //console.log(newArr);
         }
    }

  return newArr;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36.

Link to the challenge:


#2

The following is attempting to replace newArr with the concatenation of itself and arr[j]. You need to use the push function to add elements into newArr.

Doing this will not solve the challenge, but at least newArr will be an array instead of a string.


#3

Thank you for the reply. I have my output as an array now but it looks like I’m not iterating over my array. Is there something wrong with my logic, or might the problem be syntactic?


#4

You will need to reply back to this post and copy/paste your latest code, so I can take a look.


#5
function destroyer(arr) {
  // Remove all the values
  var newArr = [];
  
  for(var i = 1;arguments.length>i;i++) //goes through extra numbers after the first array in arguments
    {
       for(var j = 0;arr.length>j;j++) ///goes through the array in arguments and removes matching numbers
         {
           if(arguments[i] !== arr[j]){newArr.push(arr[j]);}
           //console.log(newArr);
         }
    }

  return newArr;
}

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

#6

Your logic is pretty far off. Let me help you by writing out the algorithm in plain language. Then you can take it and see if you can code it.

Iterate through each element in the first argument (arr) of the function and if an element in arr is not one of the other function arguments (extra arguments after the first arr argument), then push the arr element into newArr. After iterating through all elements of arr, return newArr.

The part you seem to be having trouble with is the " if an element in arr is not one of the other function arguments (extra arguments after the first arr argument)". One approach to this is to create an inner loop which iterates on the extra numbers after the first arr argument. Before this inner loops begins, you will need an extra variable (let’s call it found) which will hold the Boolean value false. This false represents the fact that the current number in arr has not been found. We assume false and will test if we do find it. When we do find it, we will assign the value true to found and then stop iterating (learn about the break statement) in the current inner loop. Right after the inner for loop is an if statement asking whether found is equal to false. If it is, then you can push the arr element into newArr, because it was not “found” to be one of the extra number after the arr argument.

Take a shot at the logic I have provided you above. If you get stuck, post your most recent code and I will give you a few more pointers. I am trying to not just give you the solution.

By the way, when you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.