Admin Admin
Posts : 27 Join date : 2009-11-13
| Subject: Fractions Calculator Wed Nov 18, 2009 12:42 am | |
| A Javascript for fractions calculator - Code:
-
var n1, n2, d1, d2, An, Ad, Op; var neg=1;
function solve(){ //If all fields are numbers if(!isNaN(document.calc.n1.value)&&!isNaN(document.calc.d1.value)&&!isNaN(document.calc.n2.value)&&!isNaN(document.calc.d2.value)){ //If no fields are blank if(document.calc.n1.value!=''&&document.calc.d1.value!=''&&document.calc.n2.value!=''&&document.calc.d2.value!=''){ //Set variables: n1=document.calc.n1.value;// Numerator 1 d1=document.calc.d1.value;// Numerator 2 n2=document.calc.n2.value;// Denominator 1 d2=document.calc.d2.value;// Denominator 2 Op=document.calc.Op.value;// Operator } else { //If blank field alert('Please fill-in all fields!'); } } else { //If field has non-number alert('Please enter only Numbers into the fields!'); }
//Which Operation switch (Op){ case '+': //add fractions using formula ((n1*d2)+(n2*d1)) over (d1*d2) An=(n1*d2)+(n2*d1) //Answer Numerator Ad=(d1*d2) //Answer Denominator if(document.calc.reduce.checked==1){ reduce(); } else { display(); } break
case '-': //subtract fractions using formula ((n1*d2)-(n2*d1)) over (d1*d2) An=(n1*d2)-(n2*d1)//Answer Numerator Ad=(d1*d2)//Answer Denominator if(document.calc.reduce.checked==1){ reduce(); } else { display(); } break
case '*': //multiply fractions using formula (n1*n2) over (d1*d2) An=n1*n2;//Answer Numerator Ad=d1*d2; //Answer Denominator if(document.calc.reduce.checked==1){ reduce(); } else { display(); } break
case '/': //divide fractions using formula (n1*d2) over (d1*n2) An=n1*d2;//Answer Numerator Ad=d1*n2;//Answer Denominator if(document.calc.reduce.checked==1){ reduce(); } else { display(); } break } }
function reduce() { neg=1; //1 if positive, -1 if negative //convert to strings ng=An+''; dg=Ad+'' if(ng.indexOf('-')!=-1){ //check to see if answer is negative. neg=-1 } if(dg.indexOf('-')!=-1){ neg=-1 } if(ng.indexOf('-')!=-1&&dg.indexOf('-')!=-1) {//if both numerator and denominator are negative the answer is positive neg=1 } var factorX //highest common factor
if ( An == 0 || Ad == 0 ) { factorX=1; return; }
An = Math.abs( An ); Ad = Math.abs( Ad );
var factorX = 1;
//Find common factors of Numerator and Denominator for ( var x = 2; x <= Math.min( An, Ad ); x ++ ) { var check1 = An / x; if ( check1 == Math.round( check1 ) ) { var check2 = Ad / x; if ( check2 == Math.round( check2 ) ) { factorX = x; } } }
An=(An/factorX)*neg; //divide by highest common factor to reduce fraction then multiply by neg to make positive or negative Ad=Ad/factorX; //divide by highest common factor to reduce fraction display(); }
function display(){ //Display answer document.calc.An.value = An; document.calc.Ad.value = Ad; }
// --> </script> </HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<table width="200" align="center" border="0" cellspacing="0" cellpadding="4" style="background-color:#ffffff;border:1px #000000 solid;"> <tr> <td align="center" valign="middle"> <h3>Fraction Calculator</h3> <form name="calc"> <table width="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td style="border-bottom:2px #000000 solid;"><input type="text" size="2" name="n1" id="n1" tabindex="1"></td> <td rowspan="2" align="center" valign="middle"> <select name="Op" tabindex="3"> <option value="+">+</option> <option value="-">-</option> <option value="*">x</option> <option value="/">รท</option> </select> </td> <td style="border-bottom:2px #000000 solid;"><input type="text" size="2" name="n2" id="n2" tabindex="4"></td> <td rowspan="2" align="center" valign="middle"><input type="button" value=" = "onClick="solve();" tabindex="6"></td> <td style="border-bottom:2px #000000 solid;"><input type="text" size="2" name="An" id="An" readonly="1"></td> </tr> <tr> <td><input type="text" size="2" name="d1" id="d1" tabindex="2"></td> <td><input type="text" size="2" name="d2" id="d2" tabindex="5"></td> <td><input type="text" size="2" name="Ad" id="Ad" readonly="1"></td> </tr> </table> <br><input type="checkbox" name="reduce" id="reduce" checked> Reduce </form> </td> </tr> </table>
<p><center> <font face="arial, helvetica" size"-2">Free JavaScripts provided<br> by <a href="http://www.downloadlibrary.board-directory.net">The JavaScript Source</a></font> </center><p> A Javascript for shapes area calculator - Code:
-
function doArea(num) { switch(num) { case 0 : return (""); break; case 1 : var length = prompt("Please enter the length of your square:", ""); length = length * length; return (length); break; case 2 : var width = prompt("Please enter the width of the base:", ""); var height = prompt("Please enter the height of the triangle:", ""); return (width * height / 2); break; case 3 : var width = prompt("Please enter the width of your rectangle:", ""); var height = prompt("Please enter the height of your rectangle:", ""); return (width * height); break; case 4 : var radius = prompt("Please enter the radius of the circle: ", ""); return (Math.PI * Math.pow(radius, 2)); break; case 5 : var radius = prompt("Please enter the radious of the sphere:", 0); return (4 * Math.PI * (Math.pow(radius, 2))); break; } } // End --> </script> </HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<center> <form name=calcarea> Find the area of a <select name="shape" size="1" onChange="this.form.area.value = doArea(this.selectedIndex);"> <option> ... <option value="square">Square <option value="triangle">Triangle <option value="rectangle">Rectangle <option value="circle">Circle <option value="sphere">Sphere </select> = <input type=text name=area size=10> </form> </center>
<p><center> <font face="arial, helvetica" size="-2">Free JavaScripts provided<br> by <a href="http://www.downloadlibrary.board-directory.net">The JavaScript Source</a></font> </center><p> A Javascript for Alarm Clock - Code:
-
var alarmTime; var curTime; var check = new Date(); var hourNum; var minNum; function GetTime() { var dt = new Date(); document.clock.local.value = IfZero(dt.getHours()) + ":" + IfZero(dt.getMinutes()); setTimeout("GetTime()", 1000); curTime = (IfZero(dt.getHours()) + ":" + IfZero(dt.getMinutes())); } function IfZero(num) { return ((num <= 9) ? ("0" + num) : num); } function alarmSet() { hourNum = document.clock.hourOpt[document.clock.hourOpt.selectedIndex].value; minNum = document.clock.minOpt[document.clock.minOpt.selectedIndex].value; alarmTime = hourNum + ":" + minNum; } function alarmOn() { if (alarmTime == curTime) { document.all.sound.src = document.clock.alarmSound.value; } else { setTimeout("alarmOn()", 1000) } } function alarmOff() { document.all.sound.src = ""; alarmTime=""; } function snooze() { document.all.sound.src = ""; var snoozeL = parseInt(document.clock.snoozeOpt[document.clock.snoozeOpt.selectedIndex].value); var snooze = new Date(); alarmTime = IfZero(snooze.getHours()) + ":" + IfZero(snooze.getMinutes() + snoozeL); alarmOn(); } // End --> </script> <bgSound src="" id="sound"> </HEAD>
<!-- STEP TWO: Insert the onLoad event handler into your BODY tag -->
<BODY onLoad="GetTime()">
<!-- STEP THREE: Copy this code into the BODY of your HTML document -->
<table width=100% border=1> <tr> <td align=middle> <form name="clock"> <input size="8" name="local"> <input type=radio name=alarmOnOff onClick="alarmOn()">On <input type=radio name=alarmOnOff onClick="alarmOff()" checked>Off <td align=middle> Set Alarm: <br> <select name=hourOpt onChange="alarmSet()" size=1> <option value="00">00<option value="01">01<option value="02">02<option value="03">03 <option value="04">04<option value="05">05<option value="06">06<option value="07">07 <option value="08">08<option value="09">09<option value="10">10<option value="11">11 <option selected value="12">12 <option value="13">13<option value="14">14<option value="15">15<option value="16">16 <option value="17">17<option value="18">18<option value="19">19<option value="20">20 <option value="21">21<option value="22">22<option value="23">23 </option> </select> <select name=minOpt onChange="alarmSet()" size=1> <option selected value="00">00<option value="01">01<option value="02">02<option value="03">03 <option value="04">04<option value="05">05<option value="06">06<option value="07">07 <option value="08">08<option value="09">09<option value="10">10<option value="11">11 <option value="12">12<option value="13">13<option value="14">14<option value="15">15 <option value="16">16<option value="17">17<option value="18">18<option value="19">19 <option value="20">20<option value="21">21<option value="22">22<option value="23">23 <option value="24">24<option value="25">25<option value="26">26<option value="27">27 <option value="28">28<option value="29">29<option value="30">30<option value="31">31 <option value="32">32<option value="33">33<option value="34">34<option value="35">35 <option value="36">36<option value="37">37<option value="38">38<option value="39">39 <option value="40">40<option value="41">41<option value="42">42<option value="43">43 <option value="44">44<option value="45">45<option value="46">46<option value="47">47 <option value="48">48<option value="49">49<option value="50">50<option value="51">51 <option value="52">52<option value="53">53<option value="54">54<option value="55">55 <option value="56">56<option value="57">57<option value="58">58<option value="59">59 </option> </select> <tr> <td align=middle> Select Alarm Sound: <br> <input type=file size=4 name=alarmSound> <td align=middle> Snooze For: <br> <select NAME=snoozeOpt size=1 onChange="snooze()"> <option value="1">1<option value="2">2<option value="3">3<option value="4">4 <option value="5">5<option value="6">6<option value="7">7<option value="8">8 <option value="9" selected>9 </option> </select> </form> </td> </tr> </table>
<p><center> <font face="arial, helvetica" size"-2">Free JavaScripts provided<br> by <a href="http://www.downloadlibrary.board-directory.net">The JavaScript Source</a></font> </center><p> | |
|