Home  >  Article  >  Web Front-end  >  How to find the area of ​​a circle using js

How to find the area of ​​a circle using js

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-08 18:43:008662browse

Method to calculate the circle area: 1. Create an input tag and receive the radius value; 2. Use "document.getElementById('id').value" to get the radius value in the input input box; 3. , use the area formula "area=3.14*(radius*radius)" to find the area of ​​the circle.

How to find the area of ​​a circle using js

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

The specific condition is to enter a radius, then find the area, round to 2 decimal places,

<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
</head>
    <body>
    输入圆的半径:<input type="text" id="inp">
    <input type="button" value="确定" onclick="test()"> 
  </body>
  </html>
  <script>
    function test(){
        var inp = parseFloat(document.getElementById(&#39;inp&#39;).value);        
        var s = 3.14 * (inp * inp);
        alert(&#39;面积为:&#39; + s)
    }
    </script>

[Recommended learning: javascript video tutorial]

The above is the detailed content of How to find the area of ​​a circle using js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn