Teach me whats wrong with how I write arguments

Teach me whats wrong with how I write arguments
0.0 0

#1

This is from one of the questions of front-end development course.
I tried to define a function. Below is the description of this problem.
“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.”
Please someone teach me what is wrong with my code. Thanks!

function destroyer(arr) {
  // Remove all the values
  var result =[];
  for (var i=0; i<arguments; i++){
    for (var j=0; j<arguments[0].length; j++){
      if (arguments[0][j]!==arguments[i]){
        result.push(arguments[0][j]);
      }
    }
  }
  return result;
}

#2

I’ve edited your post for readability. 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.


#3

This was a really hard one that I struggled with as well. There is a function you should read about that helped me solve the problem. It does not mention it in the description. Check this out which will help solve your problem. its called Array.prototype.slice.call(arguments) This will help you turn your arugements into a array,
Then all you need to do is take them as your are. also your logic needs some help. You could also just slice from the 1st array when there is a double " hint hint" . Hope it helps


#4

You omitted .length of the arguments object in the outer for loop condition (see below). Add that in an the result array will have values in it. The problem is, your logic is not correct, so the values in it are not correct either. @joshuaaguilar20 has a great suggestion which you should investigate further.

for (var i=0; i<arguments; i++){

I added some console.log statements to your original code, so you can see what each variable’s value is at various points in the code as it executes. I think you will be surprised what the values are. Run the code with the console.log statements and check your browser’s console (Ctrl+Shft+J in Chrome) to see what is going on.

function destroyer(arr) {
  // Remove all the values
  var result =[];
  for (var i=0; i<arguments.length; i++){
    console.log('i='+i);
    for (var j=0; j<arguments[0].length; j++){
      console.log('j='+j)
      console.log('arguments[0][j] = ' + arguments[0][j] + ', arguments[i] = ' + JSON.stringify(arguments[i]));
      if (arguments[0][j]!==arguments[i]){
        result.push(arguments[0][j]);
      }
    }
    console.log();
  }
  return result;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

#5

Thanks for your helps, I could solve this following way.

function destroyer(arr) {
// Remove all the values
var result =[];
for (var i=0; i<arr.length; i++){
for (var j=1; j<arguments.length; j++){
if (arr[i]!== arguments[j]){
if (j==arguments.length-1){
result.push(arr[i]);
break;
}

  } else {break;}
}

}
return result;
}