# Thread: How can I do some multiplication/division with some script results?

1. New Comer (less than 5 posts)
Join Date
Sep 2008
Posts
3
Thanks
2
Thanked 0 Times in 0 Posts

## How can I do some multiplication/division with some script results?

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!

Thanks,
Matt

Code:
```<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()
now(startdate.getYear(),startdate.getMonth(),startdate.getDate(),startdate.getHours(),startdate.getMinutes(),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>```

2. Junior Coders
Join Date
Sep 2008
Posts
26
Thanks
2
Thanked 4 Times in 4 Posts
Hello, simply use something like this

Javascript
Code:
```var someNewVariableName1 = somevariable2 (+/-/*/...) someothervariableornumber3;

document.getElementById('niceplace').innerHTML=someNewVariableName1;```
HTML
Code:
`<div id="niceplace">&nbsp;</div>`

EDIT: Corrected the codebits. I accedently copy&pasted something that depends on a class not shown here Sorry.
Last edited by schorhr; 09-22-2008 at 06:37 AM.

3. ## The Following User Says Thank You to schorhr For This Useful Post:

musicmjw (09-23-2008)

4. New Comer (less than 5 posts)
Join Date
Sep 2008
Posts
3
Thanks
2
Thanked 0 Times in 0 Posts
I'm still having some trouble - sorry!

Where exactly within the script should I put the

Code:
`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!

AND -

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

Code:
```var shoes = "worldpop" * 2;
?
Thanks.
Last edited by musicmjw; 09-22-2008 at 01:55 PM. Reason: addition

5. This will never work:

Code:
`"worldpop" * 2`
because a quoted word is a string and only numbers may be multiplied. You could do something like:

Code:
```var worldpop = 2000;
var shoes = worldpop * 2;```
This is also highly suspect because it has an extra space:

Code:
`document.getElementById("yadayada`"). in`nerHTML=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.

6. ## The Following User Says Thank You to jscheuer1 For This Useful Post:

musicmjw (09-23-2008)

7. New Comer (less than 5 posts)
Join Date
Sep 2008
Posts
3
Thanks
2
Thanked 0 Times in 0 Posts
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,

Matt

8. Use the toFixed() method:

http://developer.mozilla.org/en/Core...Number/toFixed

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

Code:
```var num = 12.7285;
num = `num.toFixed(2)`;
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.

#### Posting Permissions

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