Thursday, September 24, 2015

Odd and Even Number Checker in JavaScript

In this article I will share with you a program that will check if the given number by our user is an Odd or Even Number using Javascript as our  programming language. I included a button to clear the text field and validation code to check for invalid values being given by our end user.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.






Sample Program Output


Program Listing

<html>
<head>
<style>
body {
  background-color:lightgreen;
  font-family:arial;
  font-size:25px;
  color:blue;
  }
  label {
    float: left;
    width: 30%;
    text-align: left;
    margin-right: 1em;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
}
.form-field-no-caption {
    margin-le

input[type="radio"]{
 float:left;
}
</style>
<meta charset="UTF-8">
<title> Odd and Even Number Checker in JavaScript</title>

 <script type="text/javascript">

        function find_odd_even() {
        var input = document.getElementById("userInput").value;
            var number = parseInt(input);
                    
            if (isNaN(number)) {
                alert("Please enter a number.");
                document.getElementById("userInput").value="";
                document.getElementById("result").innerHTML =  "";
                document.getElementById("userInput").focus();
                
            }
                     
            else if (input.length === 0) {
                alert("Please enter a valid input");
                document.getElementById("userInput").focus();
               
            }
           
            else if (number % 2 == 0 ) {
                document.getElementById("result").innerHTML = number + " is even number.";
            }
            else {
            document.getElementById("result").innerHTML = number + " is odd number.";
              }
           }
        
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h4> Odd and Even Number Checker in JavaScript </h4>
  Enter a Number 
    <input type="text" id="userInput" autofocus value = "" />
    <input type="submit" value="Check" 
    title="Click here to check if the given number is an ODD or EVEN number."
    onclick="find_odd_even()" />
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br>
  <p id="result"></h1>
</body>
</html>



Basic Math Calculator in JavaScript

Learning how to write a code in JavaScript is a wonderful felling why because almost all web applications uses JavaScript in their websites. In this article I will show you how to create a program using JavaScript a simple math calculator that will perform basic arithmetic operations using radio buttons to select addition, subtraction, multiplication and division operations. I also includes error trapping routines to check and block invalid entry values by our user. I hope you will find my work useful in your programming assignments and projects using JavaScript.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.







Sample Program Output


Program Listing


<html>
<head>
<style>
body {
  background-color:lightgreen;
  font-family:arial;
  font-size:25px;
  color:blue;
  }
  label {
    float: left;
    width: 30%;
    text-align: left;
    margin-right: 1em;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
}
.form-field-no-caption {
    margin-le


input[type="radio"]{
 float:left;
}
</style>
<meta charset="UTF-8">
<title>Basic Math Calculator</title>

<script type="text/javascript">

 function clear_all()
 {
   document.getElementById('math').value1="";
   document.getElementById('math').value2="";
   document.getElementById('math').value1.focus();
 }


function do_math()
{
     var val1 = document.getElementById('math').value1.value;
     var val2 = document.getElementById('math').value2.value;
  
     var sum = parseInt(val1) + parseInt(val2);
     var subtract = parseInt(val1)  - parseInt(val2);
     var multiply = parseInt(val1) *  parseInt(val2);
     var quotient = parseInt(val1) / parseInt(val2);

      if (val1=="" || val2=="") {
          alert(" Text fields cannot be empty.");
         document.getElementById('math').value1="";
         document.getElementById('math').value2="";
         document.getElementById('math').value1.focus();
          }
        
       else if(document.getElementById('1').checked && val1=="" && val2==""){
          document.getElementById('math').value1="";
          document.getElementById('math').value2="";
          document.getElementById('math').value1.focus();
          }
        else if(document.getElementById('2').checked && val1=="" && val2==""){
          document.getElementById('math').value1="";
          document.getElementById('math').value2="";
          document.getElementById('math').value1.focus();
          }
        else if(document.getElementById('3').checked && val1=="" && val2==""){
          document.getElementById('math').value1="";
          document.getElementById('math').value2="";
          document.getElementById('math').value1.focus();
          }
        else if(document.getElementById('4').checked && val1=="" && val2==""){
          document.getElementById('math').value1="";
          document.getElementById('math').value2="";
          document.getElementById('math').value1.focus();
          }
        else if (document.getElementById('1').checked) {
         alert("The sum of " + val1 + " and " + val2 + " is " + sum+".");
   
         }
     
       else if (document.getElementById('2').checked) {
    alert("The difference of " + val1 + " and " + val2 + " is " + subtract+".");
        }
      
      else if (document.getElementById('3').checked) {
    alert("The product of " + val1 + " and " + val2 + " is " + multiply+".");
        }
      
      else if (document.getElementById('4').checked) {
    alert("The quotient of " + val1 + " and " + val2 + " is " + quotient +".");
        }
     return false;
}
 </script>
 </head>
 <body><br>
 Basic Math Calculator<br><br>
<form id="math" method="post" action="#"  onSubmit="do_math()" > 
<label> Enter first value </label>   <input type="text" name="value1" value="" > <br>
<label> Enter second value </label>  <input type="text" name="value2" value="" > <br>
<br>
Arithmetic Operators
<br><br>
<input type="radio" id="1"  name="optr" value="1"> <label> Addition </label><br>
<input type="radio" id="2"  name="optr" value="2"><label>Subtraction</label> <br>
<input type="radio"  id="3" name="optr" value="3"><label>Multiplication </label><br>
<input type="radio" id="4"  name="optr" value="4"><label>Division</label><br>
 <br>
<input type="submit"  value="Compute" 
title="Click here to see the result."/>
<input type="submit"  onclick="click_all()" value="Clear" 
title="Click here to clear the text fields."/>
</form>
</body>
</html>




Tuesday, September 22, 2015

Text Box and Radio Buttons in JavaScript

In this article I would like to share with you how to use text box and radio buttons using Javascript to accept user input values and process it along the way.  What does the program will do is to ask the users name and then the user will select which  programming language he or she prefer to use and then by choosing the submit button the values given by the user will be process and will display user alert message box of JavaScript.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.





Sample Program Output


Program Listing


<!doctype html>
<html>
<head>
<style>
body {
  font-family:arial;
  font-size:25px;
  color:blue;
  }
  

label{
 color:red;
margin-left: 15px;
font-size:20px;
vertical-align: middle;
}
input[type="radio"]{
 float:left;
}
</style>
<meta charset="UTF-8">
<title>Text Box and Radio Buttons in JavaScript</title>

<script type="text/javascript">

function checkValue() 
{

     var form = document.getElementById('formBuzType'); 
     var form1 = document.getElementById('formBuzType').inputbox.value;
     var f = form1.toUpperCase();     

     for(var i = 0; i < form.buztype.length; i++)
     {
          if(form.buztype[i].checked)
          {
          var selectedValue = form.buztype[i].value;
          }

     }

      if (form1.length == 0 ) {
     
    alert("Name Cannot be empty");
    form1.focus();
      }
      else if (form.length == 0 ) {
     
      alert("Please select a check box");
      form1.focus();
        }
      else  if (selectedValue==1) {
      alert("Hi " + f + " You have selected Java.");
      }
     
     else if (selectedValue==2) {
    alert("Hi " + f + " You have selected C.");
      }
     else if (selectedValue==3) {
    alert("Hi " + f+ " You have selected C++.");
      }
     else if (selectedValue==4) {
    alert("Hi " + f + " You have selected C#.");
  }

 else if (selectedValue==5) {
alert("Hi " + f + " You have selected Pascal.");
  }
 else if (selectedValue==6) {
alert("Hi " + f + " You have selected BASIC.");
  } 
   
   return false;
}


</script>


</head>

<body>

<form id="formBuzType" method="post" action="#" onSubmit="return checkValue();" > <!-- you can pass the form here as well by doing: return checkValue(this);  -->
Enter your name     <INPUT TYPE="text" NAME="inputbox" VALUE="" required><P>
<input type="radio" id="1"    name="buztype" value="1"> <label> Java </label><br>
    <input type="radio" id="2"     name="buztype" value="2"><label> C </label> <br>
    <input type="radio"  id="3"    name="buztype" value="3"><label>C++ </label><br>
 <input type="radio" id="1"    name="buztype" value="4"><label>C# </label><br>
    <input type="radio" id="2"     name="buztype" value="5"> <label>Pascal</label> <br>
    <input type="radio"  id="3"    name="buztype" value="6"><label>BASIC </label><br>
   <br>
<input type="submit" value="Submit" title="Click here to see the result."/>
</form>
</body>
</html>



Palindrome in JavaScript

In this article I will share with you a sample program that demonstrate the concept of Palindrome what is a palindrome? a palindrome can refer to a series of numbers or words that when we read forward and backward the spelling is the same examples of palindrome is ANA, MADAM, RADAR and many others. I implement the palindrome concept using JavaScript as my programming language. The code is very simple and easy to understand ideal for those people that are new in JavaScript programming.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.




Sample Program Output


Program Listing

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Palindrome in JavaScript</title>
<style>
b {
  font-family:arial;
  color:blue;
  font-size:18px;
  }
  
 input[type="text"] {
  display: block;
  margin: 0;
  width: 35%;
  font-family: arial;
  font-size: 18px;
  appearance: none;
  border-radius: 2px;
  box-shadow: 10px 10px 7px #888888;
}
input[type="text"]:focus {
  outline: none;
}

input[type=button] {
padding:12px 31px; 
background:yellow; border:10px;
cursor:pointer;
-webkit-border-radius: 12px;
box-shadow: 10px 10px 7px #888888;
border-radius: 5px; 
font-family: arial;
font-size: 18px;
}
</style>
</head>
<body bgcolor="lightgreen">
<script>
function start_palindrome()
{
var strings = document.getElementById('userInput').value;
var result = palindrome_checker(strings);
alert('The String is "'+strings +'"  is '+result+'.');
}
function palindrome_checker(strings)
{
var orignal_word;
var reverse_word = strings;
strings = strings.toLowerCase();
orignal_word = strings;
   strings = strings.split(''); 
strings = strings.reverse(); 
strings = strings.join(''); 
if(orignal_word == reverse_word)
   {
return 'A PALINDROME'; 
else
{
return 'NOT A PALINDROME';
}
}
function clear_now()
{
document.getElementById('userInput').value="";
document.getElementById('userInput').focus();
}  
</script>
<b> Palindrome in JavaScript </b> <br><br>
<form name="myform" action="">
<b>Please enter a word or a string</b><br><br>
<input type='text' id='userInput' placeholder="enter text here" value='' size="30" required/>
<br><br>
<input type='button' onclick='start_palindrome()' title="Click here to count the vowels and consonants" value='OK'/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type='button' onclick='clear_now()' title="Click here to clear the textbox." value='CLEAR'/>
</form>
<br>
</body>
</html>



Sunday, September 20, 2015

Vowels and Consonants in Javascript

In this article I will show you how to count the number of vowels and consonants in Javascript what the program will do is to ask the user to enter a word or string and then the program will count the number of vowels and consonants given by the user.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.





Sample Program Output


Program Listing

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Vowels and Consonants Counter</title>
</head>
<body bgcolor="lightgreen">
<SCRIPT> 
function count_now()
 {
    var TextVar = document.getElementById('userInput').value;
    
    if (TextVar.length==0) {
     document.getElementById('userInput').focus();
     alert("Sorry cannot be empty");
     }
    else { 
    document.getElementById('word_string').innerHTML =  "The word or string is " 
                       + "<font color='red'>" +TextVar + ".</font>";
    document.getElementById('vowels').innerHTML =  "    Number of Vowels      :  " 
                     + vowel_count(TextVar);
    document.getElementById('consonants').innerHTML = " Number of Consonants :  " 
                     + consonants_count(TextVar);
     }                
}

function clear_now()
{
 document.getElementById('userInput').value="";
 document.getElementById('word_string').innerHTML="";
 document.getElementById('vowels').innerHTML="";
 document.getElementById('consonants').innerHTML="";
 document.getElementById('userInput').focus();
}  

function vowel_count(str1)
{
  var vowel_list = 'aeiouAEIOU';
  var vcount = 0;
  
  for(var x = 0; x <str1.length ; x++)
  {
    if (vowel_list.indexOf(str1[x]) !== -1)
    {
      vcount += 1;
    }
  
  }
  return vcount;
}

function consonants_count(str1)
{
  var consonant_list = ' bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ';
  var c_count = 0;

  for(var x = 0; x <str1.length ; x++)
  {
    if (consonant_list.indexOf(str1[x]) !== -1)
    {
      c_count += 1;
    }
  
  }
  return c_count;
}
</SCRIPT>
<style>

b {
  font-family:arial;
  color:blue;
  font-size:18px;
  }


 .for_div 
 { 
  font-weight: bold;
  font-family:arial;
  font-size:18px;
  color:blue;
}
  
 input[type="text"] {
  display: block;
  margin: 0;
  width: 35%;
  font-family: arial;
  font-size: 18px;
  appearance: none;
  border-radius: 2px;
  box-shadow: 10px 10px 7px #888888;
}
input[type="text"]:focus {
  outline: none;
}

input[type=button] {
padding:12px 31px; 
background:yellow; border:10px;
cursor:pointer;
-webkit-border-radius: 12px;
box-shadow: 10px 10px 7px #888888;
border-radius: 5px; 
font-family: arial;
font-size: 18px;
}
</style>
   
</head>
<body>
<b> Vowels and Consonants Counter </b> <br><br>
<form name="myform" action="">
<b> Enter a word or a sentence </b><br><br>
<input type='text' id='userInput' placeholder="enter text here" value='' size="30" required/>
<br><br>
<input type='button' onclick='count_now()' title="Click here to count the vowels and consonants" value='OK'/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type='button' onclick='clear_now()' title="Click here to clear the textbox." value='CLEAR'/>
</form>
<br><br><br>
<div class="for_div" id='word_string'></div> <br><br>
<div class="for_div" id='vowels'> </div>
<div class="for_div" id='consonants'> </div>
</body>
</html>