...

View Full Version : Pi



1337coder
12-15-2006, 01:26 AM
Okay, something is not working. When I type:
var x = Math.PI();
it counts as an error, and causes the rest of the script to malfunction. So, instead of using the built-in javascript pi, I made my own. It usually works, but it does not work for the following code:
function spha(){
var di = prompt("Type the radius of the sphere", "")
if(di == 0){
}else{
var dj = di * di
var dk = dj * di
var dl = dk * pi
var dm = dl / 3
var dn = dm * 4
alert(di)
}
}
The prompt comes up, but nothing happens after that. It is not counted as an error, so I cannot do anything with a debugger. I learned, by taking away certain lines and observing, that the line "var dl = dk * pi" is the line causing the problem. When I use a number instead of a variable, it works. I can't do that, because at the begginging, I prompt the user if they want "3.14", or pi to 50 digits. The coding for that is:

function pivalue(){
var pivalue = prompt("Type 1 if you want pi to be 3.14, and type 2 if you want it to be pi recited to 50 digits. If you type something other than 1 or 2, pi will automatically represent 3.14", "")
if(pivalue == 1){
var pi = 3.14
else if(pivalue == 2){
var pi = 3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510
}else{
var pi == 3.14
}
}

The function "pivalue()" is triggered by body onLoad. I have tried it like that, and without a function. Neither worked. I have also tried simply stating "var pi = 3.14", but it still does not work. To recap, pi does not work if I contain it within a variable, it works when I type the numerical value instead of the variable, I need it to be in a variable, and it works fine in a variable elsewhere. Can anyone help me? Thanks in advance.

koyama
12-15-2006, 02:21 AM
Well your effort for solving this is heroic, but I think what you needed was simply Math.PI instead of Math.PI()

This looks like home work?

Another mistake is that when you have (in JavaScript) something like


function pivalue() {
...
var pi = 3.14
...
}

you are declaring pi as a local variable which becomes inaccessible outside the function pivalue, but you are trying to use it from the function spha anyway. You want a global variable here and must leave out the keyword 'var' in front of pi. This is why your 2nd attempt also fails.

brandonH
12-15-2006, 04:58 AM
also, what is the point of:

var dj = di * di
var dk = dj * di
var dl = dk * pi
var dm = dl / 3
var dn = dm * 4



should write it out as one var:

var dn=((pow(di,'3')*pi)/3)*4

pow(di,3) is di*di*di

1337coder
12-15-2006, 01:15 PM
Thanks koyama, I just needed to take it out of the function (and, no, this isn't homework, I'm just learning javascript myself). And, Brandon, I would have done all of the calculations in one variable but, unless I try to space things out, I end up with errors. Also, I found that using Math.pow did not work with a variable and a number, you need 2 variables. Therefore, it would have been around the same to set a variable to 3, as it would take 2 lines.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum