title: Javascript: Generic get next item in array
body: <p>I am trying to make a JavaScript function that will search an array of strings for a value and return the next string. For example, if an array is built such that an item is followed by its stock code, I want to search for the item and have the stock code written.</p>
  
  <pre><code>var item = (from user input); //some code to get the initial item from user
  function findcode(code){
    var arr = ["ball", "1f7g", "spoon", "2c8d", "pen", "9c3c"]; //making the array
    for (var i=0; i&lt;arr.lenth; i++){  //for loop to look through array
      arr.indexOf(item);  //search array for whatever the user input was
      var code = arr(i+1); //make the variable 'code' whatever comes next
      break;
    }
  }
  document.write(code); //write the code, I.e., whatever comes after the item
  </code></pre>
  
  <p>(I'm sure it's obvious I'm new to JavaScript, and while this is similar to a number of other questions I found, those seemed to have more involved arrays or more complex searches. I can't seem to simplify them for my needs.)</p>