Skip to content

Instantly share code, notes, and snippets.

Created December 8, 2015 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/c9a59250c3e85bb8213e to your computer and use it in GitHub Desktop.
Save anonymous/c9a59250c3e85bb8213e to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/robertsheacole 's solution for Bonfire: Where do I belong
// Bonfire: Where do I belong
// Author: @robertsheacole
// 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) {
//Push num into the arr array.
arr.push(num);
//Reorder arr from lowest to highest number.
arr.sort(function(a, b){
return a - b;
/*.sort() runs the function to compare numbers instead of the default behavior of sort
which converts inputs to strings then compares.*/
});
//Find index of num
//Loop through arr
for (var i = 0; i < arr.length; i++){
//if num === arr return index
if (arr[i] === num){
var indexNum = arr.indexOf(arr[i]);
return indexNum;
}
}
}
console.log(where([40, 60], 30));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment