Friday, September 25, 2015

Prime Number Checker in JavaScript

A simple program that I wrote to check if the number given by the user is a prime number or not. I also added a functionality that will check if the given value is an invalid entry like letters or special characters.

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>Prime Number Checker in JavaScript</title>

 <script type="text/javascript">

        function check_prime() {
        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 (!isNaN(number)) {
                            if (isPrime(number)) {
                                document.getElementById("result").innerHTML = number + " is a PRIME number.";
                            }
                            else {
                                document.getElementById("result").innerHTML = number + " is NOT a PRIME number.";
                            }
                        }
                        else {
                            document.getElementById("result").innerHTML = "Please enter a number.";
                        }
                    }
                   
                   function isPrime(n_value) {
                      
                        if (n_value < 2) {
                            return false
                           }
                        if (n_value != Math.round(n_value)) {
                           return false
                           }
                       
                        var isPrime = true;

                       for (var i = 2; i <= Math.sqrt(n_value); i++) {
                            if (n_value % i == 0) {
                            isPrime = false
                            }
                        }

                       return isPrime;       

                      }
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h2> Prime Number Checker in JavaScript </h2>
  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 prime number or not."
    onclick="check_prime()" />
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br>
  <p id="result"></h1>
</body>
</html>



Perfect Number Checker in JavaScript

This program if the given number value by the user is perfect number or not. I added a function for checking invalid entry values.

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>Prime Number Checker in JavaScript</title>

 <script type="text/javascript">

        function check_prime() {
        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 (!isNaN(number)) {
                            if (is_perfect(number)) {
                                document.getElementById("result").innerHTML = number + " is a PERFECT number.";
                            }
                            else {
                                document.getElementById("result").innerHTML = number + " is NOT a PERFECT number.";
                            }
                        }
                        else {
                            document.getElementById("result").innerHTML = "Please enter a number.";
                        }
                    }
                   
                   
        function is_perfect(number)  
        {  
        var temp = 0;  
           for(var i=1;i<=number/2;i++)  
             {  
                 if(number%i === 0)  
                  {  
                    temp += i;  
                  }  
             }  
             
             if(temp === number)  
                {  
                return true;  
                }   
             else  
                {  
               return false;
                }     
         }   
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h2> Perfect Number Checker in JavaScript </h2>
  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 perfect number or not."
    onclick="check_prime()" />
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br>
  <p id="result"></h1>
</body>
</html>





Count Words in a Sentence in JavaScript

This program will allow the user to enter a sentence and then the program will count how many words in a given sentence 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

<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> Count Words in a Sentence </title>

 <script type="text/javascript">

        function count_words() {
        var input = document.getElementById("userInput").value;
              var number = parseInt(input);
                
                 count_word=input.split(" ")
                 document.getElementById("result").innerHTML =
                 "Number of words in a given sentence is " + 
                 count_word.length + ".";
                 }
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h2> Count Words in a Sentence </h2>
Enter a sentence
  <br>
    <textarea cols="40" id="userInput" autofocus></textarea>
    <br><br>
    <input type="submit" value="Check" 
    title="Click here to count the number of words in a sentence."
    onclick="count_words()" /> &nbsp; &nbsp; 
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br><br>
  <p id="result"></h1>
</body>
</html>




Leap Year Checker in Javascript

This sample program that I wrote using JavaScript as my programming language will check if the given year by the user is a leap year or not. Leap year occurs every four year just like we having our Olympic games held. I added a function to clear the text field and checking for invalid entry values. The code is very simple and easy to understand by 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

<html>
<head>
<style>
body {
  background-color:lightgreen;
  font-family:arial;
  font-size:20px;
  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> Leap Year Checker in JavaScript </title>

 <script type="text/javascript">

        function count_words() {
        var input = document.getElementById("userInput").value;
              var year  = parseInt(input);
               
               if (isNaN(year)) {
                  alert("Enter a valid year");
                   document.getElementById("result").innerHTML =  "";
                   document.getElementById("userInput").value="";
                   document.getElementById("userInput").focus();
                   }
                 else  if  ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0))
                {
                document.getElementById("result").innerHTML = 
                "The year " + year + " is a LEAP year";
                }
                else {
                  document.getElementById("result").innerHTML = 
                "The year " + year + " is NOT A LEAP year";
                 }
                
                 }
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h2> Leap Year Checker in Javascript </h2>
Enter desired year
  <br>
    <input type="text"  id="userInput" size="4" maxlength="4" autofocus>
    <br><br>
    <input type="submit" value="Check" 
    title="Click here to check for leap year."
    onclick="count_words()" /> &nbsp; &nbsp; 
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br><br>
  <p id="result"></h1>
</body>
</html>




Celsius To Fahrenheit in JavaScript

In this article I will show you how  to perform Celsius to Fahrenheit temperature conversion using JavaScript. The code is very simple and easy to understand and use. What does the program will to is to ask the user temperature in Celsius and then convert it to its Fahrenheit equivalent value. I also added a function to check for invalid entries like letters and special characters.

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:20px;
  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> Celsius To Fahrenheit in JavaScript </title>

 <script type="text/javascript">

        function solve_temp() {
        var input = document.getElementById("userInput").value;
             var  celsius  = parseFloat(input);
             
             if (isNaN(celsius)) {
                  alert("Please enter a number.");
                  document.getElementById("userInput").value="";
                  document.getElementById("userInput").focus();
                  document.getElementById("result").innerHTML =  "";
                } 
               
              else {                  
               var solve_fahrenheit  = celsius  * 9 / 5 + 32;
               var solve_results = celsius +'\xB0C in Celsius is equivalent to '
                + solve_fahrenheit  + '\xB0F' + ' in Fahrenheit.' ;
                
                document.getElementById("result").innerHTML = solve_results;
                }
                
             }
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h2>  Celsius To Fahrenheit in JavaScript </h2>
Enter temperature in celsius
  <br>
    <input type="text"  id="userInput" size="8" maxlength="8" autofocus>
    <br><br>
    <input type="submit" value="Check" 
    title="Click here to celsius to fahrenheit."
    onclick="solve_temp()" /> &nbsp; &nbsp; 
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br><br>
  <p id="result"></h1>
</body>
</html>