Understand a piece of code


Can any one explain how this code will work

function destroyer(arr) {
  var args = [] ; 
  for (var x = 0 ; x < arguments.length ; x++){
  var args1 = [];
  for (var z = 1 ; z < args.length ; z++){
  var result = [] ; 
  // Main idea to add values inside result array which !== values from args1 array.
  // check if args[0][j] value !== args1[i] value then push value in result
  for ( var j = 0; j < args1.length ; j++){
    for (var i = 0;i < arr.length ; i++){
      if (arr[i] !== args1[j]){
  return result ;

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


I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


I will try to help you figure out your current code does not work by asking you some questions.

  1. Why do you go through the trouble of creating an array called args, but do not use it in the rest of your code? Do you even need this array?

  2. Why do your comments say you are going to be checking the following:

 if args[0][j] value !== args1[i] 

when your actual code shows the following?

if (arr[i] !== args1[j])
  1. Do you really need a nested for loop to solve this problem?

  2. Have you already learned about a JavaScript function used with arrays, where you can check if a specify value is in the array or not? If so, then you can just loop through args1 only and use the learned function with the first argument passed (the array) to solve the problem instead of your nested for loops section.


Ok, I got it. Thanks


1- I created it to access the arguments instead of using slice() and you will find it’s usage at this line
for (var z = 1 ; z < args.length ; z++){

2- Because args[0] === arr

3- I am not sure for this but, I think so.

4- Can’t get what you mean !