JavaScript - Simple Javascript Calculator -
Hi there, me again, lol.
I'm now working on a simple JavaScript calculator script, and here's what I have so far. 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>Untitled Document</title> </head> <body> <form name="Calculator"> <input type="text" name="Output" size="22"> <br/> <br/> <input name="1" type="button" value=" 1 " onClick="document.Calculator.Output.value += '1'" /> <input name="1" type="button" value=" 2 " onClick="document.Calculator.Output.value += '2'" /> <input name="1" type="button" value=" 3 " onClick="document.Calculator.Output.value += '3'" /> <input name="1" type="button" value=" 4 " onClick="document.Calculator.Output.value += '4'" /> <br/> <input name="1" type="button" value=" 5 " onClick="document.Calculator.Output.value += '5'" /> <input name="1" type="button" value=" 6 " onClick="document.Calculator.Output.value += '6'" /> <input name="1" type="button" value=" 7 " onClick="document.Calculator.Output.value += '7'" /> <input name="1" type="button" value=" 8 " onClick="document.Calculator.Output.value += '8'" /> <br/> <input name="1" type="button" value=" 9 " onClick="document.Calculator.Output.value += '9'" /> <input name="1" type="button" value=" 0 " onClick="document.Calculator.Output.value += '0'" /> <input name="1" type="button" value=" + " onClick="document.Calculator.Output.value += ' + '" /> <input name="1" type="button" value=" - " onClick="document.Calculator.Output.value += ' - '" /> <br/> <input name="1" type="button" value=" C " onClick="document.Calculator.Output.value = ''" /> <input name="1" type="button" value=" / " onClick="document.Calculator.Output.value += ' / '" /> <input name="1" type="button" value=" * " onClick="document.Calculator.Output.value += ' * '" /> <input name="1" type="button" value=" = " onClick="document.Calculator.Output.value += ' = ' + eval(Calculator.Output.value)" /> </form> </body> </html> The calculator itself is fully working functionally, feel free to test to your heart's content. :P The problem I'm having is not really a problem per se, more of a behaviour I don't want in the script. After clicking the = button and getting the results of the calculation displayed, clicking another number will just concatenate (I believe that's the right term) that to the result of the sum, and I don't want this to happen. If I've just done a calculation, I want the results of it to be cleared as soon as I hit another button. My guesses are that I would need to do this via some function activated by the = button. Is there some sort of way I can have the output of a sum cleared after a calculation has been done, so that the next calculation becomes a seperate one? If you're still not getting what I'm saying entirely, just let me know, I will try to provide screenshots. Thanks. Similar TutorialsI'm new to javascript but not to programming. Here is my code: PHP Code: PHP Code: print "<form name='goldcalculator'>"; print "<input type='hidden' name='goldspot' value='$goldvalue'>"; print "<select name='gunit' onchange='updateTotal();'>"; print "<option value='20'>Pennyweight (DWT)</option>"; print "<option value='31.1'>Grams (g)</option>"; print "</select><br>"; print "10K <input type='text' name='10k' onchange='updateTotal();'><br>"; print "14K <input type='text' name='14k' onchange='updateTotal();'><br>"; print "18K <input type='text' name='18k' onchange='updateTotal();'><br>"; print "22K <input type='text' name='22k' onchange='updateTotal();'><br>"; print "24K <input type='text' name='24k' onchange='updateTotal();'><br>"; print "TOTAL <input type='text' name='totalprice'>"; print "</form>"; Code: <script language="JavaScript"> function updateTotal() { var u = document.goldcalculator.gunit.value; var spotprice = document.goldcalculator.goldspot.value / u; var gold10 = document.goldcalculator.10k.value; var gold14 = document.goldcalculator.14k.value; var gold18 = document.goldcalculator.18k.value; var gold22 = document.goldcalculator.22k.value; var gold24 = document.goldcalculator.24k.value; var calculatedPrice = document.goldcalculator.totalprice.value; calculatedPrice = ((spotprice*.999*gold24)+(spotprice*.916*gold22)+(spotprice*.75*gold18)+(spotprice*.585*gold14)+(spotprice*.417*gold10)); } </script> I don't know where to begin to debug this. It's very simple. The $goldvalue variable is non-editable and is pulled off my MySQL database and that works. They fill in the inputs via the PHP form and then it basically adds them up and spits out the total. Can someone tell me what's wrong? Ok so I know basic html and css, no problem but i just started learning javascript and this class is kicking my but! I have this assignment due tonight and I'm taking this class online so there not much help beyond google. I know it's "simple' but I cannot figure this out for the life of me!!! It driving me bonkers, and I really don't want to fail. So any help would be more that appreciated! K here is the html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Change Calculator</title> <link rel="stylesheet" type="text/css" href="default.css" /> <script type="text/javascript" src="calculate_coins.js"></script> </head> <body> <div id="content"> <h1>Change Calculator</h1> <label>Enter number of cents (0-99):</label> <input type="text" id="cents" /> <input type="button" value="Calculate" name="calculate" id="calculate" /><br /> <p> </p> <label>Quarters:</label> <input type="text" id="quarters" class="disabled" disabled="disabled" /><br /> <label>Dimes:</label> <input type="text" id="dimes" class="disabled" disabled="disabled" /><br /> <label>Nickels:</label> <input type="text" id="nickels" class="disabled" disabled="disabled" /><br /> <label>Pennies:</label> <input type="text" id="pennies" class="disabled" disabled="disabled" /><br /> <p> </p> </div> </body> </html> i need to get it to function properly. Please HELLP ME!!! I am trying to make a simple calculator to calculate dBm, Vpp, Vrms. It is basically a conversion calculator. I am brand new to JavaScript and tried to follow a tutorial but I'm coming up short. I will post both my .js and .html code below. My issue is that when I hit the "Calculate" button nothing happens. Here is the javascript. It has a main calculate function and a function that gives me a log base 10 method. Code: //Main Calculation function calculate() { if(dBm === null && Vpp === null){ var calc = document.getElementById('ecalc'); var Vpp2 = (4*Vrms)/Math.sqrt(2); var dBm2 = 20 * (log10(Vpp/(Math.sqrt(0.008*Z)))); var Vrms2 = Vrms; return false; } else if(Vpp === null && Vrms === null){ var calc = document.getElementById('ecalc'); var Vpp2 = Math.sqrt(0.008*Z)*Math.pow(10,(dBm/20)); var Vrms2 = (Math.sqrt(2)/2)*(Vpp/2); var dBm2 = dBm; return false; } else if(dBm === null && Vrms === null){ var calc = document.getElementById('ecalc'); var dBm2 = 20 * (log10(Vpp/(Math.sqrt(0.008*Z)))); var Vrms2 = (Math.sqrt(2)/2)*(Vpp/2); var Vpp2 = Vpp; return false; } calc.Vpp2.value = Vpp2; calc.Vrms2.value = Vrms2; calc.dBm2.value = dBm2; return false; } //Log base 10 Function function log10(val) { return Math.log(val) / Math.log(10); } Here is the HTML. It has the form in it. As you can see there are 4 inputs. Z is required and only one of the other values can be entered (hence the if statements in the js. 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 content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Untitled 1</title> <script type="text/javascript" src="Calculate.js"> </script> </head> <body> <form id="ecalc" action="#"> <table align="center"border="1"> <tr> <td colspan="2" align="center"> Enter a Z value and one other value. </td> </tr> <tr> <td align="right">dBm: </td> <td align="left"><input type="text" name="dBm" size="20" /></td> </tr> <tr> <td align="right">Vpp: </td> <td align="left"><input type="text" name="Vpp" size="20" /></td> </tr> <tr> <td align="right">Vrms: </td> <td align="left"><input type="text" name="Vrms" size="20" /></td> </tr> <tr> <td align="right">Z (Ohms): </td> <td align="left"><input type="text" name="Z" size="20" value="50" /></td> </tr> <tr> <td colspan="2" align="center"> <input type="button" value="Calculate" onclick= "calculate();" /> </td> </tr> <tr> <td align="right">dBm:</td> <td alig="left"><input type="text" name="dBm2" size="20" readonly="readonly" /></td> </tr> <tr> <td align="right">Vpp:</td> <td alig="left"> <input type="text" name="Vpp2" size="20" readonly="readonly" /></td> </tr> <tr> <td align="right">Vrms:</td> <td alig="left"> <input type="text" name="Vrms2" size="20" readonly="readonly" /></td> </tr> </table> </form> </body> </html> There is probably something simple that needs to be fixed but I have spent a whole day trying to figure this out. If anyone has any comments please let me know. Thanks in advance! Hello! I have a really simple JavaScript calculator I'm running, and for the life of me can not figure out how to solve this crazy number problem. It's not doing the math properly, and javascript is not my strongest suit. All my script does is take user input of numbers into a form field, subtract that from another form and multiply the answer of those two forms by whatever the user put in. Example: 210 (minus) 120 (multiplied by) .90 = 81 Here is the actual script: Code: // Calculator function CalculateSum(Atext, Btext, Ctext, form) { var A = parseFloat(Atext); var B = parseFloat(Btext); var C = parseFloat(Ctext); form.Answer.value = A - B * C; } /* ClearForm: this function has 1 argument: form. It clears the input and answer fields on the form. It needs to know the names of the INPUT elements in order to do this. */ function ClearForm(form) { form.input_A.value = ""; form.input_B.value = ""; form.input_C.value = ""; form.Answer.value = ""; } // end of JavaScript functions --> And the html I am using: Code: <form name="Calculator" method="post"> <p>Base Average<input type=text name="input_A"></p> <p>Current Average:<input type=text name="input_B"></p> <p>Percentage of:<input type=text name="input_C"></p> <p>Your ball speed is: (miles per hour) <input name="Answer" type=text readonly> </p> <p> <input type="button" value="Calculate Handicap" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form)"> <input type="button" value="Clear" name="ClearButton" onClick="ClearForm(this.form)"> </p> </form> Any help would be greatly appreciated! 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.. Hey, This is my first post! I'm trying to learn javascript and i have tried to create a simple calculator, I have a calculator that does Code: x * x = x but when i try to cut / paste it into another project it just refuses to work! This is undoubtably VERY BASIC but any help is VERY much appreciated Code: <SCRIPT LANGUAGE="JavaScript"> <!-- Begin function fmtPrice(value) { result="$"+Math.floor(value)+"."; var cents=100*(value-Math.floor(value))+0.5; result += Math.floor(cents/10); result += Math.floor(cents%10); return result; } function compute() { var total_price = (document.forms[0].cost.value)*(document.forms[0].qty.value); document.forms[0].total_price.value=total_price; var total_price = fmtPrice(total_price); document.forms[0].total_price.value=total_price; } function resetIt() { document.forms[0].cost.value="0"; document.forms[0].qty.value="0"; document.forms[0].total_price.value=""; } // End --> </SCRIPT> Here is a link to the page - http://dl.dropbox.com/u/29087/Java%20Laptop%20Calc.html ( so you can view source code, saving me from posting it all here ) what am i doing wrong?!! man java is so much harder than VB! I am trying to make a fairly simple program for practicing for a mid-term that is coming up in my intro to programming class and I need some help figuring out how to create the function/setting up the variables. Essentially what the code is, is a program that adds up the values put in 5 text boxes, the values from checkboxes that are selected, and the value of a selected radio button. I do not think I have the variables named correctly, and I have no idea how to create the function. I will post what I have so far so you can get a better idea of what it is I'm trying to make. It's so simple, just adding up everything. I just need help figuring out how to achieve that... Here is the code I have so far: <!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>Computer Price Caculator</title> <script type="text/javascript"> function calc() { with( document.frmCom ) { var P = setPerepherals.selectedIndex; var S = setShipping.selectedIndex; var total = P + S; display.value = "Your systems total price is:" setPerepherals.value[P].text + setShipping.value[S].text + "\n Total:\t$" + total.toFixed(2); }//end with }//end calc() </script> <style type="text/css"> .auto-style1 { margin-left: 17px; } </style> </head> <body background="PracticeMidterm/bg.jpg"> <center> <h1> <img src="PracticeMidterm/Computer.jpg" height="124" width="179">Computer Price Calculator</h1> <hr class="auto-style1" style="width: 784px" /> <form name="frmCom"> <table style="border:thin black solid"> <tr> <td colspan="2" width="700" align="center"> Processor (CPU): <input type="text" align="middle" name="textCPU"></input> <br/> Motherboard: <input type="text" name="textMotherboard" ></input><br/> Case: <input type="text" name="textCase" ></input><br/> Power Supply: <input type="text" name="textPowersupply" ></input><br/> Monitor: <input type="text" name="textMonitor" ></input> </td></tr> <tr> <td align="center"><hr width="500" align="center" /></td> </tr> <tr> <td colspan="1" align="center"> Perepherals: <input type="checkbox" align="middle" name="checkboxKeyboard" value="25.00" onclick="setPerepherals()" >Keyboard ($25.00)</input> <br/><input type="checkbox" align="middle" name="checkboxMouse" value="10.00" onclick="setPerepherals()" >Mouse ($10.00)</input> <br/><input type="checkbox" align="middle" name="checkboxTablet" value="60.00" onclick="setPerepherals()" >Tablet ($60.00)</input> </td> </tr> <tr> <td align="center"><hr width="500" align="center" /></td> </tr> <tr> <td colspan="1" align="center"> Shipping: <input type="radio" align="middle" name="radShipping" value="5.00" onclick="setShipping()" >Ground ($5.00)</input> <br/><input type="radio" align="middle" name="radShipping" value="10.00" onclick="setShipping()" >Second Day Air ($10.00)</input> <br/><input type="radio" align="middle" name="radShipping" value="15.00" onclick="setShipping()" >Overnight Air ($15.00)</input> </td> </tr> <tr> <td align="center"><hr width="500" align="center" /></td> </tr> <tr> <td colspan="1" align="center"><input value="Calculate price" type="button" /></td> </tr> <tr> <td colspan="1" align="center"><textarea name="display" rows="10" cols="40">Total Price:</textarea></td> </tr> </table> *******For more information visit <a href="http://www.howstuffworks.com/pc.htm">How Computers Work.</a>******* </form> </center> </body> </html> Hi all, I am creating a simple 3 field calculator that calculates net, vat and gross. The user has to input 2 of the 3 fields, hit calculate and it should fill the 3rd field. I have so far succeeded with the net field (enter in gross and vat) but the other 2 throw up spurious results! I have worked out these equations (they work on a calculator) Net = G - (( G * V ) / ( V + 100 )) VAT = (( G - N ) x 100 ) / N Gross = N + ( N x ( V / 100 )) Heres the code for the javascript Code: <script type="text/javascript"> function netvatgross_calc() { var net = (Number(document.calc.gross.value))-(((Number(document.calc.gross.value)) * (Number(document.calc.vat.value)))/((Number(document.calc.vat.value))+100)); var net = net.toFixed(2); document.calc.net.value=net; var vat = (((Number(document.calc.gross.value))-(Number(document.calc.net.value)))*100)/(Number(document.calc.net.value)); var vat = vat.toFixed(2); document.calc.vat.value=vat; var gross = (Number(document.calc.net.value))+((Number(document.calc.vat.value))/100)*(Number(document.calc.net.value)); var gross = gross.toFixed(2); document.calc.gross.value=gross; } </script> Here's the work in progress I am also wanting to add some code to throw up an error if less or more than 2 values have been entered. What am I doing wrong? Any help greatly appreciated! Cheers Dan Hey guys. This should be extremely easy for you to answer. This is actually for an intro to computers class I am taking and I am completely stumped. Basically I have to create a really simple Pythagorean Theorem calculator and this is what I have so far: [CODE] <html> <head> <title>A Pythagoras Conversion</title> </head> <script type="text/javascript"> var right_leg, left_leg, hypotenuse; alert ("This calculates the hypotenuse of a right-angled triangle using the Pytahgorean Thereom. Press <OK> to continue!"); right_leg=prompt("Enter the length of the right leg."); left_leg=prompt("Enter the length of the left leg."); sq1 = right_leg*right_leg; sq2 = left_leg*left_leg; hypotenuse = math.sqrt(sq1 + sq2); alert ("The Hypotenuse= "); </script> <body> </body> </html> [CODE] Everything works except for when it comes to it actually calculating. The calculation window never pops up. Please tell me what I am doing wrong in the calculation here....it is driving me nuts. :/ Thanks so much... Hi guys i'm new to the world of programming and have been watching some videos on youtube by thenewboston to get to know Java. In one of his tutorials he shows you how to make a basic calculator but i wanted to improve it by adding the option to choose which operation you want to perform. However not being familiar with code i cannot think of a way to put it so that it is error free.I do also have a seperate class file which coresponds with the second paragraph.The errors appear in the if statements because of the characters * / - +. I am currently using eclipse to write my code as it is very useful for showing you your mistakes. Thanks in advance. Code: Scanner tut7 = new Scanner(System.in); double fnum, snum, answer; System.out.println("Enter First Number Please:"); fnum = tut7.nextDouble(); System.out.println("Enter Second Number Please:"); snum = tut7.nextDouble(); Scanner opinput = new Scanner(System.in); Operation OperationObject = new Operation(); System.out.println("Enter What Operation You Want to Perform: "); String oper = opinput.nextLine(); OperationObject.calc(oper); if (oper = * ){ answer = fnum * snum; System.out.print("The Answer is: "); System.out.println(answer); }else{ if (oper = / ){ answer = fnum / snum; System.out.print("The Answer is: "); System.out.println(answer); }else{ if (oper = + ){ answer = fnum + snum; System.out.print("The Answer is: "); System.out.println(answer); }else{ if (oper = - ){ answer = fnum - snum; System.out.print("The Answer is: "); System.out.println(answer); }else{ System.out.println("Invalid Operation!!!"); } } } I need help trying to figure out how to get my bmi calculator to work. I cant figure out what is wrong with my formula part and why it wont calculate the numbers you type in...pls help! <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> function calculateBMI() { var weight = numb(document.data.weight.value) var height = numb(document.data.height.value) var height2 = height2 * height2 var BMI = weight * 703 / (height2 * height2) document.form.BodyMassIndex.value=custRound(BMI,1); } </script> <title></title> <style type="text/css"> div.c2 {text-align: center} input.c1 {font-size: 8pt} </style> </head> <body> <div class="c2"> <form name="form" id="form"><input type="text" name="weight" size="4"> Weight (in Kilos) <input type="text" name="height" size="4"> Height (in Centimeters)<br> <br> <input type="text" name="BodyMassIndex" id="BodyMassIndex" size="4"> BMI <input type="button" class="c1" value="Calculate" onclick="calculateBMI()" name="button"> <input type="reset" class="c1" value="Clear Form"></form> </div> </body> </html> 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> aaaaaa
This is what I have so far, but now I am stuck. My instructor wants the output value (Number Set C) to include Number Set A and Number Set B. For instance, if Number Set A is "10" and you want to add Number Set B whose value is also "10", my instructor wants Number Set C to say "10 + 10 = 20". Where am I going wrong, and what is the easiest way I can make this work while making as little changes as possible? Most of the code is what my instructor requires us to use. Code: <HTML> <HEAD> <TITLE>COMSC-100-1241 - Assignment #8 - Introducing "if" Logic - Programming Your Own Calculator</TITLE> <SCRIPT LANGUAGE="text/JavaScript"> function Go(code){ var a var b var c var x a = parseFloat(document.getElementById("a").value); b = parseFloat(document.getElementById("b").value); if (code == 0){ x = a + b c = a + " + " + b + " = " + x } if (code == 1){ x = a - b c = a + " - " + b + " = " + x } if (code == 2){ x = a * b c = a + " * " + b + " = " + x } if (code == 3){ x = a / b c = a + " / " + b + " = " + x } document.getElementById("c").value = c; } </SCRIPT> </HEAD> <BODY> <TABLE BORDER="1" WIDTH="500" ALIGN="center"> <TR> <TD> <DIV ALIGN="left"> <FORM NAME="calculator"> <B>Instructions: </B> Type a number in the box for "Number Set #1". Then input a <I>second</I> number in the box for "Number Set #2". Click the "Calculate" button, and the sum of those two numbers will appear in the box for "Number Set #3". <BR> <BR> <B>Input Values: </B> <BR> <B>Number Set A:</B> <INPUT ID="a"> <BR><BR> <B>Number Set B:</B> <INPUT ID="b" > <BR><BR> <INPUT TYPE="Submit" VALUE="Add" OnClick="Go(0);"> <INPUT TYPE="Submit" VALUE="Subtract" OnClick="Go(1);"> <INPUT TYPE="Submit" VALUE="Multiply" OnClick="Go(2);"> <INPUT TYPE="Submit" VALUE="Divide" OnClick="Go(3);"> <BR><BR> <B>Output Value: </B> <BR> <B>Number Set C:</B> <INPUT ID="c" SIZE="50"> </FORM> </DIV> </FORM> </TD> </TR> </TABLE> </BODY> </HTML> 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! I have this java script function to set up a GPA calculator. There's something wrong, but I can't figure out what, any answers? Thanks! var GPA=new Array("F","F","F","F","F"); var GPA=new Array("D","D","D","D","D"); var GPA=new Array("C","C","C","C","C"); var GPA=new Array("C+","C+","C+","C+","C+"); var GPA=new Array("B","B","B","B","B"); var GPA=new Array("B+","B+","B+","B+","B+"); var GPA=new Array("A","A","A","A","A"); var credits=new Array(0,0,0,0,0); var credits=new Array(1,1,1,1,1); var credits=new Array(2,2,2,2,2); var credits=new Array(3,3,3,3,3); var credits=new Array(4,4,4,4,4); var GPA_point=new Array(0,0,0,0,0); var GPA_point=new Array(5,5,5,5,5); function get GPA point(gpa){ if(gpa=="A"||gpa=="a") return 4; else if(gpa=="B+"||gpa=="b+") return 3.5; else if(gpa=="B"||gpa=="b") return 3; else if(gpa=="C+"||gpa=="c+") return 2.5; else if(gpa=="C"||gpa=="c") return 2; else if(gpa=="D"||gpa=="d") return 1; else if(gpa=="F"||gpa=="f") return 0; } function calculate_gpa(){ var totalGPA=0; var total_credits =0; for(var i=0;i<5;i++){ GPA_point[i]=get_GPA_point(GPA[i]); totalGPA = totalGPA + GPA_point[i]*credits[i]; total_credits = total_credits + credits[i]; } if(total_credits==0) document.getElementById("gpa_result").value = 0; else document.getElementById("gpa_result").value = (totalGPA/total_credits).toFixed(1); } I am no JavaScript guru but I am trying to make a form that will calculate a discount if the quantity is greater than 4. I need it to also calculate a total with discounts at the very end. I have the discount working but I need it to work over multiple fields. Any help would be greatly appreciated. I have been pulling my hair out over this one for 3 days. Thanks in advance. Here is the code. Code: <html> <header> <SCRIPT type = "text/JavaScript"> function calcPrice(){ var discount, price, total, savings; var v = parseInt(document.getElementById("value").value); var q = parseInt(document.getElementById("quantity").value); if (q <= 3) discount = 0; else if (q>=4) {discount = .25} price = q * v; savings = q * v * discount; total = q * v - savings; alert(total); } </SCRIPT> Number of Bedrooms <input type="text" id="quantity" /> <input type="hidden" id="value" value="10" /> <input type="button" onclick="calcPrice()"> </header> <body> </body> </html> 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've seen several Java or Perl Derivative Calculators online, but I can't seem to find any in JavaScript. Has anyone tried? I might need one in JavaScript (or Flash ActionScript) for a graphing tool I'm working on, and I don't want to reinvent the wheel if I can avoid it.
|