topical media & game development
lib-js-math-calculator-factors.htm / htm
<!-- TWO STEPS TO INSTALL COMMON FACTORS CALCULATOR:
1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Original: Tom McComb (tom@dominoquest.com) -->
<!-- Web Site: http://www.dominoquest.com/ -->
<script>
function findFactors() {
var f = document.forms[0];
var value1 = parseInt( f.composite1.value );
var value2 = parseInt( f.composite2.value );
if ( isNaN( value1 ) || isNaN( value2 ) ) {
alert( "Please enter valid number values" );
f.reset();
return;
}
if ( value1 == 0 || value2 == 0 ) {
alert( "No common factors" );
return;
}
value1 = Math.abs( value1 );
value2 = Math.abs( value2 );
var answer = "1";
for ( var x = 2; x < Math.min( value1, value2 ); x ++ ) {
var check1 = value1 / x;
if ( check1 == Math.round( check1 ) ) {
var check2 = value2 / x;
if ( check2 == Math.round( check2 ) ) {
answer += ", " + x;
}
}
}
alert( "value1 = " + value1 + "\nvalue2 = " + value2 + "\nCommon Factors: " + answer );
}
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Original: Tom McComb (tom@dominoquest.com) -->
<!-- Web Site: http://www.dominoquest.com/ -->
<form>
<table border=1 cellspacing=1 cellpadding=3 width=300>
<tr>
<td colspan=2><font face=Arial size=2>
Enter two numbers, then click the button to find the common integer factors of both numbers.
</font></td>
</tr>
<tr>
<td><font face=Arial size=1>Composite 1:</font></td>
<td><input type=text name=composite1></td>
</tr>
<tr>
<td><font face=Arial size=1>Composite 2:</font></td>
<td><input type=text name=composite2></td>
</tr>
<tr>
<td colspan=2 align=right><input type=button value="Find Common Factors" onclick="findFactors();"></td>
</tr>
</table>
</form>
<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 2.51 KB -->
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.