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:
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);
Other Possible Solutions
I will go and drop a pair of hints and see if you can work them out for yourself?
- 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.
- 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.