0

** 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,

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
terra823
  • 92
  • 1
  • 10
  • what's the default state? – Mark Graham Feb 26 '13 at 22:47
  • i'm sorry i should have mentioned. page is white by default but turns green after any text is entered, so it should return to green as the user continues to type. – terra823 Feb 26 '13 at 22:48
  • Is the default state = white? On pageload, the background is white, right - then the user types in something, background turns blue.. THEN you want it to turn green again? – james emanon Feb 27 '13 at 02:27
  • hey, i can see how this is confusing. i put together a js fiddle: http://jsfiddle.net/terra823/WA6sX/ when the user types anything the screen turns from white to green just to show the user is typing. the code turns the screen blue when magic words are entered. my function breaks when it needs to turn from blue back to green. i'm trying to create a live analysis of comparing input to word in an array. – terra823 Feb 27 '13 at 02:42
  • http://stackoverflow.com/questions/4790946/dynamically-displaying-input-values-using-javascript This worked for me in case anyone has a similiar problem. – terra823 Mar 02 '13 at 18:01

1 Answers1

0

I am really not sure what you are looking for, but just changing it back to green after you change it to blue.

          box.style.backgroundColor = color;
            setTimeout(function(){
                box.style.backgroundColor = 'green';
            },1000);
james emanon
  • 11,185
  • 11
  • 56
  • 97
  • close. i'm looking for it to take input and compare against the array. if it matches turn the screen blue if it doesn't 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. i appreciate the help! – terra823 Feb 27 '13 at 02:48