gpt4 book ai didi

javascript - 距离=费率*时间计算

转载 作者:行者123 更新时间:2023-12-02 18:16:58 26 4
gpt4 key购买 nike

我正在尝试用 JavaScript 编写一个匿名函数,它将使用公式速率 * 时间来计算并返回行驶的距离。函数定义需要分配给变量。那么需要使用该变量调用该函数。

但是我在使用代码时遇到了问题,任何指出我做错了什么的人都会受到赞赏。

<html>
<head><title>Anonymous Function</title>
<script type="text/javascript">

var d = function(){
}

return d;
}

</script>
</head>
<body>

<script type="text/javascript">
var rate = prompt('how fast plan to drive (in mph)');
rate = parseInt(rate);
var time = prompt('how much time do you plan to drive? (in minutes)');
time = parseInt(time);
distance = rate * time;
d = distance;
</script>
</big>
</body>
</html>

最佳答案

var distance = function(rate, time) {
return rate * time;
}

var rate = prompt('how fast plan to drive (in mph)');
rate = parseInt(rate);
var time = prompt('how much time do you plan to drive? (in minutes)');
time = parseInt(time);
d = distance(rate, time);
alert("You traveled "+d+" miles");

关于javascript - 距离=费率*时间计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19193214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com