I help writing a code that will remove all elements from the initial array that are of the same value as these arguments. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I help writing a code that will remove all elements from the initial array that are of the same value as these arguments.

I've tried taking several different approaches but I can't seem to get it. Here's my code. function destroyer(arr) { var args = Array.prototype.slice.call(arguments[0], arguments[1]); return args; } destroyer([1, 2, 3, 1, 2, 3], 2, 3); </p> And this <p> function destroyer(arr) { var args = Array.prototype.slice.call(arguments[0], arguments[1],arguments[2]); // using 3 arguments in the later call ^^ return args; } // to store (or directly use) the returned value: var result = destroyer([1, 2, 3, 1, 2, 3], 2, 3); alert(result);

15th Jul 2017, 3:03 PM
Timothy
5 Answers
+ 2
/* Why have you delete my answer? I spend time for you... it's not respectuous ^^ If the answer doesn't satisfied you, rather explain why :P Anyway, as I have it still it in my clipboard, and as I guess now what's you're expecting, this is a new version of your fixed code: */ function destroyer(arr,list) { /* this is absolutly not the good way to use slice() method ;) (confusing me at my 1st attempt) var args = Array.prototype.slice.call(arguments[0], arguments[1]); */ var j, i = list.length; var new_arr = arr.slice(0,arr.length); while (i--) { do { j = new_arr.indexOf(list[i]); if (j!=-1) new_arr.splice(j,1); } while (j!=-1); } return new_arr; } // you need to store (or directly use) the returned value, and rather use an array to pass the list of elements to retrieve... var result = destroyer([1, 2, 3, 1, 2, 3], [2, 3]); console.log(JSON.stringify(result));
15th Jul 2017, 4:14 PM
visph
visph - avatar
+ 2
/* My code was good working and outputing some result, even if it wasn't what you expected... Easy to say that after deleting it, but as I previously said, I have still this first version of my fix: */ // I suppose you want to use destroyer() with first argument as array to transform it (by returnin new one, without modifying the original array) by slicing from second argument as index of start item upon third argument as index of end item (not included) function destroyer(arr) { var args = Array.prototype.slice.call(arguments[0], arguments[1],arguments[2]); // you're using 3 arguments in your later call ^^ return args; } // you need to store (or directly use) the returned value: var result = destroyer([1, 2, 3, 1, 2, 3], 2, 3); alert(result);
15th Jul 2017, 4:21 PM
visph
visph - avatar
0
I don't think you can make functions with an unspecified number of arguments--I've never managed. Try putting the arguments into an array so that they are all just a single argument. Then loop through the array, testing and removing matches. You need to loop from the last key to the first (0), because when you delete elements the following element will adopt its key. I've used .splice() to remove the elements. This will modify the original array; so, if you want the original unchanged, you'll need to copy it at the start of the function and process the copy instead. If you want an array of the elements you are removing, make a new array before the loops and use newArr.push(arr.splice(x, 1)); inside the loop instead. function destroyer(arr, arg){ for (var x = arr.length - 1; x >= 0; x--){ for (var k = arg.length - 1; k >= 0; k--){ if (arr[x] === arg[k]){ arr.splice(x, 1); } } } return arr; } var result = destroyer([1, 2, 3, 2, 1], [2, 3]);
15th Jul 2017, 3:43 PM
James
James - avatar
0
@visph Sorry about that I thought I understood what you were saying but I couldn't write an adequate code. I thought it would be unruly of me to bother you again. Thanks for the help I finally got it. I really appreciate people like you.
15th Jul 2017, 4:26 PM
Timothy
- 1
Your code isn't working for some reason. When I put it in it outputs nothing.
15th Jul 2017, 4:15 PM
Timothy