Skip to content

Instantly share code, notes, and snippets.

@AliceWonderland
Forked from anonymous/5.5 My LastIndexOf.js
Created April 12, 2017 05:29
Show Gist options
  • Select an option

  • Save AliceWonderland/0683a94d6fae8f2f93b8a511d82ae16a to your computer and use it in GitHub Desktop.

Select an option

Save AliceWonderland/0683a94d6fae8f2f93b8a511d82ae16a to your computer and use it in GitHub Desktop.
5.5 My LastIndexOf created by smillaraaq - https://repl.it/HEWJ/1
function myLastIndexOf(arr, searchItem, fromIndex){
var startIndex=(fromIndex)?fromIndex:arr.length-1;
var output= -1;
for(var i=startIndex;i>=0;i--){
if(arr[i]===searchItem){
return output= i;
}
}
return output;
}
console.log(myLastIndexOf([1,2,1], 1)); // => 2
console.log(myLastIndexOf([1,2,1], 1, 1)); // => 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment