Integrating a Filter() with an Alert Snippet

Hi Everybody,

I suspect this is not so difficult, but it’s driving me crazy.

After the filter method leaves either a string or an empty string, I want an alert to reflect which came out.

Here is the closest I have gotten:

function wordCheck(wordInput) {
    var wordList = ["sneeze bleshu","fart omg"];
    var wordResult = "";
    var wordInput = wordInput.toLowerCase().trim();
    var filtered = "";

    filtered = wordList.filter(function(val){
       return val == wordInput;
    });
      return wordResult = filtered.join("");
}

function wordAlert() {
    if (filtered == "") {
            alert("Try Again, Kiddo!");
    }
    if (filtered !== "") {
            alert("Good job!");
    }
}

wordCheck("sneeze bleshu");
    
wordAlert();

Thanks for even looking at this!

Regards,

andy

The filtered variable declared in the wordCheck function is only visible within that function. The wordAlert function assumes that the filtered variable in it is a global variable (which I’m assuming doesn’t exist). You can modify your code such that the wordAlert function accepts a string and checks its value.

function wordCheck(wordInput) {
  ...
}

function wordAlert(filtered) {
  if (filtered == '') {...}
  if (filtered !== '') {...}
}

var filtered = wordCheck();
wordAlert(filtered);

You may also want to modify the comparisons in the wordAlert function, because there are certain values which will trigger both alert calls to fire. Probably change it to something like

if (filtered !== '') {
  alert('Good job!');
} else {
  alert('Try again');
}

Thank you @kevcomedia. That input was very helpful.

There were two other helpful guys in Stack Overflow, too. I used the same title, FYI.

I was amazed to get so much help so quickly!

Unfortunately, I don’t think I described the situation well enough.

There is a form in my HTML with a text box and a check button:

<input id = "textbox" type = "text" name = "word_pair"><br>
<input id = "button" type = "button" value = "Check This!" onclick = "wordCheck();">

Students have a list of adj. and nouns to match. They choose two, input them, and click the button.

I want wordCheck() to do 3 things: (1) check that the words match the pair list & if they do, (2) cross off the words on the list; then (3) drop the words into the value = “” section of a text box, where they will write sentences based on the words. eg. “thirsty water” -> “He looks thirsty. Give him some water.”

I was thinking that if I could solve a simple alert problem, then adding other functionality would be simple.

With your help, the code got much farther in the PythonTutor.com code checker, but now Google Chrome hates my .toLowerCase, saying:

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

Or, if I comment that line out, the alerts fire off almost randomly.

You guys got me much farther, to be sure; and that was a great help.

I just thought I’d give you a thorough update, since you were so thoughtful.

If I get anywhere with this, I’ll let you know.

THX!