Advertisement
ipsBruno

(Javascript) strInArr

Jul 29th, 2012
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Copyright (c) 2012 [iPs]TeaM
  3.  *  Bruno da Silva (email@brunodasilva.com)
  4.  *  Checar e pegar a posição de uma string em uma array
  5.  *
  6.  * www.brunodasilva.com
  7.  * www.ips-team.forumeiros.com
  8. */
  9.  
  10. <script>
  11.  
  12. var fruits = ["Banana", "Orange", "Apple", "Mango"];
  13.  
  14. alert(strInArr(fruits, "Banana"));  // 0
  15. alert(strInArr(fruits, "Kiwi"));    // -1 (not exist in fruits)
  16. alert(strInArr(fruits, "Orange"));  // 1
  17.  
  18. function strInArr(arr, str) {
  19.     for(i = 0, j = arr.length; i < j; i++) {
  20.  
  21.         if(arr[i] == str) {
  22.             return i ;
  23.         }
  24.     }
  25.     return -1;
  26.    
  27. }
  28.  
  29. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement