Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## for loop

Hi,

need some help with my javascript im trying to create a script that does the following:
In 2009 there were 150,000 islanders.
The birth rate is estimated at 0.7% per year.
The death rate is estimated at 0.6% per year.
1900 people immigrate to the island each year.
1800 people emigrate from the island each year.

Write a program to calculate the estimated population of the Island at the beginning of a particular year. The year in question should be requested from the user.

This is what i have written so far and am not sure why it isnt't working:

Code:
```<html>

<body>

<script>

var year = parseInt(prompt( "Enter a year to see population(from 2009 - onwards): ",2009));

var standardYr = 150000;

var calculation = year - 2009;
for( var x = 1; x <= calculation; x++)
var newStandardYr = newStandardYr + 100 + (standardYr * 0.1)
document.write(calculation);

var calc = newStandardYr + standardYr;

var convert = calc.toString();

if(year == 2009)
document.write(standardYr);

else
document.write(calc);

</script>

</body>

</html>```
Any help would be appreciated

thanks

• Your calculations are off by a factor of 100. Percentage difference is 0.001, not 0.1
as 0.10 is a 10% difference.

Also, unless this is a homework assignment, you should avoid using prompt() or document.write() in your scripts.

You should also check for invalid values, like years less than 2009.

And you should avoid double posting on the forum.

Other than that, keep trying and understand the recommended changes here.

Code:
```<html>
<body>
Enter a year to see population(from 2009 - onwards): <input type="text" id='newYear' value='2009'><br>
<p> <button onclick="calculatePopulation()">Calculate expected population</button>

<script>
function calculatePopulation() {
var year = document.getElementById('newYear').value;
var calculation = year - 2009;
if (calculation < 0) { alert('Invalid year entry'); return; }

var standardYr = 150000;
var newStandardYr = 0;
for ( var x = 1; x <= calculation; x++) { newStandardYr += newStandardYr + 100 + (standardYr * 0.001) }

var calc = standardYr + newStandardYr;

document.getElementById('answer').innerHTML = 'Expected population in: '+year+' is: '+calc.toString();
}
</script>

</body>
</html>```

• ## Users who have thanked jmrker for this post:

timewarp (04-14-2013)

• thank you I understand the majority of it just confused about a few things:
can the html form be inside the script tag or not?
why do you have to get the answer id is it just to display the form i thought it already did that? and ive never used .innerHTML wat does that do??

also in my original answer why does mine not print the an answer even tho all the calculations are there?

thanks

• inerHTML is contents of tag
`var year = document.getElementById('newYear').value;`
changes your input method. will take data from input on page instead of popup.

`document.getElementById('answer').innerHTML = 'Expected population in: '+year+' is: '+calc.toString();`
that's how you do it in real world applications

and your original code is missing all `{`'s and `}`'s

-------
for the future you can use dev tools in chrome. you'll see errors in your code there

• ## Users who have thanked patryk for this post:

timewarp (04-14-2013)

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•