Domanda

I have been working on a PI calculation program using javascript.

     circumference
Pi = -------------
       diameter

When r = the radius, the circumference = 2πr, and the diameter = 2r.

This is what I have tried to do:

    var pi = Math.Pi,
    var r = 2
    ans = (2 * pi * r) / (2 * r);
    alert(ans);

Seems like it should work, right? But instead I get an alert message that says, "NaN."

Is there any way I can make a function like this:

function divide(numerator, denominator, decimal_places){
    //code
}
//and then:
divide(2πr, 2r, 30);//that would calculate Pi to 30 decimal places.

Does anyone know how I could make such a function using javascript? Thank you so much.

È stato utile?

Soluzione

It's PI, not Pi - javascript is case-sensitive. + omit the second var:

var pi = Math.PI,
    r = 2
ans = (2 * pi * r) / (2 * r);
alert(ans);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top