Math.pow
There are some math calculations that require you to use exponents. Math.power(x, y) does not change the value of x or y, rather it returns the result of x to the power of y.
Examples
var z = Math.pow(4, 2);
console.log(z);
Area of a Circle
Input the radius of a circle and hit "Calculate" to find the area of that circle. This example also uses Math.round to display results.
textLabel("title", "Area of a Circle");
textLabel("question", "What's the radius of your circle?");
textInput("radiusInput", "");
button("calculate", "Calculate");
textLabel("answer", "");
onEvent("calculate", "click", function( ) {
var radius = getNumber("radiusInput");
var area = 3.14 * Math.pow(radius, 2);
setText("answer", "Circle Area = " +Math.round(area));
});
Syntax
Math.pow(x,y);
Parameters
Name | Type | Required? | Description |
---|---|---|---|
x | Number | The base, or number which will be taken to the power of y | |
y | Number | The exponent or power |
Returns
Found a bug in the documentation? Let us know at support@code.org.