Home > Web Front-end > JS Tutorial > body text

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

醉折花枝作酒筹
Release: 2023-01-04 09:35:26
Original
8709 people have browsed it

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>
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template