View Full Version : How can I do some multiplication/division with some script results?

09-22-2008, 05:55 AM
The below code shows the current population of earth. I'm a javascript newbie but after playing for a while I see that the ID "worldpop" as been assigned to the population number so that it can be called up in the body of the webpage. Is it possible to do math to this "worldpop" number and show these results on the page?

For instance I'd like to do ("worldpop"*2) and show that figure in the body of the page underneath the population number.

I hope someone can help, don't be afraid to tell me I'm in way over my head if it is tooo difficult or not possible!


<script type="text/javascript">

* World Populution Counter - By JavaScript Kit (http://www.javascriptkit.com)
* Based on code by Adam Brown
* This notice MUST stay intact for use
* Visit JavaScript Kit (http://www.javascriptkit.com) for this script and more

function maind(){
startdate = new Date()

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
if (number > 1 && j==3) {
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

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


09-22-2008, 06:28 AM
Hello, simply use something like this


var someNewVariableName1 = somevariable2 (+/-/*/...) someothervariableornumber3;



<div id="niceplace">&nbsp;</div>

EDIT: Corrected the codebits. I accedently copy&pasted something that depends on a class not shown here ;) Sorry.

09-22-2008, 01:52 PM
I'm still having some trouble - sorry!

Where exactly within the script should I put the

document.getElementById("yadayada"). innerHTML=pages


Right now I'm getting a "is null or not an object" error for that line...

Thanks for the help!


Using the above example, what is the correct syntax if I want to multiply the "worldpop" ID - is that even possible?

var shoes = "worldpop" * 2;
document.getElementById("yadayada"). innerHTML=shoes;


09-22-2008, 02:48 PM
This will never work:

"worldpop" * 2

because a quoted word is a string and only numbers may be multiplied. You could do something like:

var worldpop = 2000;
var shoes = worldpop * 2;

This is also highly suspect because it has an extra space:

document.getElementById("yadayada"). innerHTML=pages

But that may just be a typo. Once corrected, if you are still having trouble with your script finding the 'yadayada' element, it means that it isn't there on the page, at least not (parsed) at the time that the line that's looking for it executes. Or, in some browsers it may just mean that there is more than one, or some other thing with the same designation confusing the issue.

If you want more help:

Please post a link to the page on your site that contains the problematic code so we can check it out.

09-23-2008, 12:23 AM
Dear John, thanks for the fix - that seemed to do it. Now, I apologize for the simpleness of this question but am new to Javascript, how can I limit the number of decimals in the answer to one of my variables? Thanks,


09-23-2008, 03:28 AM
Use the toFixed() method:


Simply put, say you want two and only two decimal places:

var num = 12.7285;
num = num.toFixed(2);
alert(num); // alerts 12.73

One thing you have to be careful of though. The result of the toFixed() method is a string. So you cannot add to it mathematically without first converting it back into a number. This can be done by subtracting 0 (or any number) from it, or by multiplying or dividing it by another number - javascript will take a value that could be number and make it one if the operation can only be performed with numbers. Addition doesn't work that way in javascript because the addition operator (+) is also used to concatenate strings.