JavaScript - Simple Calculator Program, Need Help! =(
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> Similar TutorialsBasically i have an image of a ball that i am trying to make bounce from top to bottom so far it is just going to the bottom and stopping plz help thanks <head> <title></title> </head> <body> <center> <input type="button" value="Click" onClick="moveDown()"/> </center> <img id="Ball" src="ball.jpg" style="position:absolute; top:100px; left:100px;"/> <script language="JavaScript"> var Image = document.getElementById("Ball"); var Position = 100; var direction = 1; function moveDown() { if(direction > 0) { Image.style.top = parseInt(Image.style.top) + 20+'px'; } if(parseInt(Image.style.top)> 100 || direction > 1 ) direction = 1 if(parseInt(Image.style.top)> 500 || direction < 1 ) { direction = -1 Image.style.top = parseInt(Image.style.top) - 20+'px'; } } setInterval ("moveDown()", 50 ); </script> </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.. I am very new to coding. The below is an assignment for a class. It is a "game" which is supposed to do the following: When your program starts up it must display the image that will move around, a Start button, a Stop button, and a way for the user to specify an interval before a new game begins. The default interval must be 1000 msec. When the user presses Start, a new game starts and the selected image is displayed at a random place. The image must move to a new random place once per interval. If the user presses the Stop button, the image must stop moving, and a subsequent Start must start everything over from the beginning. Each time the user successfully hits the image, the program must move the image to a new random place immediately (i.e., not wait for the current interval to end). When the user has hit the image 3 times, the program must display a confirm dialog box that reports the elapsed time and that also says something like "Another game?" If the user pushes OK, that starts a new game immediately (no need to push Start again). If Cancel is pushed, the program stops. I have been working on this for hours, but it does not work properly. Obvious issues: the game starts running when the page loads, instead of when the start button is pushed. The confirm at the end does not cause a new game to start. The time/ elapsed time calculations are not functioning properly/ the elapsed time always displays as 1 sec. Code: <html> <title> Dori's WhaM Game </title> <head> Stephen Salutes You </head> <body> <br> <img id="Colbert" src="StephenColbert.jpeg" style="position:absolute; left:10px; top:85px" onClick='hit()'> <form> <input type=button value="Start game." onClick='newgame()'> <input type=button value="Stop it, stop it now." onClick='clearInterval(setint_val); return true;'> <br> Desired speed is <input type=text id=interval value="1000" size=5> msec </form> <script> var hitcount= 0 var setint_val = (setInterval('moveit()', document.getElementById("interval").value)); var starttime var endtime function newgame() { clearInterval(setint_val) hitcount= 0 setInterval() starttime=new Date().getTime() } function moveit() { document.getElementById("Colbert").style.left = 800 * Math.random() + "px"; document.getElementById("Colbert").style.top = 500 * Math.random() + "px"; } function hit() { moveit() clearInterval(setint_val) setInterval() hitcount= hitcount + 1 if (hitcount >= 3) { clearInterval (setint_val); // clearInterval() to stop the image from moving endtime=new Date().getTime(); alert(starttime) alert (endtime) // compute the elapsed time (make another call to new Date()) if (confirm("That took " + endtime-starttime+" seconds. Would you like to try again?")) { newgame ()} else {clearInterval (setint_val) hitcount=0} }} </script> </body> </html> I have an homework for a web development class. I wrote a program that would be displayed on a webpage that returns the users zodiac sign. It was working just fine until i made small mistake somewhere but i just can't find it. If anyone could help I would appreciate it, I imagine it would be a very easy job for anyone familiar with javascript. [CODE] function signs() { var date=document.zodiac.date.value; var month=document.zodiac.month.selectedIndex; var fortune= new Array() fortune[0]="There is a true and sincere friendship between you and your friends. " fortune[1]="You find beauty in ordinary things, do not lose this ability. " fortune[2]="Ideas are like children; there are none so wonderful as your own. " fortune[3]="It takes more than good memory to have good memories. " fortune[4]="A thrilling time is in your immediate future. " fortune[5]="Your blessing is no more than being safe and sound for the whole lifetime. " if (document.zodiac.month.value == 1 && document.zodiac.date.value >=20 || document.zodiac.month.value == 2 && document.zodiac.date.value <=18) {document.zodiac.sign.value = "Aquarius";} if (document.zodiac.month.value == 1 && document.zodiac.date.value > 31) {document.zodiac.sign.value = "Invalid Date";} if (document.zodiac.month.value == 2 && document.zodiac.date.value >=19 || document.zodiac.month.value == 3 && document.zodiac.date.value <=20) {document.zodiac.sign.value = "Pisces";} if (document.zodiac.month.value == 2 && document.zodiac.date.value > 29) {document.zodiac.sign.value = "Invalid Date?";} if (document.zodiac.month.value == 3 && document.zodiac.date.value >=21 || document.zodiac.month.value == 4 && document.zodiac.date.value <=19) {document.zodiac.sign.value = "Aries";} if (document.zodiac.month.value == 3 && document.zodiac.date.value > 31) {document.zodiac.sign.value = "Invalid Date";} if (document.zodiac.month.value == 4 && document.zodiac.date.value >=20 || document.zodiac.month.value == 5 && document.zodiac.date.value <=20) {document.zodiac.sign.value = "Taurus";} if (document.zodiac.month.value == 4 && document.zodiac.date.value > 30) {document.zodiac.sign.value = "Invalid Date";} if (document.zodiac.month.value == 5 && document.zodiac.date.value >=21 || document.zodiac.month.value == 6 && document.zodiac.date.value <=21) {document.zodiac.sign.value = "Gemini";} if (document.zodiac.month.value == 5 && document.zodiac.date.value > 31) {document.zodiac.sign.value = "Invalid Date";} if (document.zodiac.month.value == 6 && document.zodiac.date.value >=22 || document.zodiac.month.value == 7 && document.zodiac.date.value <=22) {document.zodiac.sign.value = "Cancer";} if (document.zodiac.month.value == 6 && document.zodiac.date.value > 30) {document.zodiac.sign.value = "Invalid Date";} if (document.zodiac.month.value == 7 && document.zodiac.date.value >=23 || document.zodiac.month.value == 8 && document.zodiac.date.value <=22) {document.zodiac.sign.value = "Leo";} if (document.zodiac.month.value == 7 && document.zodiac.date.value > 31) {document.zodiac.sign.value = "Invalid Date";} if (document.zodiac.month.value == 8 && document.zodiac.date.value >=23 || document.zodiac.month.value == 9 && document.zodiac.date.value <=22) {document.zodiac.sign.value = "Virgo";} if (document.zodiac.month.value == 8 && document.zodiac.date.value > 31) {document.zodiac.sign.value = "Invalid Date";} if (document.zodiac.month.value == 9 && document.zodiac.date.value >=23 || document.zodiac.month.value == 10 && document.zodiac.date.value <=22) {document.zodiac..sign.value = "Libra";} if (document.zodiac.month.value == 9 && document.zodiac.date.value > 30) {document.zodiac.sign.value = "Invalid Date";} if (document.zodiac.month.value == 10 && document.zodiac.date.value >=23 || document.zodiac.month.value == 11 && document.zodiac.date.value <=21) {document.zodiac.sign.value = "Scorpio";} if (document.zodiac.month.value == 10 && document.zodiac.date.value > 31) {document.zodiac.sign.value = "Invalid Date";} if (document.zodiac.month.value == 11 && document.zodiac.date.value >=22 || document.zodiac.month.value == 12 && document.zodiac.date.value <=21) {document.zodiac.sign.value = "Sagittarius";} if (document.zodiac.month.value == 11 && document.zodiac.date.value > 30) {document.zodiac.sign.value = "Invalid Date";} if (document.zodiac.month.value == 12 && document.zodiac.date.value >=22 || document.zodiac.month.value == 1 && document.zodiac.date.value <=19) {document.zodiac.sign.value = "Capricorn";} if (document.zodiac.month.value == 12 && document.zodiac.date.value > 31) {document.zodiac.sign.value = "Invalid Date";} document.getElementById("image").src=document.zodiac.sign.value+".jpg" var randomFortune= Math.floor(Math.random() * 5) document.zodiac.fortunebox.value=fortune[randomFortune] document.getElementById('body').style.backgroundColor='#99FFCC' document.getElementById('body').style.backgroundColor='#99FFCC' document.tags.H1.color = '#99FFCC' } [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> <div style="background-color:#000000; height:60px;" > <h1 align="center"> What Zodiac Sign Are You? </h1> </div> <title>Horoscope Fortune Teller</title> <LINK REL=StyleSheet HREF="horoscope.css" TYPE="text/css" MEDIA=screen> <script type="text/javascript" src="Horoscope.js"> </script> </head> <body id=body> <br> <br> <table border=10px; align="center", height="250px"> <form name="zodiac"> <tr><td>Please select your day and month of birth to get started:</td></tr> <br> <br> <tr> <td>Choose month from the list:</td> <td> <select name="month"> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> </td> </tr> <tr> <td>Enter day of birth:</td> <td><input type="text" name="date" value="Day" onClick=value=""></td> </tr> <tr> <td>Your Zodiac Sign is:</td><td><input type="text" name="sign" value="Zodiac Sign"></td> </tr> <tr> <td><input type="button" value="Click to find your sign" onClick="signs()"></td> <tr><td><img src="" id="image" alt="" /></td><td align="center"><input type="text" name="fortunebox" value="" size="80"></td></tr> </tr> </table> </form> <br> <br> <br> <br> <div style="background-color:#000000; height:60px;"></div> </body> </html> Hey guys, Here's the program. Quote: import java.util.Random; import java.util.Scanner; public class HighLowGame { public static void main(String[] args) { Random rand = new Random(); int numberOfTries = 0; Scanner input = new Scanner(System.in); char repeat; char ans; int guess; boolean win = false; do { System.out.println("Pick a number between 0 and 10: "); guess = input.nextInt(); numberOfTries++; int numberToGuess = rand.nextInt(10); if (guess == numberToGuess) { win = true; System.out.println("Congratulations you have won!"); } else if (guess > numberToGuess) { System.out.println("Your guess was high try again"); } else if (guess < numberToGuess) { System.out.println("Your guess was low try again"); } System.out.println("The number was " + numberToGuess); System.out.println("You guessed the number in "+ numberOfTries + " attempt(s)"); System.out.println("New game? 1 for Yes, or any other number to quit "); ans = input.next().charAt(0); } while (ans == 1); } } Two questions, why doesn't my program let me keep putting in numbers until I guess the right one? What is wrong with my loop? Pressing 1 should repeat the program, any other number should close. Thanks! 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. 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'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? i am new in programming and learning it from lynda but i am trying to make a program which according to me is good enough but its not working and i am using free javascript editor for compiling and coding <!DOCTYPE html> <html> <body> <script> var a = 5; var b = 10; var c = 30; var d = a + b + c; alert("the value of d is" + d); </body> </html> Please tell me where is it i am making mistake Reply With Quote 01-08-2015, 11:56 PM #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 forgot the </script> ending tag. 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!!! 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 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! 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!!!"); } } } 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... 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 an assignment to create a program in JS but I have not read JS much more. So, for this assignment, can anyone help me to solve this assignment? Check attachment. Uploaded with ImageShack.us I need a count down clock that will count down 18 minutes and reset itself at the end. also i need a counter that increases by +1 every 18 minutes starting at 0. thankyou This is my code from prelim to finals. The missing part here is when the input is less than or equal to 65 and greater than 100, the prompt will say INVALID ENTRY. However, after it says Invalid Entry, it must ask you to enter again an Entry until you input a valid entry. It must be terminated after you input from prelim to finals either 0,20,30,40 or after you input a valid entry and after the program calculated it. My program does not terminate after I input valid entries. It just continue to ask grade again and again even it already calculated the grades. I don't know how will I put those code inside the program. I'm getting really confused. Code: <html> <head> <title>Web Programming Laboratory Exam</title> </head> <body> <script language="JavaScript"> do { { var pg=prompt('Enter Prelim Grade','0'); if(pg==0) { alert('No Grade'); document.write('<br>Prelim Grade is '+pg); } else if(pg==20) { alert('Not Attending'); document.write('<br>Prelim Grade is '+pg); } else if(pg==30) { alert('Dropped'); document.write('<br>Prelim Grade is '+pg); } else if(pg==40) { alert('Incomplete'); document.write('<br>Prelim Grade is '+pg); } else document.write('Prelim Grade is '+pg); var mg=prompt('Enter Midterm Grade','0'); } { if(mg==0) { alert('No Grade'); document.write('<br>Midterm Grade is '+mg); } else if(mg==20) { alert('Not Attending'); document.write('<br>Midterm Grade is '+mg); } else if(mg==30) { alert('Dropped'); document.write('<br>Midterm Grade is '+mg); } else if(mg==40) { alert('Incomplete'); document.write('<br>Midterm Grade is '+mg); } else document.write('</br>Midterm Grade is '+mg); var fg=prompt('Enter Final Grade','0'); } { if(fg==0) { alert('No Grade'); document.write('<br>Final Grade is '+fg); } else if(fg==20) { alert('Not Attending'); document.write('<br>Final Grade is '+fg); } else if(fg==30) { alert('Dropped'); document.write('<br>Final Grade is '+fg); } else if(fg==40) { alert('Incomplete'); document.write('<br>Final Grade is '+fg); } else { document.write('</br>Final Grade is '+fg); sg=((pg*.3)+(mg*.3)+(fg*.4)); document.write('<br>Your Semestral Grade is '+sg); } } if (sg>=74.5) { document.write('<br>Remarks: Passed'); document.write('</br>This Program is developed by -k3nN'); } else if (sg<74.5) { document.write('<br>Remarks:Failed'); document.write('</br>This Program is developed by -k3nN'); } } while (sg>=74.5|sg<74.5) { document.write('</br>Thanks') } </script> </body> </html> |