Hello all,
I'm new here, and I'm trying to learn JavaScript. I'm working on an exercise where the user is supposed to enter several market values or enter 0 to quit. It uses a while loop, but it seems to be an infinite loop, and I can't see where the problem is. It crashes my browser. I really hope someone can help me. The code is posted below:
Code:
<html>
<head>
<title>The Evil Tax Collector</title>
<script language="JavaScript">
var marketValues = new Array();
var taxes = new Array();
function computeTax(market)
{
var taxDetails = "";
var assessedValue = market * 0.28;
taxes[taxes.length] = assessedValue * 0.125;
taxDetails = "The tax on the property with a market value of $" + market + " is $" + taxes[taxes.length];
return taxDetails;
}
function printSummary()
{
var i;
var totalTax;
for(i=0; i<taxes.length; i++)
{
totalTax += taxes[i];
}
document.taxForm.results.value = "The total taxes add up to $" + totalTax;
}
function processProperties()
{
var marketTextbox = document.taxForm.txtMarket;
var resultsTextbox = document.taxForm.results;
while (marketTextbox.value != 0)
{
if(isNaN(marketTextbox.value) == true)
{
alert("Numbers only, please");
}
if (marketTextbox.value < 0)
{
alert("Positive numbers only, please");
}
marketValues[marketValues.length] = parseFloat(marketTextbox.value);
resultsTextbox.value = computeTax(marketTextbox.value);
}
printSummary();
}
</script>
</head>
<body>
<h1>The Evil Tax Collector</h1>
<h2>Here to collect your soul!</h2>
Please enter a market value and click Submit. To stop, enter 0 and then click Submit.<br><br>
<form name="taxForm">
Market Value: <input type="text" name="txtMarket"><br><br>
<input type="button" value="Submit" onclick="processProperties()">
<input type="reset" value="Nevermind"><br><br>
<input type="text" name="results">
</form>
</body>
</html>