Conditional statements and Comparisons

This code introduces the if statement, a powerful programming tool. Here, we use if to check the information entered on a form. Note the double equals sign (= =) tests equality, whereas a single equals sign reassigns a variable.


Put this code into the body of your document where you want it to appear:
<form name="ifForm">
  Please enter the word 'computer' (without the quotes) below:<br>
  <input type="text" name="theWord">
  <input type="button" value="Check Me" onClick="checkWord(ifForm.theWord.value)">
</form>

You also need to put the following code in the header of the document (somewhere between <HEAD> and </HEAD>):
<script language="JavaScript"><!--
function checkWord(text1){
  if (text1=='computer'){//compare the input string with the string 'computer'
    alert('You were correct!');//this line executed if above statement was TRUE
  }
  else {
    alert('Incorrect! You wrote ' +text1+ '. Please try again');//this line executed if the statement was FALSE
  }
}
// --></script>

The code is implemented below:

Please enter the word 'computer' (without the quotes) below: