/    Sign up×
Community /Pin to ProfileBookmark

Javascript Retirement Calculator Help

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()

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@hanleychanauthorMar 10.2004 — Changed code and the output seems to work fine now except I can't make the alert window go away. Please help

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)

{

alert("Can Retire");

}

if (Age >= 60 && YearsWorked >=30)

{

alert("Can Retire");

}

else

{

alert(YearsLeft());

}

return CanRetire()

}


function YearsLeft()

{

var Condition1 = 65 - Age;

var Condition2 = 40 - YearsWorked;

var Condition3

var a = 60 - Age;

var b = 30 - YearsWorked;

if (a < b)

{

Condition3 = a;

}

else

{

Condition3 = b;

}

alert(Math.min(Condition1,Condition2,Condition3));

return YearsLeft()

}

CanRetire()
Copy linkTweet thisAlerts:
@oleragMar 10.2004 — See if this helps you. Right now your continuously calling the

function over so your in a runaway condition.
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Retirement Sample</title>
<script type="text/javascript">
var Age,YearsWorked;

function CanRetire() {
Age = prompt("Please enter the age of the worker:");
YearsWorked = prompt("Please enter the number of years worked so far:");

if (Age >= 65 || YearsWorked >=40 || (Age >= 60 && YearsWorked >=30))
alert("Can Retire");
else
alert(YearsLeft() + " years.");
}

function YearsLeft() {
var a;
var b;

if (Age >= 60)
a = 65-Age;
else
a = 60-Age;

if (YearsWorked >= 30)
b = 40-YearsWorked;
else
b = 30-YearsWorked;

if (a < b)
return(a);
else
return(b);

}
</script>
<div style="text-align: center;">
<strong>Retirement Sample</strong>
</div>

<form action="#">
<p style="text-align: center">
<input type="button" value="Start" onClick="CanRetire()" />
</p>
</form>
[/code]
Copy linkTweet thisAlerts:
@hanleychanauthorMar 10.2004 — Thanks for the help, but I just figured out the answer. Thanks anways though. I did it this way


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) || (Age >= 60 && YearsWorked >= 30))

{

alert("Can Retire");

}

else

{

YearsLeft();

}

}

function YearsLeft()

{

var Condition1 = 65 - Age;

var Condition2 = 40 - YearsWorked;

var Condition3 = Math.max(60 - Age, 30 - YearsWorked);

alert(Math.min(Condition1,Condition2,Math.max(Condition3)));

}

CanRetire()
×

Success!

Help @hanleychan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...