Skip to content

Instantly share code, notes, and snippets.

@ejknapp
Created December 13, 2009 15:57
Show Gist options
  • Select an option

  • Save ejknapp/255467 to your computer and use it in GitHub Desktop.

Select an option

Save ejknapp/255467 to your computer and use it in GitHub Desktop.
/*
This is the JavaScript code for
"Lab 11: Searching an Array"
File: /unit5/labs/lab11searchAnArray.html
*/
function lab11Demo() {
// Your code goes in here.
var words;
var search = "not";
var index;
var currentWord;
var valueFound = false;
var output;
output = document.getElementById("outputParagraph");
words = ["Believe", "it", "or", "not", "", "the", "very"];
for (index = 0; index < words.length; index++) {
currentWord = words[index];
if (search === currentWord) {
valueFound = true;
break;
}
}
if (valueFound) {
//output the value and the index
output.innerHTML = "We found \"" + currentWord + "\" at index "
+ index;
} else {
//output "not found"
output.innerHTML = "not found";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment