Tell us what’s happening:
I know that this is a very newbie question but I’ve been struggling to understand the distinction in these two codes.
Your code so far
// This code produces [1,3,1,3]: incorrect
// It only goes through the first test argument. Why?
function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
var targetArr = args[0];
var testArr = [];
for (i = 1; i < args.length; i++) {
testArr.push(args[i]);
}
function isSame(val) {
for (var j = 0; j < testArr.length; j++){
// console.log("arr: " + arr);
// console.log("testArr: " + testArr[j]);
return val != testArr[j];
}
}
return arr.filter(isSame);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
//This code produces [1,1]: correct
function destroyer(arr) {
var testArr = [];
for (var i = 1; i < arguments.length; i++) {
testArr.push(arguments[i]);
}
function isSame(val) {
for (var j = 0; j < testArr.length; j++) {
if (val === testArr[j]) {
return false;
}
}
return true;
}
return arr.filter(isSame);
}
(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/seek-and-destroy