Javascript Tutorial to find a number is even or odd


In this tutorial i am going  to solve an assignment "Find a number even or odd using pure javascript" . We are using HTML5, you can use HTML 4 , code will run fine. First We have to check the give input is string or integer, so we use parseInt() function then created a variable check ,
check = no % 2 will check reminder is 0 or not, if reminder is 0 means input is an even number otherwise odd.,Below Code we have also provide a live Demo. If you have an query or suggestion then pass it through comment .

Assignment - Find a number even or odd using pure javascript.

<!DOCTYPE html>
<head>
<meta charset="utf-8"/>
<title>Find Even and ODD</title>
</head>
<body>
<form action="" method="post">
<input type="text" id="no" />
<input type="submit" id="check" value="check"/>
</form>
<p id="result"></p>
<script type="text/javascript">
document.getElementById("check").onclick = function() {
    var no = document.getElementById("no").value;
    myFunction(no);
   return false;
};    
function myFunction(no) {
    var check;
    no = parseInt(no);
    check = no % 2;
  if(check == null || isNaN(check) == true){
    document.getElementById("result").innerHTML = '<span style="color:red">Required a Valid Integer</span>';
  }else if(check==0){
     document.getElementById("result").innerHTML = '<span style="color:green">EVEN</span>' ;
  }else{
     document.getElementById("result").innerHTML = '<span style="color:blue">ODD</span>';
  }   
}
</script>
</body>
</html>

Demo

  

 


 Category : Javascript   Posted By : Sanjay   on 2015-08-21 15:43:30


Related Post

blog comments powered by Disqus

Login



Categories

May I Know You

  • System » Unknown
  • Browser » Unknown
  • IP Address » 54.162.250.227
  • 14 Online