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. ## Population Clock

Hello all!

For the past few days I've been having trouble trying to figure out how to do something. I want to code a world population clock similar to the one here, but simpler: I want the user to be able to choose future years up to 2020 (just years, not months/days), and have the population grow by 1 percent each year.

So far I have a code for the world population, but I have no clue where to begin in terms of making a list of the years with the option to increase the population.

Could anyone point me in the right direction? I'd appreciate any help!

Heres the code I have:

<body>

<script type="text/javascript">

function maind(){
startdate = new Date()
now(startdate.getYear(),startdate.getMonth(),startdate.getDate(),startdate.getHours(),startdate.getM inutes(),startdate.getSeconds())
}

function ChangeValue(number,pv){
numberstring =""
var j=0
var i=0
while (number > 1)
{

numberstring = (Math.round(number-0.5) % 10) + numberstring
number= number / 10
j++
if (number > 1 && j==3) {
numberstring = "," + numberstring
j=0}
i++
}

numberstring=numberstring

if (pv==1) {document.getElementById("worldpop").innerHTML=numberstring }
}

function now(year,month,date,hours,minutes,seconds){
startdatum = new Date(year,month,date,hours,minutes,seconds)

var now = 5600000000.0
var now2 = 5690000000.0
var groeipercentage = (now2 - now) / now *100
var groeiperseconde = (now * (groeipercentage/100))/365.0/24.0/60.0/60.0
nu = new Date ()
schuldstartdatum = new Date (96,1,1)
secondenoppagina = (nu.getTime() - startdatum.getTime())/1000
totaleschuld= (nu.getTime() - schuldstartdatum.getTime())/1000*groeiperseconde + now
ChangeValue(totaleschuld,1);

timerID = setTimeout("now(startdatum.getYear(),startdatum.getMonth(),startdatum.getDate(),startdatum.getHours( ),startdatum.getMinutes(),startdatum.getSeconds())",200)
}

</script>

Current world population (estimated): <span id="worldpop" style="font-weight: bold"></span>.

</body>

• If all you want is to calcualte the estimated world population x years ahead, then:-

Code:
```Population how many years ahead? <input type = "text"id = "years" size = "2" maxlength = "2" onblur = "getPopulation()">
<br><span id = "wpop"></span>

<script type = "text/javascript">
function getPopulation() {
var popNow = 5600000000;
var now = new Date();
var nowYear = now.getFullYear();
var numYears = parseInt(document.getElementById("years").value);
if (isNaN(numYears) ||(numYears == "")) {
alert ("You must enter a number!");
return false;
}
for (var i = 0; i < numYears; i++) {
popNow = popNow * 1.01; // 1% growth per annum
}
var futureYear = nowYear + numYears;
document.getElementById("wpop").innerHTML = "The world population in " + futureYear + " is expected to be " + popNow;
}
</script>```
Bear in mind that the forum rules do not allow us to do your homework for you, so if you want a selct/option list then you must provide that yourself. I will also leave it to you to display the result as a whiole number.

BTW, when posting here please follow the posting guidelines and wrap your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

In theory, there isn't any difference between the theory and practice. In practice, there is.

• Originally Posted by Philip M
If all you want is to calcualte the estimated world population x years ahead, then:-

Code:
```Population how many years ahead? <input type = "text"id = "years" size = "2" maxlength = "2" onblur = "getPopulation()">
<br><span id = "wpop"></span>

<script type = "text/javascript">
function getPopulation() {
var popNow = 5600000000;
var now = new Date();
var nowYear = now.getFullYear();
var numYears = parseInt(document.getElementById("years").value);
if (isNaN(numYears) ||(numYears == "")) {
alert ("You must enter a number!");
return false;
}
for (var i = 0; i < numYears; i++) {
popNow = popNow * 1.01; // 1% growth per annum
}
var futureYear = nowYear + numYears;
document.getElementById("wpop").innerHTML = "The world population in " + futureYear + " is expected to be " + popNow;
}
</script>```
Bear in mind that the forum rules do not allow us to do your homework for you, so if you want a selct/option list then you must provide that yourself. I will also leave it to you to display the result as a whiole number.

BTW, when posting here please follow the posting guidelines and wrap your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

In theory, there isn't any difference between the theory and practice. In practice, there is.
My apologies for not using the Code tags.

Also, my apologies for giving you the impression that I wanted anyone to do my homework for me--as I said, I was only looking for someone to point me in the right direction, i.e., if I should use an array to solve the problem, or which portion of the script I should focus on... I am sorry if I was not clear enough on that.

Thank you for your assistance though--that does indeed help! I was getting lost in terms of what kind of variables to make and how to combine them. Thank you!

•

1. ###### JavaScript - World Clock - Add Day To The Output - AllWebDevHelp.com
08-28-2014, 01:35 PM