Bug in symmetrical difference?[Solved]

Bug in symmetrical difference?[Solved]
0

#1

I seem to think there are bugs and then find out they aren’t so I want to post this here before reporting it.

My code below works fine in repl.it and in the google chrome console, but when I submit it as the answer for Symmetrical difference, I get an error that says arrays[2].forEach() is not a function.

function sym(arrays){
	var newArr = [];
	
		for(var j = 0; j < arrays[0].length; j++){
			if(arrays[1].indexOf(arrays[0][j]) === -1){
				newArr.push(arrays[0][j]);
				
			}
		}
		for( j = 0; j< arrays[1].length; j++){
			if(arrays[0].indexOf(arrays[1][j]) === -1){
				newArr.push(arrays[1][j]);
			}
		}	
		
		if (arrays.length > 2){
		  arrays[2].forEach(function(item){
			if(newArr.indexOf(item) === -1){
				newArr.push(item);
			}
		  });
		}
		return newArr;
}

sym([1, 2, 3], [5, 2, 1, 4]);

#2

Have you tried console.logging your arrays.length?

It doesn’t behave the way you are expecting. In the example above: sym([1,2,3], [5,2,1,4])
…the length of (arrays) is 3, even thought the there are TWO arguments being passed. It is measuring the length of only the first argument, an array that is 3 long!!

If you check arguments.length though, you’ll get the behaviour you are expecting.


#3

Thanks for the reply, but the function works. It works in repl, it works in the console, it just doesn’t work in freecodecamp. I wanted it to measure the length that way.


#4

Oh, wait, you’re right. I don’t know what I did. Thank you. I must have had a brain fart.