I do not have anyone else to ask, I'm still on holidays and am once again turning here for help so I would appreciate any help and less criticism.
1.
I was trying to use else if to display if the application calculates the over or under budget but it's only displaying you are under budget even when I've put in a greater amount in the expenditure??
2.HTML Code:if (intotal > extotal) { document.getElementById("budget").innerHTML = "You are under Budget"; } else { document.getElementById("budget").innerHTML = "YOU ARE OVER BUDGET!"; }
It's not calculating properly. I think it has something to do with the regexp.
...and here is the whole code.HTML Code:function toNum(str) { var p = /^[0-9]*\.?[0-9]+\s*$/; // a better number check can be done using regexp return p.test(str) ? parseFloat(str) : 0; //return (isNaN(str) || str == "") ? 0 : parseFloat(str); }
HTML Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <style type="text/css"> body{ font-family:arial; font-size:10pt; background-color:#024393; } table td{ background-color:#ffffff; padding:10px; } </style> <script type="text/javascript"> function calc() { var salary, social, gifts, other, food, electric, phone; var otherincome, water, loans, other1, other2, intotal, extotal, total; var salary = toNum(document.form1.salary.value); var social = toNum(document.form1.social.value); var gifts = toNum(document.form1.gifts.value); var otherincome = (document.form1.otherincome.value); var food = toNum(document.form1.food.value); var electric = toNum(document.form1.electric.value); var phone = toNum(document.form1.phone.value); var rent = toNum(document.form1.rent.value); var water = toNum(document.form1.water.value); var loans = toNum(document.form1.loans.value); var other1 = toNum(document.form1.phone.value); var other2 = toNum(document.form1.other2.value); var intotal = salary + social + gifts + otherincome; var extotal = food + electric + phone + rent + water + loans + other1 + other2; var total = intotal - extotal; document.form1.total.value = total.toFixed(2); if (intotal > extotal) { document.getElementById("budget").innerHTML = "You are under Budget"; } else { document.getElementById("budget").innerHTML = "YOU ARE OVER BUDGET!"; } function toNum(str) { var p = /^[0-9]*\.?[0-9]+\s*$/; // a better number check can be done using regexp return p.test(str) ? parseFloat(str) : 0; //return (isNaN(str) || str == "") ? 0 : parseFloat(str); } } </script> </HEAD> <BODY> <center> <form name="form1"> <table border="1"> <tr> <td colspan="2"><b>Income</b></td ><td colspan="2"><b>Expenditure</b></td> </tr> <tr> <td>Salary:</td><td><input name="salary" size="10" value="0"></td> <td>Food:</td><td><input name="food" size="10" value="0"></td> </tr> <tr> <td>Social Security: </td><td><input name="social" size="10" value="0"></td> <td>Electricity: </td><td><input name="electric" size="10" value="0"></td> </tr> <tr> <td>Gifts: </td><td><input name="gifts" size="10" value="0"></td> <td>Phone: </td><td><input name="phone" size="10" value="0"></td> </tr> <tr> <td>Other: </td><td><input name="otherincome" size="10" value="0"></td> <td>Rent: </td><td><input name="rent" size="10" value="0"></td> </tr> <tr> <td colspan="2" rowspan="4" valign="top" bgcolor="white"> <b>Calculate your house budget</b><p> <center><span id="budget"></span></center> </td><td>Water:</td><td><input name="water" size="10" value="0"></td> </tr> <tr> <td>Loans: </td><td><input name="loans" size="10" value="0"></td> </tr> <tr> <td>Other: </td><td><input name="other1" size="10" value="0"></td> </tr> <tr> <td>Other: </td><td><input name="other2" value="0" size="10"></td> </tr> <tr> <td colspan="2"><input type="button" value="Calculate" onClick="calc()"> <input type="reset" value="Clear"> </td> <td colspan="2"><input name="total" value="$0.00" size=""> </td> </tr> </table> </form> </center> </BODY> </HTML>


LinkBack URL
About LinkBacks
Reply With Quote




