Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duggiemitchell/6c58b13875464158b566 to your computer and use it in GitHub Desktop.
Save duggiemitchell/6c58b13875464158b566 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/duggiemitchell 's solution for Bonfire: Where do I belong
// Bonfire: Where do I belong
// Author: @duggiemitchell
// Challenge: http://www.freecodecamp.com/challenges/bonfire-where-do-i-belong
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function where(arr, num) {
arr.push(num);
arr.sort();
var newArr = arr.sort(function(a,b) {
return a - b;
});
return newArr.indexOf(num);
}
where([2, 20, 10], 19);
@duggiemitchell
Copy link
Author

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

First, push(num) to the end of array

I thought it would be as simple as just using sort() on the array, but it didnt do anything.

So, the sortCompare function is needed to sort numbers.

Lastly, just simply needed to return the indexOf num within the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment