I have a program where the user enters a sentence and the input is compared against an array when the user clicks the submit button the function "checkSentence() should be called but this does not happen. would really appreciate it if someone told me what's wrong wth my code

<script type="text/javascript">
var noun=["cat","mat","rat"];
var verb=["like","is","see","sees"];
var object=["me","the","a"];
var subject=["I","a","the"];
var sentence=subject.concat(verb,noun,object);
var userInput=document.getElementById("idTextArea").value;
function compareSentences(){
    if (userInput==sentence.length){
    alert("congratulations! Your sentence is correct");
    return false;
    }
    else if(userInput==null||userInput=="");
           {
           alert("your text was blank, please enter a valid sentence")
           }

    else
    alert("Your Sentence does not follow micro-english rules,Please enter a micro-English sentence in the form of a subject, verb and object");

    return false;

}
</script>


<body>
<div id="header">
<img src= alt="logo">
<br/>
<h1><font face="comic sans ms">Micro-English</font></h1>
</div>



<h1><font size="3" color="black" face="comic sans ms">Welcome to Micro English</font></h1>
<p>Micro English is a mini version of English with a limited vocabulary. You may only use the following</p>
<dl>
<dt>Nouns</dt>
<dd>Cat, Mat,Rat</dd>
<dt>Verbs</dt>
<dd>Like,is,see,sees</dd>
<dt>objects</dt>
<dd>me,the,a</dd>
<dt>Subjects</dt>
<dd>I,a,the</dd>
</dl>
<h2><font size="2" color="blue" face="comic sans ms">Please Enter a sentence in micro English in the box below</font></h2>

<form onsubmit="checkSentences();">
<p><font face="tempus sans itc">Please Enter a sentence in micro English</font></p>
<input type="text" name="input" id="userInput"/>
<input type="submit" value="go" onclick="compareSentences();"/>

</form>

Dani AI

Generated

A few concrete reasons the click/submit handler never behaves as expected: the page mixes different function names and element IDs, the input value is read before the DOM exists, a stray semicolon after an else if makes that branch always run, and userInput == sentence.length is comparing a string to a number (and therefore will not do what is intended). already flagged the function-name/logic issue, and asked for the revised code — the checklist below fixes those problems and keeps the validation simple and debuggable.

Suggested fixes and a minimal event-driven pattern to use (assumes the word arrays are declared once, above this script; add id="microForm" to the form if needed):

<script>
document.addEventListener('DOMContentLoaded', function () {
  var form = document.getElementById('microForm');
  form.addEventListener('submit', function (e) {
    e.preventDefault();
    var text = document.getElementById('userInput').value.trim();
    if (!text) { alert('your text was blank, please enter a valid sentence'); return; }
    var words = text.toLowerCase().split(/\s+/);
    // simple order check: subject, verb, noun
    if (words.length >= 3 && subject.indexOf(words[0]) !== -1 && verb.indexOf(words[1]) !== -1 && noun.indexOf(words[2]) !== -1) {
      alert('congratulations! Your sentence is correct');
    } else {
      alert('Your Sentence does not follow micro-english rules');
    }
  });
});
</script>

Quick debugging tips: open the browser console for syntax errors, remove stray semicolons after if/else if, get element values inside the handler (not at top-level), and use console.log() to inspect words and membership checks. This addresses the issues in the original post by and follows the naming/behavior note from .

Recommended Answers

All 4 Replies

You have reference to two different functions. I only see this function:

 function compareSentences()

I don't see a function called checkSentence().

Modified that thanks. but the function call still won't happen

Member Avatar for Member #1042208

Modified that thanks. but the function call still won't happen

Post your modified code.

You have all sorts of syntax errors and logic problems in your code. here is an example where the function will run when you click on submit.

<!DOCTYPE html>
<html>
<head>
<script>
var noun=["cat","mat","rat"];
var verb=["like","is","see","sees"];
var object=["me","the","a"];
var subject=["I","a","the"];
var sentence=subject.concat(verb,noun,object);
var userInput;
function compareSentences(){
    userInput = document.getElementById("userInput").value;
    if (userInput==sentence.length){
    alert("congratulations! Your sentence is correct");
    return false;
    }
    else if(userInput==null||userInput=="")
           {
           alert("your text was blank, please enter a valid sentence");
           }
    else {
    alert("Your Sentence does not follow micro-english rules"...);
    return false;
}
return false;
}
</script>
</head>
<body>
<div id="header">
<img src= alt="logo">
<br/>
<h1><font face="comic sans ms">Micro-English</font></h1>
</div>
<h1><font size="3" color="black" face="comic sans ms">Welcome to Micro English</font></h1>
<p>Micro English is a mini version of English with a limited vocabulary. You may ... </p>
<dl>
<dt>Nouns</dt>
<dd>Cat, Mat,Rat</dd>
<dt>Verbs</dt>
<dd>Like,is,see,sees</dd>
<dt>objects</dt>
<dd>me,the,a</dd>
<dt>Subjects</dt>
<dd>I,a,the</dd>
</dl>
<h2><font size="2" color="blue" face="comic sans ms">Please Enter a sentence ...</font></h2>
<form onsubmit="return compareSentences();">
<p><font face="tempus sans itc">Please Enter a sentence in micro English</font></p>
<input type="text" name="input" id="userInput"/>
<input type="submit" value="go" />
</form>
</body>
</html>

However, with regard to this condition if (userInput==sentence.length) , I dont undertand how you expect for that condition to ever be true.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.