我是一名初学者,试图用while循环解决斐波那契问题,但我对使用while循环或数组没有信心。这绝对是一个糟糕的编码,但有人可以查看并告诉我修复它的方法吗?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
/* Each new term in the Fibonacci sequence is generated by adding the
previous two terms. By starting with 1 and 2, the first 10 terms will
be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not
exceed four million, find the sum of the even-valued terms.*/
function math() {
arr values = [1,2];
var fib = 0;
var sum = 2;
while (values[values.length]<4000000) {
fib = values[values.length] + values[values.length-1];
values.push(fib);
if (fib%2===0) {
sum += fib;
}
}
alert(sum);
}
</script>
</head>
<body>
<script type="text/javascript">
math();
</script>
</body>
</html>