Home > Article > Web Front-end > How to find the perimeter of a rectangle in JavaScript
How to find the perimeter of a rectangle in JavaScript: First create an HTML sample file; then add a script tag to the body; and finally use prompt in js to get the width and height of the rectangle to calculate the perimeter.
The operating environment of this article: windows7 system, javascript version 1.8.5, DELL G3 computer
How to find the perimeter of a rectangle using JavaScript?
Use prompt in js to get the width and height of the rectangle to calculate the perimeter and area
prompt("msg","defaultText")//The first parameter is the slenderness Prompt information is required in the box. The second parameter is the default value of the text that needs to be output in the box. You can manually enter the value that needs to be entered
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script language="JavaScript"> var w=prompt("请输入矩形的宽","1");//如果是高是5的话就输入5 var h=prompt("请输入矩形的高","2"); alert(typeof h); alert("矩形的面积为:"+h*w); alert("矩形的周长为:"+2*(parserFloat(h)+parserFloat(w)));//不加parseFloat是不对的,结果会是42,因为+前后连接的字符而不是浮点型数字,所以需要转换一下就可以了。 </script> </body> </html>
Recommended study: "javascript advanced tutorial"
The above is the detailed content of How to find the perimeter of a rectangle in JavaScript. For more information, please follow other related articles on the PHP Chinese website!