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

Thread: for loop

  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,086
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    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>
    <div id="answer"></div>
    
    <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)

  • #3
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?
    and this one bit: document.getElementById('answer').innerHTML
    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

  • #4
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    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();
    this will take div with "answer" id and put answer in it instead of using document.write() to return answer.
    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
    Last edited by patryk; 04-14-2013 at 11:54 AM.

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall

  • 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
    •