0

我正在尝试在 JavaScript 中编写一个匿名函数,该函数 将使用公式 rate * time 计算并返回行进的距离。函数定义需要分配给一个变量。然后需要使用该变量调用该函数。

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

<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>
4

1 回答 1

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");
于 2013-10-05T02:05:59.897 回答