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

How to calculate the area of ​​a square using js

王林
王林forward
2020-04-04 09:05:373808browse

How to calculate the area of ​​a square using js

You can use a Math.pow() function to calculate the area of ​​a square using JS.

Let’s first look at the implementation effect:

How to calculate the area of ​​a square using js

Example code:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
</head>
<body>
 <form method="get" action="">
  <h2>计算正方形的面积</h2>
  正方形的边长:<input type="text" id="radius"><br>
  正方形的面积:<input type="text" readonly="readonly" id="area"><br>
  <input type="button" value="计算" onclick="show()" />
  <input type="reset" value="重置" />
 </form>
</body>
<script type="text/javascript">
 function area(radius){
  var radius=document.getElementById("radius").value;//获取正方形的边长
  var area=Math.pow(radius,2);//计算正方形的面积
  return area;
 }
 function show(){
  //输出正方形的面积
  document.getElementById("area").value=area(radius);
 }
</script>
</html>

Related tutorial recommendations: js tutorial

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

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete