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

# Thread: Maths - Squaring Variables

1. ## Maths - Squaring Variables

Hi there,

I have a variable which I'm dividing ..

PHP Code:
``` var ltotal = (data/12)  ```
I now need to sqaure this, and also cube it after that ..

Not sure at all how to square and then cube this result, seperately .. can anyone give me some pointers?

thanks all
p

• the 'Math' object (built-in) has a 'pow()' function that given 2 arguments, returns the first, to the power of the second.
i.e.
[code]
var a = Math.pow(3,2);
//a=9
var b = Math.pow(2,3);
//b=8

• Hi Jay,

I saw Math.pow on javascriptkit but got stuck with it...

This is what I had ...
var y = Math.pow('total',2);
var x = Math.pow('total',3);
total = y + x;
So the above was meant to square total, and then cube total, and then add them both toegther...

but it prints NaN

No idea what that means or what I did wrong, any thoughts?

cheers
p

• yo

after reading some more... the NaN is returned because I also use Math.log ... the variable that is returned is negitive ...

Does anyone know how to print a negitive variable, rather than printing NaN?

thank you
p

• infact scrap that, i have look into it and the variable that is outputted should not be negitive.. so something is wrong ..

calculation is this ...

var data = 2500
var total = (data+1);
var total = Math.log(total);
var y = Math.pow('total',2);
var x = Math.pow('total',3);
total = y + x;
Now, total prints NaN .. but how can that be negitive from the above?

any ideas?

thanks again
p

• Remove the ' quotes around 'total'.

'total' in quotes is a literal string varaible, which is of course NaN.

• hi phil thansk for replying ..

took out the quotes. still get NaN ...

any further thoughts?

thank you

• Had a look again and it returns NaN before it does the math.pow.. it gets NaN from the math.log ...

the data var is not 1000 in the real thing it is actually 2.4501.01325 ... is it a problem that it has decimal places etc?

many thanks

• Works fine for me.

<SCRIPT>
var data = 2500;
var total = (data+1);
var total = Math.log(total);
var y = Math.pow(total,2);
var x = Math.pow(total,3);
total = y + x;
</SCRIPT>

Result is 540.249823..........

• Thanks again Phil ..

Started using your alerts to debug.. found the problem, variable names clashing ...

d;'oh.

thanks for all your help, great work!

•

#### Posting Permissions

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