通过Javascript w/while循环求解Euler#2

通过Javascript w/while循环求解Euler#2,javascript,Javascript,我是一个初学者,试图用while循环来解决fibonacci问题,但我对使用while循环或数组没有信心。这绝对是一个糟糕的编码,但是有人能检查一下并告诉我修复它的方法吗 <!DOCTYPE html> <html> <head> <script type="text/javascript"> /* Each new term in the Fibonacci sequence is generated by adding the

我是一个初学者,试图用while循环来解决fibonacci问题,但我对使用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> 

/*斐波那契序列中的每个新项都是通过添加
前两届。从1和2开始,前10个术语将
是:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
通过考虑Fibonacci序列中的项,其值不
超过四百万,求偶数项之和*/
函数数学(){
arr值=[1,2];
var-fib=0;
var总和=2;

while(values[values.length]那么,它有效吗?如果有效-它很好,如果无效-它不有效。
值[values.length]
将始终不被数组的定义所定义work@JaromandaX因此,如果arr值=[1,2],则值[values.length]不等于2?正确…value.length将为2…但数组是基于零的索引…因此[0]和[1]中有值…因此,第一步,将
values.length-1
更改为
values.length-2
values.length
更改为
values.length-1
-看看这是否解决了您的问题(我还没有检查你剩下的“逻辑”)@JaromandaX很好,谢谢!但是到目前为止代码还没有运行