Need help debugging a javascript?

I need help with my javascript homework?

  • Hi, i am having trouble with my javascript homework. In box 1, the user enters an amount which i have named amount, and in the next box the user enters another amount which is the sales tax percentage. And the total amount due is found by multiplying the amount by the sales tax, and adding that back to the amount to give a total. We need to use parseFloat to convert the textbox values from string data types to number data types. So so far i've tried everything I can but nothing is working. When i enter an amount and then an amount for the tax it just multiplies those 2 numbers, together. If someone can help me out with this, it would be great. Thanks! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-… <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> function updateOrder() { var amount = parseFloat(document.getElementById("amou… var taxRate = parseFloat(document.getElementById("str"… // examin box1 using the isNaN function to determine if it's a number or not. if (isNaN(document.getElementById("amount")… alert("Field 1 is not a number"); if (isNaN(document.getElementById("str").va… alert("Field 2 is not a number"); var total = amount * taxRate; // found by multiplying the amount by the sales tax, and adding that back to the amount to give a total document.getElementById("total").value = "$" + total.toFixed(2); } </script> </head> <body> <form id="form" action="" /> <h2>Sales Tax Calculator</h2> Amount: <input type="text" id="amount" value="" onchange="updateOrder();" /> + Sales tax percentage: <input type="text" id="str" value="" onchange="updateOrder();" /> + Total: <input type="text" id="total" value="" disabled="disabled" /> </form> </body> </html>

  • Answer:

    Your code is mostly right, but what you've coded it to do is to multiply the amount by the tax rate, however, you are truly multiplying by the tax rate and not the percentage. Easiest solution here is to divide the tax rate by 100, then add 1. Let me explain: If the tax is 8.5%, and you're putting in 8.5 in the text field, have your code calculate (1+(8.5/100) so it will return 1.085. That way when you multiply by the amount, it will give you the proper total. Or you could just multiply by the percentage, but I like using the actual arithmetic better. Hope that helps!

Omid at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.