JavaScript - Ratio Calculator Problem
hi guys, im haveing a bit of a problem with this script - its a ratio calculator.
can you guys see whats the problem? Heres the java script eg. when you write 86708 kills and 53240 death the K/D calc should show this: 1,62 = 86 (-86) 1,64 = 389 1,70 = 3582 but it doesent???? heres the code guys : <HTML> <HEAD> <script type="text/javascript"> function find_ratio() { var x = document.getElementById("x1").value; var y = document.getElementById("y1").value; var gcd=calc(x,y); var r1=x/gcd; var r2=y/gcd; var ratio = r1 / r2; document.getElementById("res").value = ratio.toFixed(2); var death1 = r1 / r2; death1 = Math.floor(ratio * 100) / 100; document.getElementById("death1").value = death1.toFixed(2); var kill1 = r1 / r2; kill1 = Math.ceil(ratio * 100) / 100; document.getElementById("kill1").value = kill1.toFixed(2); var kill3 = r1 / r2; kill3 = Math.ceil(ratio * 10) / 10; document.getElementById("kill3").value = kill3.toFixed(2); var deathcalc = (death1 - 0.00401) * r2; var death2 = deathcalc - r1; document.getElementById("death2").value = death2.toFixed(0); var killcalc1 = (kill1 - 0.00401) * r2; var kill2 = killcalc1 - r1; document.getElementById("kill2").value = kill2.toFixed(0); var killcalc2 = (kill3 - 0.00401) * r2; var kill4 = killcalc2 - r1; document.getElementById("kill4").value = kill4.toFixed(0); } function calc(n1,n2) { var num1,num2; if(n1 < n2){ num1=n1; num2=n2; } else{ num1=n2; num2=n1; } var remain=num2%num1; while(remain>0){ num2=num1; num1=remain; remain=num2%num1; } return num1; } function isInteger(s,iid) { var i; s = s.toString(); for (i = 0; i < s.length; i++) { var c; if(s.charAt(i)==".") { } else { c = s.charAt(i); } if (isNaN(c)) { alert("Given value is not a number"); document.getElementById(iid).value=""; return false; } } return true; } </script> <style type="text/css"> .style1 { width: 90px; } .style2 { height: 55px; } #x1 { text-align: center; } #y1 { text-align: center; } </style> </HEAD> <BODY> <table cellspacing=0 cellpadding=2 style="border:1px solid green; width: 250px; height: 217px;" align=center> <tr><td class="style1">Kills:</td><td style="text-align: center"><input type="text" id="x1" onkeyup="isInteger(this.value,this.id);"></td></tr> <tr><td class="style1">Death:</td><td style="text-align: center"><input type="text" id="y1" onkeyup="isInteger(this.value,this.id);"></td></tr> <tr><td colspan=2 align=center class="style2"><input type="button" value="Lad magien flyde" onclick="find_ratio()"></td></tr> <tr><td class="style1">Ratio:</td><td style="text-align: center"><input type="text" readonly id="res" style="font-weight:bold; width: 65px; text-align: center;" size="6"> = <input type="text" readonly id="res2" style="font-weight:bold; width: 65px; text-align: center;" size="6"></td></tr> <tr><td class="style1">Deaths to:</td><td style="text-align: center"> <input type="text" readonly id="death1" style="font-weight:bold; width: 65px; text-align: center;" size="6"> = <input type="text" readonly id="death2" style="font-weight:bold; width: 65px; text-align: center;" size="6"></td></tr> <tr><td class="style1">Kills to:</td><td style="text-align: center"> <input type="text" readonly id="kill1" style="font-weight:bold; width: 65px; text-align: center;" size="6"> = <input type="text" readonly id="kill2" style="font-weight:bold; width: 65px; text-align: center;" size="6"></td></tr> <tr><td class="style1">Kills to:</td><td style="text-align: center"> <input type="text" readonly id="kill3" style="font-weight:bold; width: 65px; text-align: center;" size="6"> = <input type="text" readonly id="kill4" style="font-weight:bold; width: 65px; text-align: center;" size="6"></td></tr></table> </BODY> </HTML> Similar TutorialsHey I've been working on my Spanish Clubs' website over the summer and I need some help when making this converter. I want to make it so when someone chooses a different country, it will change the answeres and do the equation in the text boxes and also if they change the numbers manually they will also do the equation. I've tried it out and it's making me go mad. PLEASE HELP! Here is the javascript code I made: Code: function calculatecountry() { var country = calculate.countrys.options[calculate.countrys.selectedIndex].text; //===============================COUNTRIES==================================== if (countrys=="--Country--") { var usd1 = 0; var other1 = 0; } if (countrys=="Argentina") { var usd1 = 0.2553; var other1 = 1; } if (countrys=="Bolivia") { var usd1 = 0.14245; var other1 = 1; } if (countrys=="Chile") { var usd1 = 0.0018; var other1 = 1; } //================================EQUATIONS===================================== var c1 = calculate.other.value; var c2 = calculate.usd.value; var ans1 = c1 * other1; var ans2 = c2 * usd1; calculate.usd.value = ans1; calculate.other.value = ans2; } Here is the form code: Code: <html><head><title></title> <script type="text/javascript" src="js_money_calc.js"></script> </head><body> Converter <form name="calculate"> <select name="countrys" onChange="calculatecountry()"> <option selected>--Country--</option> <option>Argentina</option> <option>Bolivia</option> <option>Chile</option> </select><p> <input type="text" name="usd" onKeyUp="calculatecountry()" /> = <input type="text" name="other" onKeyUp="calculatecountry()" /></p> </form> </body></html> Thanks. Hi, I am new to Javascript and I am having a problem with this calculator I am building. The purpose is to see if you will be able to go to prom. In a form you input 4 pieces of info. (1)How much money the ticket costs. (2)How much money your tuxedo costs. (3)How much money the dinner costs. (4)How much money you have. I have the calculator set up, and it is supposed to add together the ticket cost, tuxedo cost, and dinner cost, and subtract it from how much money you have. I finished the code, and I am getting no error at the bottom of the screen, but the calculator doesn't work. So, I guess it is a logic error. Here is the code and a link to the page: http://themusiccove.com/Documents/prom2010.htm <html> <head> <title>Prom 2010</title> <script LANGUAGE="JAVASCRIPT"> function prom(myform) { var ticket=document.Calculator.ticket.value var ticket=parseInt(ticket,10) if (isNaN(ticket)) { alert("Enter a number for the Ticket Cost.") document.Calculator.ticket.value="" document.Calculator.ticket.focus() } else { var tuxedo=document.Calculator.tuxedo.value var tuxedo=parseInt(tuxedo,10) if (isNaN(tuxedo)) { alert("Enter a number for the Tuxedo Cost.") document.Calculator.tuxedo.value="" document.Calculator.tuxedo.focus() } else { var dinner=document.Calculator.dinner.value var dinner=parseInt(dinner,10) if (isNaN(dinner)) { alert("Enter a number for the Dinner Cost.") document.Calculator.dinner.value="" document.Calculator.dinner.focus() } else { var money=document.Calculator.money.value var money=parseInt(money,10) if (isNaN(money)) { alert("Enter a number for how much money you have.") document.Calculator.money.value="" document.Calculator.money.focus() Calculate=money-(ticket+tuxedo+dinner) document.Calculator.answer.value=Calculate } } } } } </script> </head> <body bgcolor="ccffcc"> <img src="http://www.shop-direct.net/images/prom5.jpg" align="left" width="125" length="100"> <img src="http://www.packerpress.com/wp-content/uploads/2009/05/5301-men-tuxedo-jacket-b.jpg" align="right" width="125" length="100"> <h2><center>Prom Calculator</center><h2> <h4><center>Enter the data in the textboxes to see if you have enough money to go to prom.</center></h4> <Center><FORM Name="Calculator"> <table border="0"> <tr> <td><p align=right>Ticket Cost: </td> <td><Input Type="text" Name="ticket" value=" "></td> </tr> <tr> <td><p align=right>Tuxedo Cost: </td> <td><Input Type="text" Name="tuxedo" value=" "></td> </tr> <tr> <td><p align=right>Dinner Cost: </td> <td><Input Type="text" Name="dinner" value=" "></td> </tr> <tr> <td><p align=right>Your Money: </td> <td><Input Type="text" Name="money" value=" "></td> </tr> <tr> <td><p align=right>Profit/Loss: </td> <td><Input Type="text" Name="answer" value=" "></td> </tr> <tr align="center"> <td colspan=2><Input Type="Button" Value="Calculate" Onclick="prom(Calculator)"></tr> <tr align="center"><td colspan=2><Input Type="Reset"></td></tr> </table></center> <SCRIPT LANGUAGE="JAVASCRIPT"> <!--Hide from old browsers //--> </SCRIPT> <CENTER><HR Width="75%"></CENTER> <SCRIPT LANGUAGE="JAVASCRIPT"> <!--Hide from old browsers var tNow = new Date() var tlocDate = tNow.toLocaleString() var tDate = tlocDate.substring(0,23) document.write("<H2><CENTER>Welcome, today is "+tDate+"</CENTER></H2>") //--> </SCRIPT> <SCRIPT LANGUAGE="JAVASCRIPT"> <!--Hide from old browsers document.write("<H4><CENTER>This document was last modified "+document.lastModified+"</CENTER></H4>") //--> </SCRIPT> </body> </html> I have this project i have to complete for class, that i cant figure out. this is what the project says: Prompt the user for the grades of 10 students with one prompt for each grade with the prompting text being: "Enter grade between 1 and 10 for student n:" where n varies from 1 to 10. If the grade input is not between 1 and 10, please prompt again for the grade. Once the user is done, it prints a report telling you the grade of each individual student and the average of the ten grades. The individual student grade should be in different lines with the text "Grade for student n is x". The average should also be in a different line with the text "Average grade is y". The average grade should not be rounded off. my problem is i cant figure out how to write each individual grade (from 1 through 10), and display it on the page. i have this code from a similar project, i tried to ammend it for this code but i always end up ruining it any tips on how to display each grade (1-10)? or how to verify that the user only enters grades between 1 and 10 Code: <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Class Average Program: Sentinel-controlled Repetition</title> <script type = "text/javascript"> <!-- var total; // sum of grades var gradeCounter; // number of grades entered var grade; // grade typed by user (as a string) var gradeValue; // grade value (converted to integer) var average; // average of all grades // Initialization phase total = 0; // clear total gradeCounter = 0; // prepare to loop // Processing phase // prompt for input and read grade from user grade = window.prompt( "Enter Integer Grade, -1 to Quit:", "0" ); // convert grade from a String to an integer gradeValue = parseInt( grade ); while ( gradeValue != -1 ) { // add gradeValue to total total = total + gradeValue; // add 1 to gradeCounter gradeCounter = gradeCounter + 1; // prompt for input and read grade from user grade = window.prompt( "Enter Integer Grade, -1 to Quit:", "0" ); // convert grade from a String to an integer gradeValue = parseInt( grade ); } // end while // Termination phase if ( gradeCounter != 0 ) { average = total / gradeCounter; // display average of exam grades document.writeln( "<h1>Class average is " + average + "</h1>" ); } // end if else document.writeln( "<p>No grades were entered</p>" ); // --> </script> </head> <body> <p>Click Refresh (or Reload) to run the script again</p> </body> </html> I am a student trying to complete this assignment. Any assistance you can give me is greatly appreciated. My code is inserted below. This is calculator program that I have gotten stuck on. Here is where I think I am at: The program is now taking the 1st number entered displaying it and storing it in the variable "memory". Operation (+,-,*,/) is added to the memory in function action1() but then when the second number is entered it replaces memory with the second number. How can I get the variable memory to the whole string of 1st number,action, 2nd number? Also once I do get that working correctly: Wouldn't eval(document.calculator.display.value= memory) in my HTML for the "=" give me the calculation. ( if I could figure out how to have memory store the 1st number and action1 and 2nd number all together) Or would eval(document.calculator.display.value) do it for me. //////////////////////////////////////////////////////////////////////////////////// <html> <head> <title> Calculator</title> </head> <script type = "text/javascript" src="calculator2.js"></script> <body> <center>Calculator<br> To use this calculator, CLICK a number, then an ACTION, then another number, then the EQUALS button. <br> Press "C" when ready to start over. <br> The "N" button makes your previous number a negative.<br></center> <form name="calculator"> <table border = "1" align ="center"> <tr> <td ><input type = "text" size = 26 name = "display" id = ""/></td> </tr> <tr ><br /> <!-- first row of calculator --> <td> <input type = "button" value = " 7 " id = "" onclick="newstring('7')"> <input type = "button" value = " 8 " id = "" onclick="newstring('8')"/> <input type = "button" value = " 9 " id = "" onclick="newstring('9')"> <input type = "button" value = " / " id = "" onclick="action1('/')"> </td> </tr> <tr ><br /> <!-- second row of calculator --> <td> <input type = "button" value = " 4 " id = "" onclick="newstring('4')"> <input type = "button" value = " 5 " id = "" onclick="newstring('5')"> <input type = "button" value = " 6 " id = "" onclick="newstring('6')"> <input type = "button" value = " * " id = "" onclick="action1('*')"> </td> </tr> <tr ><br /> <!-- third row of calculator --> <td> <input type = "button" value = " 1 " id = "" onclick="newstring('1')"> <input type = "button" value = " 2 " id = "" onclick="newstring('2')"> <input type = "button" value = " 3 " id = "" onclick="newstring('3')"> <input type = "button" value = " - " id = "" onclick="action1('-')"> </td> </tr> <tr ><br /> <!-- forth row of calculator --> <td> <input type = "button" value = " 0 " id = "" onclick="newstring('0')"> <input type = "button" value = " N " id = "" onclick="newstring('N')"> <input type = "button" value = " = " id = "" onclick="eval(document.calculator.display.value= memory)"> <input type = "button" value = " + " id = "" onclick="action1('+')"> </td> </tr> <tr ><br /> <!-- fifth row of calculator --> <td> <input type = "button" value = "   " id = ""> <input type = "button" value = "   " id = ""> <input type = "button" value = "   " id = ""> <input type = "button" value = " C " id = "" onclick="clearCalc()"> </td> </tr> </table> </body> </form> </html> /////////////////////////////////////////////////////////////////////////////////////// javascript memory = "0"; current = 0; operation =0; function newstring(digit) { current=document.calculator.display.value; if (digit >= 0 && digit <=9) { if(current==0) { current = digit; } else { current = current + digit; } } document.calculator.display.value=current; memory= current; } function action1(newaction) { operation = document.calculator.display.value; if (newaction == '+') { operation = newaction; } else if (newaction == '-') { operation = newaction; } else if (newaction == '*') { operation = newaction; } else if (newaction == '/') { operation = newaction; } memory= memory + operation; document.calculator.display.value=memory ; } function clearCalc() { document.calculator.display.value=null; num1= null; num2 = null; lastaction= null; action = null; } I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of the main floors of the main house: Part C: Gross Floor Area of the basement or cellar: Part D: Gross Floor Area of the attic: Part E. Gross Floor Area of all accessory structures except detached garages: (including cabanas, guest houses, caretaker's cottages, pool houses, sheds, barns, or other structures except a detached garage) Part F. Gross Floor Area of the garage: (not including basement garages) Part G: Total Floor Area: The Javascript involved in the calculations is as follows: [CODE] <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"> function checkAllInputFields(){ IsValidEntry(entireForm.A1.value) IsValidEntry(entireForm.A5.value) IsValidEntry(entireForm.A6.value) IsValidEntry(entireForm.A8.value) IsValidEntry(entireForm.A9.value) IsValidEntry(entireForm.B1.value) IsValidEntry(entireForm.B3.value) IsValidEntry(entireForm.B4.value) IsValidEntry(entireForm.B5.value) IsValidEntry(entireForm.C1.value) IsValidEntry(entireForm.C2.value) IsValidEntry(entireForm.C3.value) IsValidEntry(entireForm.D3.value) IsValidEntry(entireForm.D4.value) IsValidEntry(entireForm.D6.value) IsValidEntry(entireForm.D7.value) IsValidEntry(entireForm.D9.value) IsValidEntry(entireForm.D10.value) IsValidEntry(entireForm.D12.value) IsValidEntry(entireForm.D13.value) IsValidEntry(entireForm.D14.value) IsValidEntry(entireForm.D15.value) IsValidEntry(entireForm.E1.value) IsValidEntry(entireForm.F2.value) IsValidEntry(entireForm.F3.value) IsValidEntry(entireForm.F4.value) IsValidEntry(entireForm.F5.value) IsValidEntry(entireForm.F8.value) IsValidEntry(entireForm.F9.value) IsValidEntry(entireForm.F11.value) IsValidEntry(entireForm.F12.value) IsValidEntry(entireForm.F14.value) IsValidEntry(entireForm.F15.value) IsValidEntry(entireForm.F17.value) IsValidEntry(entireForm.F18.value) IsValidEntry(entireForm.F19.value) IsValidEntry(entireForm.F20.value) } function IsValidEntry(entry){ if(entry == "N/A to this property" || entry == "" || IsNumeric(entry)){ //do nothing } else{ alert(entry + " is not a number. Numbers must only contain digits and decimal points") } } function IsNumeric(sText){ var ValidChars = "0123456789."; var IsNumber=true; var Char; for (i = 0; i < sText.length && IsNumber == true; i++) { Char = sText.charAt(i); if (ValidChars.indexOf(Char) == -1) { IsNumber = false; } } return IsNumber; } //function which returns given number to 2 decimal places function roundTwo(x) { return (Math.round(x*Math.pow(10,2)))/Math.pow(10,2) } // function which returns given number to 4 decimal places function roundFour(x) { return (Math.round(x*Math.pow(10,4)))/Math.pow(10,4) } // function which calls calculation functions for all sections // this function is called whenever anything is changed on the form function calculateAll(){ checkAllInputFields() doZoneChange() doPartA() doPartB() doPartC() doPartD() doPartE() doPartF() doPartG() } //funtion that changes form according to zoning district function doZoneChange(){ if (entireForm.zone.value == "AA1"){ entireForm.A5.value = "N/A to this property" entireForm.A5.disabled = true entireForm.A6.value = "N/A to this property" entireForm.A6.disabled = true entireForm.A7.value = "---------------------" entireForm.A8.value = "N/A to this property" entireForm.A8.disabled = true entireForm.A9.value = "N/A to this property" entireForm.A9.disabled = true entireForm.A10.value = "---------------------" entireForm.A11.value = "---------------------" entireForm.A12.value = "---------------------" entireForm.A13.value = "---------------------" entireForm.A14.value = "N/A to this property" } else{ if (entireForm.A6.value == "N/A to this property"){ entireForm.A5.value = "" entireForm.A5.disabled = false entireForm.A6.value = "" entireForm.A6.disabled = false entireForm.A7.value = "" entireForm.A8.value = "" entireForm.A8.disabled = false entireForm.A9.value = "" entireForm.A9.disabled = false entireForm.A10.value = "" entireForm.A11.value = "" entireForm.A12.value = "" entireForm.A13.value = "" entireForm.A14.value = "" if (entireForm.zone.value == "A1"){ entireForm.A13.value = "1200" } else if (entireForm.zone.value == "A4" || entireForm.zone.value == "A5"){ entireForm.A13.value = "400" } else{ entireForm.A13.value = "700" } } else{ if (entireForm.zone.value == "A1"){ entireForm.A13.value = "1200" } else if (entireForm.zone.value == "A4" || entireForm.zone.value == "A5"){ entireForm.A13.value = "400" } else{ entireForm.A13.value = "700" } } } } function doPartA(){ // Calculates A2 var lotArea = entireForm.A1.value if(lotArea < 5000){ entireForm.A2.value = .43 } else if(lotArea < 10000){ entireForm.A2.value = (.43-(((lotArea-5000)/1000)*.016)) } else if(lotArea < 15000){ entireForm.A2.value = (.35-(((lotArea-10000)/1000)*.012)) } else if(lotArea < 30000){ entireForm.A2.value = (.29-(((lotArea-15000)/1000)*.006)) } else if(lotArea < 35000){ entireForm.A2.value = (.20-(((lotArea-30000)/1000)*.0045)) } else if(lotArea < 40000){ entireForm.A2.value = (.1775-(((lotArea-35000)/1000)*.003)) } else if(lotArea < 45000){ entireForm.A2.value = (.1625-(((lotArea-40000)/1000)*.002)) } else if(lotArea < 50000){ entireForm.A2.value = (.1525-(((lotArea-45000)/1000)*.0015)) } else if(lotArea < 76231){ entireForm.A2.value = .145 } else { entireForm.A2.value = 0 } entireForm.A2.value = roundFour(entireForm.A2.value) // Calculates A4 if(lotArea < 76231){ var maxFar = entireForm.A2.value entireForm.A4.value = (roundTwo(lotArea * maxFar)) } else{ entireForm.A4.value = 15000 } // Calculates A7 & A10 var RRightSetBack = entireForm.A5.value var PRightSetBack = entireForm.A6.value var RLeftSetBack = entireForm.A8.value var PLeftSetBack = entireForm.A9.value entireForm.A7.value = (PRightSetBack - RRightSetBack) if(entireForm.A7.value <= 0){ entireForm.A7.value = 0 } entireForm.A10.value = roundFour(PLeftSetBack - RLeftSetBack) if(entireForm.A10.value <= 0){ entireForm.A10.value = 0 } leftResult = entireForm.A10.value rightResult = entireForm.A7.value // Calculates A11 entireForm.A11.value = roundFour(Number(leftResult) + Number(rightResult)) // Calculates A12 entireForm.A12.value = roundFour(entireForm.A11.value * 100) // Calculates A14 if(entireForm.zone.value == "AA1"){ entireForm.A14.value = "0" } else{ var formA13 = entireForm.A13.value var formA12 = entireForm.A12.value if(Number(formA13) < Number(formA12)){ entireForm.A14.value = formA13 } else{ entireForm.A14.value = formA12 } } // Calculates A15 entireForm.A15.value = (Number(entireForm.A14.value) + Number(entireForm.A4.value)) } function doPartB(){ //------------------------------------- // Calculates B2 & B6 //-------------------------------------- var formB2 = (entireForm.B1.value * 2) var formB3 = entireForm.B3.value var formB4 = entireForm.B4.value var formB5 = entireForm.B5.value entireForm.B2.value = formB2 entireForm.B6.value = roundTwo(Number(formB2) + Number(formB3) + Number(formB4) + Number(formB5)) } var firstTimeThrough = 0 function doPartC(){ //------------------------------------- // Calculates C4 //-------------------------------------- var tallestPoint = entireForm.C1.value if( !(entireForm.C1.value == "") ){ firstTimeThrough = 1 } if ( (tallestPoint < 3) && (firstTimeThrough == 1) ){ entireForm.C2.value = "N/A to this property" entireForm.C2.disabled = true entireForm.C3.value = "N/A to this property" entireForm.C3.disabled = true entireForm.C4.value = "0" } else{ if (entireForm.C2.value == "N/A to this property"){ entireForm.C2.value = "" entireForm.C2.disabled = false entireForm.C3.value = "" entireForm.C3.disabled = false entireForm.C4.value = "" } else{ entireForm.C4.value = roundTwo(entireForm.C3.value * entireForm.C2.value) } } } function doPartD(){ if(entireForm.D1.value == "No" || entireForm.D2.value == "No"){ entireForm.D3.value = "N/A to this property" entireForm.D3.disabled = true entireForm.D4.value = "N/A to this property" entireForm.D4.disabled = true entireForm.D5.value = "---------------------" entireForm.D6.value = "N/A to this property" entireForm.D6.disabled = true entireForm.D7.value = "N/A to this property" entireForm.D7.disabled = true entireForm.D8.value = "---------------------" entireForm.D9.value = "N/A to this property" entireForm.D9.disabled = true entireForm.D10.value = "N/A to this property" entireForm.D10.disabled = true entireForm.D11.value = "---------------------" entireForm.D12.value = "N/A to this property" entireForm.D12.disabled = true entireForm.D13.value = "N/A to this property" entireForm.D13.disabled = true entireForm.D14.value = "N/A to this property" entireForm.D14.disabled = true entireForm.D15.value = "N/A to this property" entireForm.D15.disabled = true entireForm.D16.value = "0" } else if(entireForm.D3.value == "N/A to this property"){ entireForm.D3.value = "" entireForm.D3.disabled = false entireForm.D4.value = "" entireForm.D4.disabled = false entireForm.D5.value = "" entireForm.D6.value = "" entireForm.D6.disabled = false entireForm.D7.value = "" entireForm.D7.disabled = false entireForm.D8.value = "" entireForm.D9.value = "" entireForm.D9.disabled = false entireForm.D10.value = "" entireForm.D10.disabled = false entireForm.D11.value = "" entireForm.D12.value = "" entireForm.D12.disabled = false entireForm.D13.value = "" entireForm.D13.disabled = false entireForm.D14.value = "" entireForm.D14.disabled = false entireForm.D15.value = "" entireForm.D15.disabled = false entireForm.D16.value = "" } else{ //------------------------------------- // Calculates D5 //-------------------------------------- if(entireForm.D3.value == "0"){ entireForm.D4.value = "0" entireForm.D5.value = "0" } else{ entireForm.D5.value = roundFour((entireForm.D4.value / entireForm.D3.value)*100) } //------------------------------------- // Calculates D8 //-------------------------------------- if(entireForm.D6.value == "0"){ entireForm.D7.value = "0" entireForm.D8.value = "0" } else{ entireForm.D8.value = roundFour((entireForm.D7.value / entireForm.D6.value)*100) } //------------------------------------- // Calculates D11 //-------------------------------------- if(entireForm.D9.value == "0"){ entireForm.D10.value = "0" entireForm.D11.value = "0" } else{ entireForm.D11.value = roundFour((entireForm.D10.value / entireForm.D9.value)*100) } if(entireForm.D12.value == "0"){ entireForm.D13.value = "0" } if(entireForm.D4.value == ""){ entireForm.D5.value = "NMI" } if(entireForm.D7.value == ""){ entireForm.D8.value = "NMI" } if(entireForm.D10.value == ""){ entireForm.D11.value = "NMI" } //------------------------------------- // Calculates D16 //-------------------------------------- if(entireForm.D5.value < 30 && entireForm.D8.value < 30 && entireForm.D11.value < 30 && entireForm.D12.value >= 5 && entireForm.D13.value >= 5){ entireForm.D14.value = "N/A to this property" entireForm.D14.disabled = true entireForm.D15.value = "N/A to this property" entireForm.D15.disabled = true entireForm.D16.value = "N/A to this property" } else if (entireForm.D14.value == "N/A to this property"){ entireForm.D14.value = "" entireForm.D14.disabled = false entireForm.D15.value = "" entireForm.D15.disabled = false entireForm.D16.value = "" } if (!(entireForm.D14.value == "") && !(entireForm.D15.value == "") && !(entireForm.D15.value == "N/A to this property") && !(entireForm.D15.value == "N/A to this property")){ entireForm.D16.value = (entireForm.D14.value - entireForm.D15.value) } } } function doPartE(){ //------------------------------------- // Calculates E2 //-------------------------------------- entireForm.E2.value = entireForm.E1.value } function doPartF(){ if(entireForm.F0.value == "No"){ entireForm.F3.value = "0" entireForm.F3.disabled = true entireForm.F4.value = "0" entireForm.F4.disabled = true entireForm.F5.value = "0" entireForm.F5.disabled = true entireForm.F6.value = "No" entireForm.F6.disabled = true entireForm.F7.disabled = true } else if(entireForm.F0.value == "Yes" && entireForm.F3.value == "0"){ entireForm.F3.value = "" entireForm.F3.disabled = false entireForm.F4.value = "" entireForm.F4.disabled = false entireForm.F5.value = "" entireForm.F5.disabled = false entireForm.F6.value = "Yes" entireForm.F6.disabled = false entireForm.F7.disabled = false } if(entireForm.F1.value == "No"){ entireForm.F2.value = "0" entireForm.F2.disabled = true } else if(entireForm.F1.value == "Yes" && entireForm.F2.value == "0"){ entireForm.F2.value = "" entireForm.F2.disabled = false } if(entireForm.F6.value == "No" || entireForm.F7.value == "No"){ entireForm.F8.value = "N/A to this property" entireForm.F8.disabled = true entireForm.F9.value = "N/A to this property" entireForm.F9.disabled = true entireForm.F10.value = "---------------------" entireForm.F11.value = "N/A to this property" entireForm.F11.disabled = true entireForm.F12.value = "N/A to this property" entireForm.F12.disabled = true entireForm.F13.value = "---------------------" entireForm.F14.value = "N/A to this property" entireForm.F14.disabled = true entireForm.F15.value = "N/A to this property" entireForm.F15.disabled = true entireForm.F16.value = "---------------------" entireForm.F17.value = "N/A to this property" entireForm.F17.disabled = true entireForm.F18.value = "N/A to this property" entireForm.F18.disabled = true entireForm.F19.value = "N/A to this property" entireForm.F19.disabled = true entireForm.F20.value = "N/A to this property" entireForm.F20.disabled = true entireForm.F21.value = "0" } else if(entireForm.F8.value == "N/A to this property"){ entireForm.F8.value = "" entireForm.F8.disabled = false entireForm.F9.value = "" entireForm.F9.disabled = false entireForm.F10.value = "" entireForm.F11.value = "" entireForm.F11.disabled = false entireForm.F12.value = "" entireForm.F12.disabled = false entireForm.F13.value = "" entireForm.F14.value = "" entireForm.F14.disabled = false entireForm.F15.value = "" entireForm.F15.disabled = false entireForm.F16.value = "" entireForm.F17.value = "" entireForm.F17.disabled = false entireForm.F18.value = "" entireForm.F18.disabled = false entireForm.F19.value = "" entireForm.F19.disabled = false entireForm.F20.value = "" entireForm.F20.disabled = false entireForm.F21.value = "0" } else{ //------------------------------------- // Calculates F10 //-------------------------------------- if(entireForm.F8.value == "0"){ entireForm.F9.value = "0" entireForm.F10.value = "0" } else{ entireForm.F10.value = roundFour((entireForm.F9.value / entireForm.F8.value)*100) } //------------------------------------- // Calculates F13 //-------------------------------------- if(entireForm.F11.value == "0"){ entireForm.F12.value = "0" entireForm.F13.value = "0" } else{ entireForm.F13.value = roundFour((entireForm.F12.value / entireForm.F11.value)*100) } //------------------------------------- // Calculates F16 //-------------------------------------- if(entireForm.F14.value == "0"){ entireForm.F15.value = "0" entireForm.F16.value = "0" } else{ entireForm.F16.value = roundFour((entireForm.F15.value / entireForm.F14.value)*100) } if(entireForm.F17.value == "0"){ entireForm.F18.value = "0" } if(entireForm.F9.value == ""){ entireForm.F10.value = "NMI" } if(entireForm.F12.value == ""){ entireForm.F13.value = "NMI" } if(entireForm.F15.value == ""){ entireForm.F16.value = "NMI" } //------------------------------------- // Calculates F21 //-------------------------------------- if(entireForm.F10.value < 30 && entireForm.F13.value < 30 && entireForm.F16.value < 30 && entireForm.F17.value >= 5 && entireForm.F18.value >= 5){ entireForm.F19.value = "N/A to this property" entireForm.F19.disabled = true entireForm.F20.value = "N/A to this property" entireForm.F20.disabled = true entireForm.F21.value = "N/A to this property" } else if (entireForm.F19.value == "N/A to this property"){ entireForm.F19.value = "" entireForm.F19.disabled = false entireForm.F20.value = "" entireForm.F20.disabled = false entireForm.F21.value = "" } if (!(entireForm.F19.value == "") && !(entireForm.F20.value == "") && !(entireForm.F20.value == "N/A to this property") && !(entireForm.F20.value == "N/A to this property")){ entireForm.F21.value = roundTwo(entireForm.F19.value - entireForm.F20.value) } } //------------------------------------- // Calculates F23 //-------------------------------------- entireForm.F22.value = entireForm.A1.value if(entireForm.F22.value <= 10000){ entireForm.F23.value = 250 } else{ entireForm.F23.value = 400 } //------------------------------------- // Calculates F24 & F25 //-------------------------------------- entireForm.F24.value = roundFour(Number(entireForm.F2.value) + Number(entireForm.F3.value)) entireForm.F25.value = roundFour(entireForm.F24.value - entireForm.F23.value) //------------------------------------- // Calculates F26 //-------------------------------------- if(entireForm.F25.value < 0){ entireForm.F25.value = 0 } entireForm.F26.value = roundTwo(Number(entireForm.F4.value) + Number(entireForm.F5.value) + Number(entireForm.F21.value) + Number(entireForm.F25.value)) } function doPartG(){ //------------------------------------- // Calculates G1 & G2 //-------------------------------------- entireForm.G1.value = roundTwo(Number(entireForm.B6.value) + Number(entireForm.C4.value) + Number(entireForm.D16.value) + Number(entireForm.E2.value) + Number(entireForm.F26.value)) entireForm.G2.value = entireForm.A15.value //------------------------------------- // Calculates G4 & G5 //-------------------------------------- if (entireForm.G1.value > entireForm.G2.value){ entireForm.G3.value = "Yes" entireForm.G4.value = roundTwo(entireForm.G1.value - entireForm.G2.value) } else{ entireForm.G3.value = "No" entireForm.G4.value = "Not exceeded" } } </SCRIPT> [CODE] I have attached the html file as a .txt file for reference, and I have attached a .txt document including all the values I used for testing. Why is it showing NaN values? Any help would be greatly appreciated. Thank You. hello, ive been wondering about this possibility for a while. i havent been able to find an answer to it using google, but im not very knowledgable about javascript in the first place. what id like to do is make a webpage that has one frame (or div if its necessary) that resizes based on a percentage of the size of the user's window upon loading, but always keeps the same aspect ratio. to get more specific: i want that second frame (or div) to have a locked 4:3 ratio. can this be done? Im trying to get this script working on this game on Kongregate Im experimenting on this I took some script which resizes the whole game to its maximizations and I thought it needs to keep aspect ratio anyway I tried to modify all I can but all I get are gaps and game gets aligned to left some reason I couldnt find out I based it on this http://userscripts.org/scripts/show/34214 so can you tell me what I am doing wrong to get those gaps and what I need to do to make game not get aligned to left Im working like zomg This is what I tried but alas, it does not work por que no? This is located on my main index file which if the result of z is greater than 1, I would have the user stay at this page as opposed to redirecting to the portrait design. I have optimized the website for mobile use so there is a reason of having two different pages, the wide screen has extra tools / other stuff that isn't shown on the portrait not because of size/responsive design but becasue I want it to be this way. Anyway... "What's the situation captain?" This is located above my <style> tag Code: <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script language="JavaScript" type="text/javascript"> <script> function redirect() { var x = screen.width; var y = screen.height; var z = x/y; if (z<1) { window.location.replace("alternate destination"); }; else ( ){ // stay here }; }; </script> Reply With Quote 12-21-2014, 01:58 AM #2 Old Pedant View Profile View Forum Posts Supreme Master coder! Join Date Feb 2009 Posts 28,311 Thanks 82 Thanked 4,754 Times in 4,716 Posts You have extra semicolons in there. In general, don't put a semicolon directly after a right brace }; There are a couple of exceptions, but they don't apply here. What's the point in having the else?? Below is the code for a calculator using script. can one help me to explain me the colored line?? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> </body> <script language="javascript" type="text/javascript"> function multiply() { a=Number Quote: Quote: (document.calculator.number1.value) ; b=Number(document.calculator.number2.value); c=a*b; document.calculator.total.value=c; } function addition(){ a=Number(document.calculator.number1.value); b=Number(document.calculator.number2.value); c=a+b; document.calculator.total.value=c; } function subtraction(){ a=Number(document.calculator.number1.value); b=Number(document.calculator.number2.value); c=a-b; document.calculator.total.value=c; } function division(){ a=Number(document.calculator.number1.value); b=Number(document.calculator.number2.value); c=a/b; document.calculator.total.value=c; } function modulus(){ a=Number(document.calculator.number1.value); b=Number(document.calculator.number2.value); c=a%b; document.calculator.total.value=c; } </script> </head> <body> <form name="calculator"> Number 1: <input type="text" name="number1"> Number 2: <input type="text" name="number2"> Get Result: <input type="text" name="total"> <input type="button" value="ADD" onclick="javascript:addition();"> <input type="button" value="SUB" onclick="javascript:subtraction();"> <input type="button" value="MUL" onclick="javascript:multiply();"> <input type="button" value="DIV" onclick="javascript:division();"> <input type="button" value="MOD" onclick="javascript:modulus();"> </form> </body> </html> Hi , this is my bmi calculator script <HEAD> <script type="text/javascript"> <!-- Begin script of spillo3000 function calculateBMI() { var weight = eval(document.form.weight.value) var height = eval(document.form.height.value) var height2 = height / 100 var BMI = weight / (height2 * height2) if(weight == "" || isNaN(weight) || height2 == "" || isNaN(height2)) { alert("inserisci un valore"); return; } else { document.form.BodyMassIndex.value=custRound(BMI,1); if(BMI < 18.5) { document.getElementById('feedback').innerHTML = 'Underweight '; }else if(BMI >=18.5 && BMI < 29.9) { document.getElementById('feedback').innerHTML = 'Normal '; }else if(BMI > 29.9) { document.getElementById('feedback').innerHTML = 'Overweight '; } } } function custRound(x,places) { return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places) } function resetAll(){ document.getElementById('feedback').innerHTML=""; return true; } // End --> </script> </HEAD> <!-- --> <BODY> <div align="left"> <form name="form" id="form"> <input type="Text" name="weight" size="4"> Peso (Kg)<br> <input type="Text" name="height" size="4"> Altezza (Cm)<br> <input type="Text" name="BodyMassIndex" id="BodyMassIndex" size="4"> BMI<br> <input type="Text" name="feedback" id="feedback" style="padding:10px 0 20px 0; border:none; font-weight:600; color:#555555; font-family:verdana;" ><br> <input type="button" style="font-size: 8pt" value="Calcola" onClick="calculateBMI()" name="button"> <input type="reset" style="font-size: 8pt" value="Reset" onclick="resetAll()" name="button"> </form> </div> <!-- Script Size: I would want to show writing that it says if is in overweight, norms or I underweigh, but the script does not work. could someone help me? please How could i make a calculator that changes three values when a vertex on an image moves. Example : When a point moves on the triangle it changes the cost quality and time. I have this start and need the meat for the concept to work.<!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <p><img src="Assets/Project-triangle.svg.png" width="500" height="492" alt=""/>-------------------------------------------<img src="Assets/triangle.gif" width="224" height="210" alt=""/></p> <p> <label for="textfield">Cost :</label> <input type="text" name="textfield" id="textfield"> <label for="textfield2"> Time :</label> <input type="text" name="textfield2" id="textfield2"> <label for="textfield3">Quality :</label> <input type="text" name="textfield3" id="textfield3"> </p> <p> </p> </body> </html> Help would be greatly appreciated! I'm using the following code to calculate an age from the given date of birth however it works for some DOB's, gives wrong answers for some and undefined for others, can someone help me? Code: <div style="font-size:13px; font-family:Verdana;"> <script> <!-- /*Darren McGrady */ var current= new Date() var day = current.getDate() var month = current.getMonth() + 1 var year = current.getFullYear() var a = 29 var b = 12 var c = 1980 var age if (month < b) age = (year-c)-1 if ((month == b) && (day < a)) age = (year-c)-1 else age = year-c document.write(age); //--> </script> </div> Hi all. I'm having a bit of a problem returning a different value each time a different button is pressed for calculator. What am I doing wrong? Code: <script> function calc(btname){ for(var x=0;x<=11;x++){ if(x==0){ btname="zero"; }else if(x==1){ btname="one"; }else if(x==2){ btname="two"; }else if(x==3){ btname="three"; }else if(x==4){ btname="four"; }else if(x==5){ btname="five"; }else if(x==6){ btname="six"; }else if(x==7){ btname="seven"; }else if(x==8){ btname="eight"; }else if(x==9){ btname="nine"; }else if(x==10){ btname="multiply"; }else if(x==11){ btname="divide"; } if('document.forms.test.'+btname+'.onClick'){ alert(btname); } } } function scalc(){ var sign=""; var btname=""; document.write('<form name="test">'); for(var x=0;x<=11;x++){ if(x==0){ btname="zero"; }else if(x==1){ btname="one"; }else if(x==2){ btname="two"; }else if(x==3){ btname="three"; }else if(x==4){ btname="four"; }else if(x==5){ btname="five"; }else if(x==6){ btname="six"; }else if(x==7){ btname="seven"; }else if(x==8){ btname="eight"; }else if(x==9){ btname="nine"; }else if(x==10){ btname="multiply"; sign = "*"; }else if(x==11){ btname="divide"; sign = "/"; } if(x<10){ document.write('<input type="button" size="30" name="'+btname+'" value="'+x+'" onClick="calc(this);"> '); }else if(x>9 && x<12){ document.write('<input type="button" size="30" name="'+btname+'" value="'+sign+'" onClick="calc(this);"> '); } if(x==2 || x == 5 || x == 8){ document.write('<br>'); } } document.write('</form>'); } scalc(); </script> ANY help is GREATLY appreciated! I'm not sure what the code is to add this to javascript. I don't know if anyone can help me with this? Each numeric button should change the input box up at the top to whatever. When one of the operator buttons (+,-,*,/) is pressed, the value in the input box at the top should change to 0. Before an operator button is pressed, the value should be stored in a variable to keep track of the left-hand side of the equation. After it is pressed, the value should be stored in the right hand side variable. Thanks Hi all master and expert coders I am working on a calculator which using the instructor's codes from the school. The problem that I having right now is the operator. I have successfully complete only the addition, but could not manage on subtraction and the rest operator. I hope experienced codes would help me out on this. Below is my codes. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <script type="text/javascript"> function calculate() { var value1 = parseInt(validate(document.getElementById("value1").value)); var value2 = parseInt(validate(document.getElementById("value2").value)); radioButtonCheck(); checkboxCheck(); dropDownListCheck(); document.getElementById("result").value = addition(value1, value2); document.getElementById("result").value = subtraction(value1, value2); document.getElementById("result").value = multiplication(value1, value2); document.getElementById("result").value = division(value1, value2); } function validate(value) { if (value == null || value == "") { alert("Required Field"); return 0; } else if (isNaN(value)) { alert("Must be a number"); return 0; } else return value; } function addition(value1, value2){ return value1 + value2; } // trying to use the same addition return statement but fail. function radioButtonCheck() { if ((document.getElementById("radio1").checked == false) && (document.getElemtById("radio2").checked == false)) { alert("Please select a radio button"); valid = false; } } function checkboxCheck() { if ((document.getElementById("cbox1").checked == false) && (document.getElementById("cbox2").checked == false)) { alert("Please select a checkbox"); valid = false; } } function dropDownListCheck() { if ((document.getElementById("ddlOperator").selectedIndex == 0)) { alert("Please select a operator"); valid = false; } } </script> <title>Calculator By Alsus</title> </head> <body> <h2>Calculator</h2> <table border = "1"> <tr> <td>Value 1:</td> <td><input type="text" id="value1" name="value1" /></td> </tr> <tr> <td>Value 2:</td> <td><input type="text" id="value2" name="value2" /></td> </tr> <tr> <td>Operator:</td> <td><input type="text" id="Operator" name="Operator" /></td> </tr> <tr> <td><input type="radio" id="radio1" name="radio1" value="radio1" />Radio1<br /></td> <td><input type="radio" id="radio2" name="radio2" value="radio2" />Radio2<br /></td> </tr> <tr> <td><input type="checkbox" id="cbox1" name="cbox1" value="cbox1" />Checkbox 1<br /></td> <td><input type="checkbox" id="cbox2" name="cbox2" value="cbox2" />Checkbox 2<br /></td> </tr> <tr> <td> <select id="ddlSelect"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> </td> <td> </td> </tr> <tr> <td>Result:</td> <td><input type="text" id="result" name="result" /></td> </tr> </table> <input type="submit" id="btn_sub" name="btn_sub" value="Submit" onclick="calculate()" /> <input type="reset" id="btn_res" name="btn_res" value="Reset" /> </body> </html> I have been having problems adding checkboxes and option selects in the same function.. Here's what I have. Code: <script type="text/javascript"> function bonuscalc() { var aaaa = document.getElementById("aaaa").value; var bbbb = document.getElementById("bbbb").value; var cccc = document.getElementById("cccc").value; if (dddd.checked){ var dddd = document.dddd.value = 2; } else { var dddd = document.dddd.value = 1; } var eeee = document.getElementById("eeee").value; var bonus = aaaa* bbbb * dddd * cccc * eeee; var roundbonus = Math.round(bonus*10)/10; document.getElementById("roundbonus").value = roundbonus; } </script> Javascript first.. The part I'm not sure about is the if/else part for checbox. Now the html: Code: <form action="" id="calc1"> <table width="100%"> <tbody> <tr> <td>A status: <select id="aaaa"> <option value="1.1">A1</option> <option value="1.02">A2</option> <option value="1">A3</option> <option value="0.95">A4</option> </select> </td> <td>B status: <select id="bbbb"> <option value="1">0%</option> <option value="0.99">1%</option> <option value="0.98">2%</option> <option value="0.97">3%</option> <option value="0.96">4%</option> <option value="0.95">5%</option> <option value="0.94">6%</option> <option value="0.93">7%</option> <option value="0.92">8%</option> <option value="0.91">9%</option> <option value="0.90">10%</option> </select> </td> <td>E status:<input id="eeee" type="text" /> </td> <td>D status: <input id="dddd" name="dddd" type="checkbox"> </td> <td>C status: <select id="cccc"> <option value="1">No</option> <option value="1.2">Yes</option> </select> </td> </tr> <tr> <td width="100%"> <input type="button" value="Submit" onclick="bonuscalc()" /> <br /> Result: <input type="text" readonly="readonly" id="roundbonus" /> </td> </tr> </tbody> </table> </form> I hope it's not too confusing. The idea is to assign value 2 if the checkbox is checked and value 1 is it isn't.. aaaaaa
Hi everyone, I am currently doing a project with a calculator and I am having a bit of trouble trying to figure something out and was wondering if I could get some help with it or possibly even a good site to read up on it. I am trying to make it so my calculator is disabled on startup. I can only seem to get my textbox disabled but I need everything disabled except the ON button.. so whey they click the ON button everything is enabled. when they click the OFF button everything is wiped and disabled.. I'm not to familiar with that yet so all the help is greatly appreciated.. I'm not sure if I'm going on the right direction with this but heres my code... Code: var value function start() { form1.tb1.disabled = true; } onload = start; function enable() { form1.tb1.disabled = false } function shownumber(value){ document.form1.tb1.value=document.form1.tb1.value+value document.form1.plus.disabled=false document.form1.minus.disabled=false document.form1.times.disabled=false document.form1.divide.disabled=false } function showoperator(value){ document.form1.tb1.value=document.form1.tb1.value+value } function evalit(){ document.form1.tb1.value=eval(document.form1.tb1.value) } and my body is Code: <table width="210" border="1" bgcolor="#0000"> <form name="form1"> <tr> <td align="center"> <table width="173" border="0" cellspacing="0" cellpadding="0" height="130"> <tr> </tr> <tr> <td colspan="3"> <input type="text" name="tb1" size="20"> </td> <td width="53" height="0"> <font color="#FFFFFF"> <input type="button" name="clear" value=" c " onClick="document.form1.tb1.value=''"> </font></td> </tr> <tr> <td width="53" height="0"> <input type="button" name="7" value=" 7 " onClick="shownumber('7')"> </td> <td width="53" height="0"> <input type="button" name="eight" value=" 8 " onClick="shownumber('8')"> </td> <td width="53" height="0"> <input type="button" name="nine" value=" 9 " onClick="shownumber('9')"> </td> <td width="53" height="0"> <font color="#FFFFFF"> <input type="button" value=" / " name="divide" onClick="showoperator('/')"> </font></td> </tr> <tr> <td width="53" height="0"> <input type="button" name="four" value=" 4 " onClick="shownumber('4')"> </td> <td width="53" height="0"> <input type="button" name="five" value=" 5 " onClick="shownumber('5')"> </td> <td width="53" height="0"> <input type="button" name="six" value=" 6 " onClick="shownumber('6')"> </td> <td width="53" height="0"> <font color="#FFFFFF"> <input type="button" name="times" value=" x " onClick="showoperator('*')"> </font></td> </tr> <tr> <td width="53" height="0"> <input type="button" name="one" value=" 1 " onClick="shownumber('1')"> </td> <td width="53" height="0"> <input type="button" name="two" value=" 2 " onClick="shownumber('2')"> </td> <td width="54" height="0"> <input type="button" name="three" value=" 3 " onClick="shownumber('3')"> </td> <td width="72" height="0"> <font color="#FFFFFF"> <input type="button" name="minus" value=" - " onClick="showoperator('-')"> </font></td> </tr> <tr> <td width="53" height="0"> <input type="button" name="zero" value=" 0 " onClick="shownumber('0')"> </td> <td width="53" height="0"> </td> <td width="54" height="0"> <input type="button" value=" + " name="plus" onClick="showoperator('+')"> </td> <td width="72" height="0"> <font color="#FFFFFF"> <input type="button" name="calculate" value=" = " onClick="evalit()" </font></td> <tr> <td> <input type="button" id="but1" value="On" onClick="enable();"> <input type="button" id="button2" value="Off"> </td> </tr> </table> </td> </tr> </form> </body> </html> I've got this code below, it seems ok, but sometimes, the multiplication does not work..i don't know why... pls help me..thanks btw, this is my 1st javascript "application", so, im really new into this thanks in advance for any help Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My First JavaScript Application</title> <style type="text/css"> td { text-align:center } table { background:#660033 } body { background-color:#666; text-wrap:supress; } h1 { color:#FFFFFF; } .ex { background-color:#99F; color:#660000; font:Verdana, Geneva, sans-serif; font-size:20px; text-align:right; } .ec { background-color:#CCC; color:#660033; font:Arial, Helvetica, sans-serif; font-size:18px; } </style> <script type="text/javascript"> var num1 = 0; var ope = 0; var num2 = 0; var ek = 0 function addOnScr1() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+1; document.getElementById("nums").value = txt; } function addOnScr2() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+2; document.getElementById("nums").value = txt; } function addOnScr3() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+3; document.getElementById("nums").value = txt; } function addOnScr4() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+4; document.getElementById("nums").value = txt; } function addOnScr5() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+5; document.getElementById("nums").value = txt; } function addOnScr6() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+6; document.getElementById("nums").value = txt; } function addOnScr7() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+7; document.getElementById("nums").value = txt; } function addOnScr8() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+8; document.getElementById("nums").value = txt; } function addOnScr9() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+9; document.getElementById("nums").value = txt; } function addOnScr0() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+0; document.getElementById("nums").value = txt; } function addOnScrp() { document.getElementById("neg").disabled=true; document.getElementById("point").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+"."; document.getElementById("nums").value = txt; } function addOnScrn() { var txt = document.getElementById("nums").value; txt = txt+"-"; document.getElementById("nums").value = txt; } function add() { document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek=1; if(num1==0) { num1 = parseFloat(document.getElementById("nums").value); } else { ent(); } ope = 1; } function diff() { document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek=1; if(num1==0) { num1 = parseFloat(document.getElementById("nums").value); } else { ent(); } ope = 2; } function prod() { document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek=1; if(num1==0) { num1 = parseFloat(document.getElementById("nums").value); } else { ent(); } ope = 3; } function quo() { document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek=1; if(num1==0) { num1 = parseFloat(document.getElementById("nums").value); } else { ent(); } ope = 4; } function clr() { num1 = 0; num2 = 0; ope = 0; document.getElementById("nums").value = ""; document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek = 0; } function ent() { ek = 1; var ans; num2 = parseFloat(document.getElementById("nums").value); switch(ope) { case 1: ans = num1 + num2; document.getElementById("nums").value = ans; num1 = ans; break; case 2: ans = num1 - num2; document.getElementById("nums").value = ans; num1 = ans; break; case 3: ans = num1 * num2; document.getElementById("nums").value = ans; num1 = ans; break; case 4: ans = num1 / num2; document.getElementById("nums").value = ans; num1 = ans; break; default: document.getElementById("nums").value = "???"; } } function enterz() { document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek = 1; var ans; num2 = parseFloat(document.getElementById("nums").value); switch(ope) { case 1: ans = num1 + num2; document.getElementById("nums").value = ans; num1 = 0; break; case 2: ans = num1 - num2; document.getElementById("nums").value = ans; num1 = 0; break; case 3: ans = num1 * num2; document.getElementById("nums").value = ans; num1 = 0; break; case 4: ans = num1 / num2; document.getElementById("nums").value = ans; num1 = 0; break; default: document.getElementById("nums").value = "???"; } } </script> </head> <body><center> <form><h1> Calculator</h1> <table border="0"> <tr><th colspan="4"><input type="text" class = "ex" id = "nums" name = "nums" /></th></tr> <tr><td><button type="button" class = "ec" onclick="addOnScr1()">1</button></td> <td><button type="button" class = "ec" onclick="addOnScr2()">2</button></td> <td><button type="button" class = "ec" onclick="addOnScr3()">3</button></td> <td><button type="button" class = "ec" onclick="add()">+</button></td> </tr> <tr><td><button type="button" class = "ec" onclick="addOnScr4()">4</button></td> <td><button type="button" class = "ec" onclick="addOnScr5()">5</button></td> <td><button type="button" class = "ec" onclick="addOnScr6()">6</button></td> <td><button type="button" class = "ec" onclick="diff()">--</button></td></tr> <tr><td><button type="button" class = "ec" onclick="addOnScr7()">7</button></td> <td><button type="button" class = "ec" onclick="addOnScr8()">8</button></td> <td><button type="button" class = "ec" onclick="addOnScr9()">9</button></td> <td><button type="button" class = "ec" onclick="prod()">*</button></td></tr> <tr><td><button type="button" class = "ec" id="neg" onclick="addOnScrn()">-</button></td> <td><button type="button" class = "ec" onclick="addOnScr0()">0</button></td> <td><button type="button" class = "ec" id="point" onclick="addOnScrp()">.</button></td> <td><button type="button" class = "ec" onclick="quo()">/</button></td></tr> <tr><td></td> <td><button type="button" class = "ec" onclick="enterz()">=</button></td> <td><button type="button" class = "ec" onclick="clr()">C</button></td><td> </td></tr> </form> </body> </html> |