Skip to content

Instantly share code, notes, and snippets.

@SerhiiLihus
Created December 6, 2015 14:40
Show Gist options
  • Save SerhiiLihus/a61afc10e7ae74c34315 to your computer and use it in GitHub Desktop.
Save SerhiiLihus/a61afc10e7ae74c34315 to your computer and use it in GitHub Desktop.
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted.
// Bonfire: Where do I belong
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-where-do-i-belong#?solution=function%20where%28arr%2C%20num%29%20{%0A%20%20arr.sort%28function%20%28a%2Cb%29{%0A%20%20%20%20return%20a%20-%20b%3B%0A%20%20}%29%3B%0A%20%20var%20i%20%3D%200%3B%0A%20%20while%20%28%20num%20%3E%20arr[i]%29%20i%2B%2B%3B%0A%20%20return%20i%3B%0A}%0A%0Awhere%28[40%2C%2060]%2C%2050%29%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function where(arr, num) {
arr.sort(function (a,b){
return a - b;
});
var i = 0;
while ( num > arr[i]) i++;
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment