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

Thread: Pi

  1. #1
    New Coder
    Join Date
    Sep 2006
    Location
    Toronto
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Pi

    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.
    var currentTime = new Date()
    var chance = currentTime.getTime()
    var chance2 = chance % 3
    alert(chance2)

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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
    Code:
    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.

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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
    Last edited by brandonH; 12-15-2006 at 02:49 PM.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #4
    New Coder
    Join Date
    Sep 2006
    Location
    Toronto
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.
    var currentTime = new Date()
    var chance = currentTime.getTime()
    var chance2 = chance % 3
    alert(chance2)


  •  

    Posting Permissions

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