如何使我的函数在javascript中使用来自用户的输入(带提示)计算矩形区域? [英] How do I make my function calculate the area of a rectangle with inputs (with prompt) from the user in javascript?

查看:37
本文介绍了如何使我的函数在javascript中使用来自用户的输入(带提示)计算矩形区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个函数来计算矩形的面积,周长和体积,提示用户输入高度,宽度和长度,并使用document.writeln()写出输入和输出。



我尝试过:



var length = prompt(输入一个整数对于矩形的长度。);

var width = prompt(输入矩形宽度的整数。);



功能区(长度,宽度){

返回长度*宽度;

}



console.log(矩形区域为+区域;

解决方案

尝试



< html> 
< head>
< script>
var length = prompt(输入矩形长度的整数。);
var width = prompt(输入矩形宽度的整数。);
函数区域(长度,宽度){
返回长度*宽度;
}
函数周长(长度,宽度){
返回2 *(长度+宽度);
}

document.writeln('矩形区域为'+区域(长度,宽度));
document.writeln('矩形的周长是'+周长(长度,宽度));


< / script>
< / head>
< body>

< / body>
< / html>

 


请查看

 <  !DOCTYPE     html  >  
< html >
< body >

< p > 点击按键输入。< / p >

< 按钮 onclick = myFunction() > 尝试< / button >

< p id = a > < / p >
< p id = p > < / p >
< p id = v > < / p >

< script >
< span class =code-keyword> function myFunction(){

var length = prompt( 输入矩形长度的整数。);
var width = prompt( 输入矩形宽度的整数。);
var depth = prompt( 输入矩形棱镜深度的整数);


var 周长=( 2 * length)+( 2 * width);
var area = length * width;
var volume = length * width * depth;

document .getElementById( a)。innerHTML =
矩形区域: +区域;
document .getElementById( p).innerHTML =
矩形周长: +周长;
document .getElementById( v).innerHTML =
矩形棱镜体积: +体积;
}
< / 脚本 >

< / body >
< / html >


在JavaScript中,一切都会执行,除非它们绑定到上下文或是一个函数。在执行函数之前需要调用函数。只需更改以下行,

  console  .log(  矩形区域为 +区域(长度,宽度);  //  <  - 此处更改。 



那是因为,area是你持有的JavaScript代码中的一个函数,所以你需要调用它,你还需要将参数传递给它。有关函数的更多信息,请阅读此MDN文档,函数 - JavaScript | MDN [ ^ ]。


I have to create a function to calculate the area, perimeter, and volume of a rectangle, prompt user to enter height, width, and length, and write out inputs and outputs using document.writeln().

What I have tried:

var length = prompt("Enter a whole number for the length of your rectangle.");
var width = prompt ("Enter a whole number for the width of your rectangle.");

function area (length, width) {
return length * width;
}

console.log(The area of your rectangle is " + area);

解决方案

try

<html>
<head>
    <script >
        var length = prompt("Enter a whole number for the length of your rectangle.");
        var width = prompt("Enter a whole number for the width of your rectangle.");
        function area(length, width) {
            return length * width;
        }
        function perimeter(length, width) {
            return 2*( length + width);
        }
        
        document.writeln('The area of your rectangle is ' + area(length, width));         
        document.writeln('The perimeter of your rectangle is ' + perimeter(length, width));

             
    </script>
</head>
<body>
    
</body>
</html>


Please have a look

<!DOCTYPE html>
<html>
<body>

<p>Click the button keyin inputs .</p>

<button onclick="myFunction()">Try it</button>

<p id="a"></p>
<p id="p"></p>
<p id="v"></p>

<script>
function myFunction() {
  
var length = prompt("Enter a whole number for the length of your rectangle.");
var width = prompt ("Enter a whole number for the width of your rectangle.");
var depth= prompt ("Enter a whole number for the depth of your rectangle prism");

   
var perimeter = (2 * length ) + (2 * width );
var area= length * width ;
var volume= length * width * depth;

 document.getElementById("a").innerHTML =
        "Area of rectangle:"  + area;
 document.getElementById("p").innerHTML =
        "perimeter of rectangle:"  + perimeter ;
 document.getElementById("v").innerHTML =
        "volume of rectangle prism:"  + volume;
}
</script>

</body>
</html>


In JavaScript, everything executes unless they are bound to a context or are a function. Functions need to be called before they can execute. Just change the following line,

console.log("The area of your rectangle is " + area(length, width)); // <- Change here.


That is because, area is a function in JavaScript code that you hold, so you need to call it, and you also need to pass the parameters to it. For more on functions, please read this MDN documentation, Function - JavaScript | MDN[^].


这篇关于如何使我的函数在javascript中使用来自用户的输入(带提示)计算矩形区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆