Get the App
SLTechnology News&Howtos  ›  Development  › 

How to calculate the area and perimeter of a circle by javascript

Shulou Source: shulou.com Published: 2022-06-02 12:30:59 10月02日 Update

This article mainly explains "javascript how to find the area and perimeter of the circle", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "javascript how to find the area and perimeter of the circle" bar!

The method of calculating the circle area and perimeter: 1, using the PI attribute of the Math object and the pow () method to calculate the area of the circle, the syntax is "Math.PI * Math.pow (radius, 2)"; 2, using the PI attribute of the Math object to calculate the perimeter of the circle, the syntax is "2 * Math.PI * radius".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

How to find the area and perimeter of a circle by javascript

You can use the Math object in JavaScript to perform mathematical operations, and the Math object is used to perform mathematical tasks.

The Math object is not a class of objects like Date and String, so there is no constructor Math ().

For example:

Var x = Math.PI; / / returns PIvar y = Math.sqrt (16); / / returns the square root of 16

There is also the pow () method

The syntax is:

Math.pow (XBI y)

The pow () method returns the y power of x.

Let's take a look at how to calculate the primary color area and perimeter based on an example, as follows:

Document / / rounding function round (number, X) {X = (! X? 2: X); return Math.round (number*Math.pow (10Magine X)) / Math.pow (10Magazine X);} var r = Number (prompt ('Please enter the radius of the circle')) If (! isNaN (r)) {var m = round (Math.PI * Math.pow (r, 2), 2); var c = round (2 * Math.PI * r); alert ('area =' + m +'\ n' + 'perimeter =' + c);} else {alert ('Please enter a number!');}

Output result:

After the input radius is 5, the output result:

Thank you for your reading, the above is the content of "how to find the area and perimeter of a circle in javascript". After the study of this article, I believe you have a deeper understanding of how to find the area and perimeter of a circle in javascript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Perimeter area object radius method grammar learning input content circles attributes mathematics examples results output rounding tasks functions primary colors that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS NVidia Shulou Information Xiaomi