Sunday, February 11, 2018

Factors of a Number in JavaScript

A simple program that I wrote using Javascript to ask the user to give a number and then our program will display the factors of the given number.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.






Sample Program Output


Program Listing

index.htm

<!--
 Written By Mr. Jake R. Pomperada, MAED-IT
 February 11, 2018   04:24 PM   Sunday
 Bacolod City, Negros Occidental Republic of the Philippines
 jakerpomperada@yahoo.com and jakerpomperada@gmail.com -->
<!doctype html>
<html>
<head>
<title>Factors of a Number in JavaScript </title>
<style>

   .art {

      font-family: arial;
      font-weight: bold;
      color:red;
      text-align: center;
      width:60%;
      background-color:yellow;
   }

.art2 {

      font-family: arial;
      font-weight: bold;
      font-size: 25px;
      color:blue;
      text-align: center;
      width:45%;
      background-color:lightgreen;
   }

   body {
     font-family: arial;
     font-weight: bold;
     font-size: 20px;
   }

   #button_me {
    background-color: #008CBA;
    font-family: arial;
    font-weight: bold;
    font-size: 20px;
    height: 50px;
    width: 120px
     text-align: center;
    cursor: pointer;
 }

 input[type=text] {
    font-family: arial;
    font-weight: bold;
    font-size: 20px;
   
 }
</style>
      
<script>
function factors(n)
{
 var num_factors = [], i;

 for (i = 1; i <= Math.floor(Math.sqrt(n)); i += 1)
  if (n % i === 0)
  {
   num_factors.push(i);
   if (n / i !== i)
    num_factors.push(n / i);
  }
 num_factors.sort(function(x, y)
   {
     return x - y;});  // numeric sort
     return num_factors;
    }


function start()
{
num1=Number(document.getElementById("val1").value);

document.getElementById("result").value= factors(num1);
}

</script>


<script>
function clear_me()
{
document.getElementById("val1").value = "";
document.getElementById("result").value = "";
document.getElementById("val1").focus();
}
</script>

</head>
<body>
<h1 class="art"> Factors of a Number in JavaScript </h1>
    <p  class="art2"> Created By Mr. Jake R. Pomperada, MAED-IT </p>
<br><br>
Give a Number <input id="val1" type="text" size="5" autofocus>
<br><br>
The result is <input id="result" type="text" size="50"
<br> <br> <br>
<button id="button_me" onclick="start()">Check</button>
&nbsp;&nbsp;&nbsp;
<button id="button_me" onclick="clear_me()">Clear</button>
</br></br>
</body>
</html>




No comments:

Post a Comment