简体   繁体   English

编写程序以将所有奇数加到数组中

[英]Writing a program to add all the odd numbers in an array

 // Write a program to sum all the odd elements of an array

    a = Number(prompt("a:"));
    b = Number(prompt("b:"));
    c = Number(prompt("c:"));

    sum=Number(0);

    var test = [a,b,c];

    for (i=0; i<test.length;i++) {
        // All even numbers can be divided by 2
        if ((test[i]%2)>0) {
            alert(test[i] + ":odd");
        } else {
            alert(test[i] + ":even");
        }
        test[i]=0;
   }

   sum=0+test[i];
   alert(sum);

My program works brilliant up until its meant to add up all the numbers where it returns a NaN message! 我的程序工作得很出色,直到将所有返回NaN消息的数字加起来为止! Any ideas on how I can sort this problem out? 关于如何解决此问题的任何想法?

I see three problems with your code. 我发现您的代码存在三个问题。 (Not counting the weird formatting of the curly brackets.) (不包括大括号的怪异格式。)

  1. Edit: looks like the question has been updated a couple of times since I started my answer. 编辑:自从我开始回答以来,问题似乎已经更新了两次。 In the "if odd" test you are setting the number to zero with Perhaps you meant to zero out the even numbers so that you could later add up all the numbers and just get the odd ones, but test[i]=0 is not part of the if/else structure. 在“如果是奇数”测试中,您将数字设置为零,也许您的意思是将偶数归零,以便以后可以将所有数字加起来并得到奇数,但是test[i]=0并不是if / else结构的一部分。 It looks like it should be because of the indenting, but JavaScript ignores extra white-space. 看起来应该是因为缩进,但是JavaScript忽略了多余的空格。

  2. You are not adding the numbers to the sum within your loop. 您没有将数字加到循环中的sum

  3. You are getting NaN because of this line: 由于以下原因,您得到的是NaN:

     sum=0+test[i]; 

That statement is outside the end of the loop, so at that point i is equal to the length of the array and test[i] is undefined . 该语句在循环末尾之外,因此在那一点上, i等于数组的长度,而test[i] undefined

Try the following pseudo-code: 尝试以下伪代码:

sum = 0
for (loop through the array) {
   if current number is odd
      sum = sum + current number
}

Your sum (sum=0+test[i];) is outside the for loop. 您的总和(sum = 0 + test [i];)在for循环之外。 You should move it inside the for loop to get the sum in your "sum" variable. 您应该将其移动到for循环中,以将总和添加到“ sum”变量中。

var A= [17, 6, 19, 27, 56, 73, 43, 70, 41, 48, 
10, 69, 22, 71, 53, 11, 40, 72, 32, 25, 14, 54, 
13, 38, 62, 66, 2, 37, 60, 75, 52, 33, 58, 30, 
61, 5, 57, 49, 21, 34, 67, 51, 16, 45, 64, 24, 
23, 20, 47, 65, 46, 18, 1, 44, 15, 42, 68, 26, 
74, 7, 55, 36, 8, 50, 9, 59, 31, 3, 4, 29, 
35, 39, 63, 12, 28];

A.filter(function(i){return i%2}).reduce(function(a, b){return a+b});

returned value: (Number) 1444 返回值:(数字)1444

//equalizer for old browsers
if(![].filter){
    Array.prototype.filter= function(fun, scope){
        var T= this, A= [], i= 0, itm, L= T.length;
        if(typeof fun== 'function'){
            while(i< L){
                if(i in T){
                    itm= T[i];
                    if(fun.call(scope, itm, i, T)) A[A.length]= itm;
                }
                ++i;
            }
        }
        return A;
    }
}
if(![].reduce){
    Array.prototype.reduce= function(fun, temp, scope){
        var T= this, i= 0, len= T.length, temp;
        if(typeof fun=== 'function'){
            if(temp== undefined) temp= T[i++];
            while(i < len){
                if(i in T) temp= fun.call(scope, temp, T[i], i, T);
                i++;
            }
        }
        return temp;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM