PDA

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

musicmjw
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!

Thanks,
Matt

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

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

Javascript

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

document.getElementById('niceplace').innerHTML=someNewVariableName1;

HTML

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

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

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

Where exactly within the script should I put the

?

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?

var shoes = "worldpop" * 2;

?
Thanks.

jscheuer1
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:

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.

musicmjw
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,

Matt

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

http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toFixed

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

var num = 12.7285;
num = num.toFixed(2);