There’s really never any need to do this. If statements already take in a boolean, why would you have them return one?
return this !== value;
You should also console.log arguments and see what it looks like. I think your problems are stemming from the fact that arguments works best when it’s modified. You can either a) turn it into an array with Array.from(arguments).slice(1); //this will give you an array of all of the arguments besides arr. or b) write the function like this
function destroyer(arr, ...theArgs){ ..... }
Where theArgs is an array of all of the other arguments after arr.
There’s also an indexOf method. You don’t need to filter each argument out of arr one by one
When using ===, the function isValid(value) returns true every time no matter what.
The reason has to do with the difference between == (loose comparison) and === (strict comparison). Loose comparison performs type conversion before comparing. Strict does not.
The this value in memory is not a javascript number. it’s a javascript object that works functionally like this (if the value were loosely 10):
this = {
valueOf: function(){
return 10 ;
}
}
So, this == 10 is true, because javascript converts the this object to a number for loose comparison. this ===10 is false, because an object { … } is not exactly equal to the number 10.
But if I try this destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3);
It gives me the correct answer when I do the strict comparison. All the test cases work but code camp doesn’t give me credit for it until I do the loose comparison.
With the strict comparison it should fail. You’d have to demonstrate what you mean by test cases working. If it were the case maybe you stumbled across a bug but I can’t reproduce it.
@razieltov Ok, I think i see what you’re getting at. The code camp editor is not returning the correct result and I’m not sure why. If you run the code through a normal javascript engine (like a web browser) and inspect the output the === doesn’t work and == does.
For some reason FCC editor isn’t displaying the correct output.
@reichamus Your code is close to correct. The problem is you’re using global variables unnecessarily.
By leaving the var … = [] outside the function call they’re not being reset between tests… so push() keeps adding new values to the arrays which causes the test to fail. You should always keep variables in the most contained scope possible to prevent unintended consequences like this.
function destroyer(arr) {
var argArray = [];
var testArray = [];
var finalArray = [];
// Remove all the values
testArray = arguments[0];
for(var i =1; i < arguments.length; i++){
argArray.push(arguments[i]);
}
for(var j = 0; j < testArray.length; j++){
if(argArray.indexOf(testArray[j]) < 0){
finalArray.push(testArray[j]);
}
}
return finalArray;
}
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3);
@cjsheets I see. So its not that === is correct. Its actually incorrect. But for some reason the FCC editor shows the correct output when in reality with that code its different output. I tried the code in a regular webpage and it doesn’t work. I see now. Thank you for that.