Challenge: Where Do I belong?

the popular adage – never go with the name of a thing – could not have gotten any more truer. This particularly sad-looking challenge packs enough punch to get all they happy javascript feelings right out of your guts and stomp it all over while you sit in a corner bellowing at the no fairytale ending of your Javascript career.

However, later (to my sheer pleasure) I found that the solution was rather painless if you thought outside the box (or bucket, if that’s where you  prefer to keep all of your javascript eggs) and lucky for us all, I’m here to save you all the pain of going through everything.

quoting the outline, first, as found in FCC

Outline

Return the lowest index at which a value (second argument) should be inserted into a sorted array (first argument).

For example, where([1,2,3,4], 1.5) should return 1 because it is greater than 1 (0th index), but less than 2 (1st index).

Here are some helpful links:

Array.sort()

Solution

the description for this problem is self-explanatory. you need to return the index at which the number(s)   from the second argument  should be added  in the first argument, but here’s the catch: in order to get the right index, you need to make sure that your number(s) is/are added at the right place. in other words, your array must be sorted in an ascending order before you start adding the numbers from your second arguments into it. Now I can think of no more less than 5 solution to this problem (but then again it’s just probably me) but here I will only share the one that’s the most simple and yet the most compact.

One thing more that merits to be mentioned here is that, as far as my earlier post on arr.sort() is concerned, It only sorts the alphabets for you. I didn’t go into the details of how you can get it to sort the numbers as well. nonetheless, It is not that hard to achieve, and only takes a custom function in its body to do the number sorting stuff. Again, for the sake of relevance, I won’t go into its details and directly quote the function below that actually does the number sorting:


function compare (a,b) {

return a - b ;

}

if you need to read up on how the above function works, follow the link to its MDN entry here

Code


function where(arr, num) { 

// pushing numbers (in the second argument) into arr (the first argument)

arr.push(num) ;

//sorting the whole array in an ascending order

arr.sort(function(a,b)

return a - b ;

}) ;

// returning the index of the number recently added

console.log(arr.indexOf(num)) ;

}

// logs 1

where([40, 60], 50, "") ;

// logs 2
where([10, 20, 30, 40, 50], 30);


// logs 0

where([5, 3, 20, 3], 3) ;


// logs 3

where([2, 5, 10], 15);


code in action

Other Possible Solutions

I will go and drop a pair of hints and see if you can work them out for yourself?

  1. you could do it the loopy way. sort the array first and loop through all the elements and return the index of the extra numbers added.
  2. since there is looping involved, you could make use of array.forEach() or arr.filter() or even array.every() and wait did I mention arr.map() ?

PS:

thanks to @Rafase282 and @rahul1992 from the FCC community for lending me their companies, time and wits needed for me to come up with this code.

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