function destroyer(arr)
{
// Remove all the values
//Turn to a real array
var args = [];
args = Array.from(arguments);
var filtered = [];
var val;
//Filter 'argument[0]'(args[0])
for (i = 0; i < args[0].length; i++) //args[0]
{
for(j = 1; j < arguments.length; j++) //args[1] =<
{
if(args[0][i] == args[j])//Check whether args[0] elements equal to others
{
break; // Stops if equal, lead to check next args[0] element
}
else
{
val = args[0][i];
//Push only after checked by all following args
if (j == arguments.length - 1)
{
filtered.push(val);
}
continue;
}
}
}
return filtered;
}
destroyer(["tree", "hamburger", 53], "tree", 53);
function destroyer(arr) {
var args = Array.from(arguments);
var result = args.shift().filter(function (value) {
if (args.indexOf(value) < 0) {
return true;
}
});
return result;
}
Hi, I was doing fine on all of the other exercises until this one. Can someone explain why filter(Boolean) only returns true values? I thought that Boolean evaluated to true OR false?
HereĀ“s my version using the same tricks. This was a tough one for me but I am glad I took the time and effort and now I actually understand the idea of the callback function.
The increment operator (++) is supposed to go after the integer variable, so it should be i++ and j++ in your for loops.
The condition for your if statement dictates that if a value in arguments[0][i] is equal to the arguments we are trying to exclude from the array (i.e. arguments[j]), it should be pushed to newArr. What this means is that youāre actually pushing the values you donāt want to the new array. Instead of pushing those values to newArr, you could simply delete them from arr. This also means you no longer need use a new array.
The else statement you implemented also meant that whenever your if condition was false, the for loops would be broken out of (which isnāt what you want as you want the for loop to iterate over all the values in arr), and nothing would be returned as you didnāt place anything after the return keyword. The else statement is actually unnecessary, as shown in my reworking of your code below.
Using the delete keyword to remove the unwanted values from arr leaves null values in their place and youāll end up with: [1, null, null, 1, null, null]
So, when returning arr, filter out the null values using arr.filter(Boolean);.
I reworked your code as follows and it now passes:
function destroyer(arr) {
for (var i = 0; i < arguments[0].length; i++){
for(var j = 1; j < arguments.length; j++){
if (arguments[0][i] === arguments[j]){
delete arguments[0][i];
}
}
}
return arr.filter(Boolean);
}
This solution above is quite similar to the Basic Code Solution, minus the use of Array.prototype.slice.call() to create an array of arguments.
Thank you @MarioAleixo! I had been scratching my head on this one for a while. Great explanation and you also helped me understand why we need to filter the Boolean values.
I really appreciate all the help I get in these guides, but I just wanted to say that the second hint threw me a bit of the scent on this challenge and that the ultimate solution was pretty much what I was thinking it was going to be before I read the hints. I know I should have tried to write the code as I saw it in my head before looking for hints, but just throwing it out there that maybe Hint 2 could be removed or changed because I donāt think there are many solutions that actually use those methods.
Again, thanks for all the help.
This is because he used the filter() method on the array arr. With this method it passes each and every element in the array arr to the variable val one at a time.
This is my solution. I decided to use some ES6 because I got a little bitter looking at all the extra code.
var args = Array.prototype.slice.call(arguments, 1);
return arr.filter(function(val){ return args.every(x => x !== val); });
I tried to use for loops as little as possible.
EDIT: Decided to change the empty arg array and for loop in favor of Array.prototype.slice.call(arguments, 1); that everybody has used. Never thought about it and did some research on it on the MDN.