我正在尝试在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>
答案 0 :(得分:2)
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");