First Exercise
- Users are required to enter their income into a text box txtIncome.
- Users are required to enter their age into a text box txtAge. Users click an Estimate Credit button (cmdEstimateCredit) that will calculate and display (in pounds and pence) the credit limit of the applicant in a text box txtCreditLimit.
- Under 18 years old ZERO credit
- 18 years to 25 years inclusive 1 x income
- 26 years or older 3 x income
Like this:
Solution
<SCRIPT type="text/javaScript">
function button1Listener()
{
var credit;
var income= parseFloat(document.getElementById("txtIncome").value);
var age= parseFloat(document.getElementById("txtAge").value);
if (isNaN(income)) alert("Income must be an numeric. Please Re-enter");
else if (isNaN(age)) alert("Age must be an integer. Please Re-enter");
else
{ if (age<18) credit=0;
else if (age < 26) credit=income;
else credit=3.0*income;
document.getElementById("txtCreditLimit").value = '\u00A3'+ credit;
}
}
function doit()
{
var btn1=document.getElementById("cmdEstimateCredit");
btn1.addEventListener("click",
button1Listener,
false
);
}
</SCRIPT>
Second Exercise
Solution
<SCRIPT type="text/javaScript">
function maxButtonListener()
{
var max=0;
for (var i=0;i<5;i++)
{
var n=prompt("Enter a number")
if (n>max) max=n;
}
alert ("the biggest was " +n);
}
function doit()
{
var btn1=document.getElementById("cmdEstimateCredit");
btn1.addEventListener("click",
button1Listener,
false
);
// you must add the maxButtonListner to the maxButton here!
}
</SCRIPT>