我尝试使用以下代码计算变量的平均年龄John
,Mary
和Ahmed
,但它不起作用:
<!DOCTYPE HTML>
<html>
<head>
<title>This is a page for testing JavaScript</title>
<script>
var John = 23;
var Mary = 25;
var Ahmed = 22;
var average = John + Mary + Ahmed / 3;
console.log(average);
</script>
</head>
<body>
<p>
JavaScript testing page, open the console and reload the page to see whats happening!
</p>
</body>
</html>
我到处玩,最终通过添加一个带有 sum 变量的额外行找到了正确的方法:
var sum = John + Mary + Ahmed; var average = sum / 3
只是想知道为什么我的第一次尝试没有奏效?