I need to make an online insurance calculator using basic HTML and Javascript.
I need a form that allows a visitor to enter their age. Then, the visitor is to select (via radio buttons) an amount for their coverage rate (either 20,000, 30,000, 40,000, 50,000 or 60,000). Calculate the monthly premium according to the following info:
Age: 2 -18: $24 per $10,000 in coverage
Age: 19 - 40: $32 per $10,000 in coverage
Age: 41 – 65: $42 per $10,000 in coverage
Age: 66 - 70: $59 per $10,000 in coverage
For example, a 40 year old desiring $20,000 in coverage would pay $64 per month.
Display the resulting total on the page using innerHTML.
Code:
<html>
<body>
<div>
<form>
<!-- Input
age field-->
Enter your
age:<br><br><input id="age"
type="text"><br><br>
<!-- Coverage rate options-->
Select Coverage rate: <br><br>
<input type="radio" id="coverage" name="coverage" value=20000>20000</option><br>
<input type="radio" id="coverage" name="coverage" value=30000>30000</option><br>
<input type="radio" id="coverage" name="coverage" value=40000>40000</option><br>
<input type="radio" id="coverage" name="coverage" value=50000>50000</option><br>
</select> <br><br>
<!-- Input
button onclicking this goto coverageFunction-->
<input type="Button" value="Calculate Coverage Rate" onclick="calculateCoverage()">
</form>
</div>
<div
id="output"></div>
</body>
<script>
function
calculateCoverage() {
var age=parseInt(document.getElementById("age").value); //take input age from user
var amounts = document.getElementsByName('coverage'); //take user selected coverage rate
var amount;
for(var i = 0; i < amounts.length; i++){
if(amounts[i].checked)
amount =
parseInt(amounts[i].value);
}
var premium_amount;
if (age>=2 && age<=18) //based on age calculate premimum amount monthly
premium_amount=(amount/10000)*24;
else if (age>=19 && age<=40)
premium_amount=(amount/10000)*32;
else if (age>=41 && age<=65)
premium_amount=(amount/10000)*42;
else if (age>=66 && age<=70)
premium_amount=(amount/10000)*59;
document.getElementById("output").innerHTML="<br>Monthly Premimum amount is $" + premium_amount; //finally write the output
}
</script>
</html>
Code and Output Screenshots:
Note: if you have any queries please post a comment thanks a lot..always available to help you...
Get Answers For Free
Most questions answered within 1 hours.