Challenge: Seek and Destroy

Sorry for such a star war-ish name for the challenge we are up against today. I didn’t come up with it myself, It’s just how it was named on FCC.

the solution of this problem makes heavy use of arguments object so make sure you are comfortable with it before you bring your wits to seek or destroy anything for you. (I certainly don’t want the blood of any innocuous neurons on my conscious.)

Outline

You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

Here are some helpful links:

Arguments object
Array.filter()

Solution

this problem comes in this format:

destroy([argument 1], [argument 2])

as you might have guessed it, some of the elements in argument 1 are similar to the elements in argument 2. we need to come up with a function that takes both the arguments into account, and removes the twins present in the both arguments.

since there’s going to be filtering involved, you can count on me to use the array.filter() method to get to the end of this problem. Before that, we will need to make sure we have something that has access to all the arguments (both the argument 1, and argument 2), that’s your cue for using arguments object , and since we need to check for duplicates, we will also be using an array method called, arr.indexOf() –  arr.indexOf() checks for the first occurrence of an element in an array and returns its index. If there is no such element present, it returns -1. In other words, -1 indicates that the said element doesn’t exist in the array.

enough with the theory.. let’s get to the battleground !

Code


function destroyer(arr) {

// args = all the arguments in the function from index 1. i.e. (2,3," ")
var args = Array.prototype.slice.call(arguments, 1);

//passes each element  in the array (i.e. 1,2,3,1,2,3) into the indexOf()
return arr.filter(function(e) {

//if an element in args doesn't exist in arr, keep it otherswise filter it out
return args.indexOf(e) === -1
});
}

//returns 1, 1

destroyer([1, 2, 3, 1, 2, 3], 2, 3, "") ;

//returns 1,5,1

destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) ;

//returns 1

destroyer([3, 5, 1, 2, 2], 2, 3, 5) ;

code in action

array.prototype.sort()

hello guys! long time no code? well, I lost my focus for a while, but then I’m back here so let’s do a quick post here. there is nothing fancy going on here. just  a simple array method() that can do a world of wonders. chances are, if you have just began to learn JS, then,  this might really come in handy.

Definition:

someArray.sort() is an array method and takes one array into an account and sorts its element up using its own dark magic defined somewhere deep in JS. I mean, you don’t really have to do the array’s element sorting as long as you know how to use sort(). you simply need an array and call it on and it will result into sorted element just like that. one thing to note here is that, the way it “sorts” the elements or the logic behind the sorting is not necessarily stable. it’s purely based on the Unicode to code points so you will have to get ready for funny results now and then, but it should work fine for most of the time. again, it sorts both alphabets and digits accordingly.

Syntax:

arr.sort()    ;    //where arr is any given arr you want the sort() to work on

Examples:

let’s whip up a teeny weeny function that takes a string and sort it up for us;


function sorting(text) {

//turning string into array for sort() to do its sortWork on it

var getArray = text.split(" ") ;

// calling sort() on our newly turned getArray[]

var sort = getArray.sort();

//logging the sorted results

console.log(sort);

}

sorting("cat bat apple");   //logs "apple bat cat"
sorting("a g c e d z");    // logs "a c d e g z"
sorting("1 5 7 4  6 5 3"); // logs "1 3 4 5 5 6 7"

Proof:

usually, I provide some kind of link here with my  code in action, but due to the soporific nature of this problem you will have to do that for yourself  since I’m already asleep on my feet and might not be able to even complete my senten