How did I not think of that… I stared at the problem for 10 minutes thinking what to do with the arguments object and the best I could some up with was an empty array and a for loop to push all the args into that array.
Here is my solution. It looks quite different from those in the wiki. Could someone take a quick look and let me know if it’s acceptable?
function destroyer(arr) {
// Create two new arrays, one for the initial array and another for the characters to remove
var initialArr = arguments[0];
var toRemove = [];
// Push all other args to 'toRemove' array
for (i=1; i<arguments.length; i++) {
toRemove.push(arguments[i]);
}
// Filter out unwanted chars from the initial array
for (i=0; i<toRemove.length; i++) {
initialArr = initialArr.filter(function(num) {
return num != toRemove[i];
});
}
return initialArr;
}
Another solution of seek and Destroy
function destroyer(arr) {
// Remove all the values
for(var i =1 ; i<arguments.length; i++){
var min=i;
var max=arguments[min];
arr=arr.filter(function(value){
if(value===max){
value-=max;
return value;
}
else{
return value;
}
});
}
return arr;
}
the freecodecamp coding system throws a warning when i use the spread operator but it lets me use it in the code so i figure why not take advantage of it
I’ve been stuck on this exercise for days and I’m really struggling with the filter and callback methods.
So regarding the Basic Code Solution:
[code]function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < args.length; j++) {
if (arr[i] === args[j]) {
delete arr[i];
}
}
}
return arr.filter(Boolean);
}[/code]
I see what you’re doing with the two for-loops and I’ve attempted several solutions with a similar structure. My problem was always accessing the original arguments and going from there.
Could someone please explain to me what’s going on here:
Why not go for 2 arrays (one with the numbers we want to test against and one with the numbers to test with)? Why not use arguments[0]? What exactly does this line of code produce? What does “var args” actually look like? Is it:
?
I apologize if these seem like dumb questions, but after banging my head into a wall for a while I figured I needed to go back to the absolute basics and make sure I actually understand everything.
This is one of the solutions I attempted that does not work. I’m including it here because I think it illustrates what I struggle with:
function destroyer(arr) {
var array = Array.prototype.slice.call(arguments[0]);
var test = Array.prototype.slice.call(arguments[1]);
var newArray = [];
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < test.length; j++) {
if (array[i] !== test[i]) {
newArray.push(arr[i]);
}
}
}
return newArray;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
function destroyer(arr) {
// Remove all the values
var args = Array.from(arguments);
for (var i = 1; i < args.length; i++){
arr = arr.filter(function(ele) {
return ele !== args[i];
});
}
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
I passed the challenge with this code but I get the message ''functions declared within loops referencing an outer scoped variable may lead to confusing semantics". How do I rewrite my code to avoid this message? Don’t really understand what it is telling me.
function destroyer(arr) {
var toRemove = [];
var target = arguments[0];
for (i = 1; i < arguments.length; i++) {
toRemove.push(arguments[i]);
}
var results = target.filter(function(val){
for (i = 0; i < toRemove.length; i++) {
return toRemove.indexOf(val) < 0;
}
});
// Remove all the values
return results;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
This isn’t the most elegant solution, but I decided to use the tools I had already learned. I was stuck on this for a couple of hours yesterday getting lost in syntax. Today, on my morning walk, I though the problem through then came home and pseudocoded it out on paper. I came up with something different from the provided answers, but much simpler for me to wrap my head around. This solution works for any amount of arguments. Please let me know what I could improve.
function destroyer(arr) {
arr = Array.from(arguments); //-------- turn arr into an array containing all elements.
var arr1 = arr.shift(); //------------- cut the total array into arr1, leaving arr as the arguments.
var currentNum = []; //---------------- creates an array to store current argument to test.
while (arr.length > 0) { //------------ iterates through until arguments stored in arr are all gone.
currentNum = arr.splice(0, 1); //------ cuts current argument to test, leaving arr[0] as the next argument.
arr1 = arr1.filter(function(val) { //-- sets arr1 to filtered arr1.
return val != currentNum; //----------- arr1 becomes everything that doesn't match currentNum.
}); //--------------------------------- loops back to filter next argument.
}
return arr1; //------------------------ when all arguments have been filtered, returns filtered array.
}
Took me a rather long time to figure this out, my code looks like spaghetti code
function destroyer(arr) {
//Store unspecified array size to argArr using arguments.
//newArr is result
var argArr = Array.prototype.slice.call(arguments);
var newArr =[];
//Initialize var for filter function checks
var argLength = arguments.length;
var test = false;
//Filter function, sends each val from array, loops however many arguments needed
function seekDestroy(val){
for (let i =1; i<argArr.length; i++){
if (val==argArr[i]){
test = false;
break;
} else{
test = true;
}
}
return test;
}
newArr = argArr[0].filter(seekDestroy);
return newArr;
}
destroyer(["tree", "hamburger", 53], "tree", 53);
brief explanation:
Store arguments in argArr
Make newArr to store results
Filter function loops check array element in arr[0] with all values from nonarray elements (arr[1], arr[2], arr[3]…etc) arguments
Store conditional check and break loop if something is found
Return that value
EDIT doing solution again a few days later
function destroyer(arr) {
// Remove all the values
var arg = Array.prototype.slice.call(arguments);
return arr.filter(seekDestroy);
function seekDestroy(val) {
return !arg.includes(val);
}
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
similar to intermediate solution but not as efficient in that the includes uses the whole arg array rather than slicing off the first part of array
This is how I did it. If there were more than three elements I would have run into trouble though!
function destroyer(arr) {
//convert into a new array with all arguments
var args = Array.from(arguments);
//if the first element in the new array is an array object:
if (typeof args[0] === ‘object’) {
//splice to a new array containing that first element
var splicedArr = args.splice(0,1);
}
//argValue is the current element of the splicedArr array
function checkVals (argValue) {
if (argValue === args[0]) {
return false;
}
if (argValue === args[1]) {
return false;
}
if (argValue === args[2]) {
return false;
}
else {
return true;
}
}
//if checkVals returns true the element of splicedArr will be kept
var filteredArr = splicedArr[0].filter(checkVals);
var arg=0;
function different(value){
return value !== arg;
}
function destroyer(arr) {
// Remove all the values
var destroy =arr;
for (i=1;i<arguments.length;i++){
arg = arguments[i];
destroy = destroy.filter(different);
}
return destroy;
}
function destroyer(arr) {
var args = Array.from(arguments); //create an array in args with all function arguments
for(i = 0; i < args[0].length; i++){ //loop through the first array in args
for (j = 1; j < args.length; j++){ //then loop through second array in args
if(args[0][i] === args[j]){ //if value in args[0] = any value of array in args[j], delete.
delete args[0][i];
}
}
}
return args[0].filter(Boolean); //filter values, then return values when true
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
P.S: I had to look up solutions for this one. It seems to be quite hard for beginners. Credit to Christine Javier in her video https://www.youtube.com/watch?v=Gz9ECvouuR0 .