Created
December 8, 2015 15:50
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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