Hi,
For homework I have to make a retirement calculator using javascript that prompts the user to enter the age and yearsworked so far. It then outputs whether the person can retire and if not the numbers of years left able to retire.
The person can retire if:
1. Age is greater than or equal to 65 or
2. Years worked is greater than or equal to 40 or
3. Age is greater than or equal to 60 and years worked is greater than or equal to 30
I can’t seem to get the right output. And for some reason after it alerts the popup for the result the window doesn’t close when I click ok. Heres my code so far. Thanks
var Age = prompt(“Please enter the age of the worker:”);
var YearsWorked = prompt(“Please enter the number of years worked so far:”);
function CanRetire()
{
if (Age >= 65 || YearsWorked >=40)
{
“Can Retire”
}
if (Age >= 60 && YearsWorked >=30)
{
“Can Retire”
}
else
{
alert(YearsLeft());
}
return CanRetire()
}
function YearsLeft()
{
var Condition1 = 65 – Age;
var Condition2 = 40 – YearsWorked;
var a = 60 – Age;
var b = 30 – YearsWorked;
if (a < b)
{
var Condition3 = a;
}
else
{
var Condition3 = b;
}
if(Condition1 < Condition2 && Condition1 < Condition3)
{
alert(“The number of years left before retiring is: ” + Condition1);
}
if(Condition2 < Condition1 && Condition2 < Condition3)
{
alert(“The number of years left before retiring is: ” + Condition2);
}
else
{
alert(“The number of years left before retiring is: ” + Condition3);
}
return YearsLeft()
}
CanRetire()