** UPDATE:
jsfiddle.net/terra823/WA6sX
I'm basically looking to take text INPUT and compare the INPUT text against an array called magic_words. If it matches, turn the screen blue, if it doesn't match turn the screen green. So far, when I have a match it turns the screen blue, but it doesn't seem to continue comparing after an initial match is made. If anyone know's a better way of writing the functions -- maybe with regular expressions-- I'd greatly appreciate the help!
** LEGACY POST:
I'm working on some Javascript code that takes what the user types and compares it to an array I have. If it matches against certain words, it triggers a change. That part of the code I have working, however, after said event is triggered, I'd like the page to return to the default state. In other words, user begins page at default state, during the course of writing he triggers an event by typing a specific word from the array, once he continues typing he returns to the default state. Here's what I have for that function:
function handleChange(){
var newText=input.value;
if (newText==oldText)return; else oldText=newText;
set(reversed, reverse(newText));
set(count, 'You entered '+newText.length+' characters.');
set(orig, newText);
// Array of magic words
var magic_words = ["cool", "brother", "fun", "sister", "apple", "orange"];
// Screen BG
var box = document.getElementById("box");
// splitEnds is taking newText and spliting whole strings of entered text into words by spliting the string at SPACE
var splitEnds = newText.split(" ");
// screen is initially white
var color = "white";
// When user begins turn screen green just to indicate user has begun
if(newText != " ") {
color = "green";
}
// Look through array for all magic_words
for (var i = 0; i < magic_words.length; i++) {
// Look through all INPUT string text that's been divided into seperate words
for(var x = 0; x < splitEnds.length; x++){
// if the user has begun typing and they type a magic_word turn screen blue
if(color == "white" || color == "green")
if (magic_words[i] == splitEnds[x]) {
color = "blue";
break;
}
// WHAT I NEED IS TO TELL THE FUNCTION TO KEEP ANALYZING THE TEXT TO SEE IF IT MATCHES. RIGHT NOW IT STICKS ON BLUE
}
}
box.style.backgroundColor = color;
}
Appreciate any help.
Thanks and best,