0

I need to create a calculator where the user inputs a number, and it calculates the factors, cube and square of the given number.

Below is the code i am using.. I have NO idea how to work out the factor. Any advice would be appreciated.

document.getElementById('calculate').addEventListener('click', estimateTotal);

function estimateTotal(event) {
 event.preventDefault();
 var initial2 = document.getElementById('initial').value;
 
 document.getElementById('factor').value = 0;
 document.getElementById('sqaure').value = initial2 * initial2;
 document.getElementById('cube').value = initial2 * initial2 * initial2;
}
<form id="calculator" method="POST">
    <p>Please enter a number between 0 and 50 <input name="initial" id="initial" type="text" size="20" required><button id="calculate">Calculate</button></p>
    <p>The Factorial of your number is: <input name="factor" id="factor" class="factor" type="text" size="20"></p>
    <p>The Square of your number is:<input name="sqaure" id="sqaure" class="sqaure" type="text" size="20"></p>
    <p>The Cube of your number is:<input name="cube" id="cube" class="cube" type="text" size="20"></p>
</form>
Fushy
  • 83
  • 2
  • 13
  • check http://stackoverflow.com/questions/8647059/finding-factors-of-a-given-integer – Min Naing Oo Mar 11 '15 at 05:06
  • 3
    Use a factoring algorithm. Search for "integer factoring algorithm" on Google. See http://en.wikipedia.org/wiki/Integer_factorization. By the way, your HTML refers to "factorial"; are you interested in factorials, or factors (of which an integer may of course have more than one)? –  Mar 11 '15 at 05:07
  • 1
    Factor != Factorial. What do you really need to calculate? – Alexey Ten Mar 11 '15 at 05:09
  • Defnitly Factor, the html part is wrong. :) – Fushy Mar 11 '15 at 05:11
  • Write a function with initial2 as input parameter and assign the output of this function to document.getElementById('factor').value. You can find the function at the link shared in the first comment – gkb Mar 11 '15 at 05:18

2 Answers2

2
  **recursive JavaScript function factor(n)**

 **Check this link:** **http://www.javascripter.net/math/primes/factorization.htm**

function factor(n) {
 if (isNaN(n) || !isFinite(n) || n%1!=0 || n==0) return ''+n;
 if (n<0) return '-'+factor(-n);
     var minFactor = leastFactor(n);
 if (n==minFactor) return ''+n;
 return minFactor+'*'+factor(n/minFactor);
}
Mushahid Khan
  • 2,816
  • 1
  • 19
  • 32
1

try this for both factor and factorial

document.getElementById('calculate').addEventListener('click', estimateTotal);

function estimateTotal(event) {
 event.preventDefault();
 var initial2 = document.getElementById('initial').value;
 
 document.getElementById('Factorial').value = fact(initial2);
  document.getElementById('factor').value = factors(initial2);
 document.getElementById('sqaure').value = initial2 * initial2;
 document.getElementById('cube').value = initial2 * initial2 * initial2;
 
}


   function fact(n)
            {
                if(n == 0)
                    return 1;
                else
                    return (n*fact(n-1));
            }


  function factors(num)
{
 var
  n_factors = [],
  i;
 
 for (i = 1; i <= Math.floor(Math.sqrt(num)); i += 1)
  if (num % i === 0)
  {
   n_factors.push(i);
   if (num / i !== i)
    n_factors.push(num / i);
  }
 n_factors.sort(function(a, b){return a - b;});  // numeric sort
 return n_factors;
}
<p>Please enter a number between 0 and 50 <input name="initial" id="initial" type="text" size="20" required><button id="calculate">Calculate</button></p>
<p>The Factorial of your number is: <input name="factor" id="Factorial" class="factor" type="text" size="20"></p>

<p>The Factor of your number is: <input name="factor" id="factor" class="factor" type="text" size="20"></p>
<p>The Sqaure of your number is:<input name="sqaure" id="sqaure" class="sqaure" type="text" size="20"></p>
<p>The Cube of your number is:<input name="cube" id="cube" class="cube" type="text" size="20"></p>
Dhaval
  • 2,341
  • 1
  • 13
  • 16