【问题标题】:Arrays Math Logic JavaScript数组 数学 逻辑 JavaScript
【发布时间】:2015-12-10 00:03:44
【问题描述】:

我似乎无法列出数组中偶数的数量,当我将数组整数乘以乘数时也无法得到结果。想了很久,想不出来了。提前致谢!

<!DOCTYPE HTML>
<html>

<head>
<meta charset="utf-8">
<title>EvensMultiply</title>
<script type="text/javascript">
    /*Write a defining table and a function named countEvens that counts and returns the number of even integers in an array. The function must have this header: function countEvens(list)*/

    // This function will call and test countEvens() and multiply(list, multiplier).
    function testFunctions() {
        var list = [17, 8, 9, 5, 20];
        var multiplier = 3;
        var result1 = 0;
        var result2 = 0;
        var result3 = 0;
        result1 = countEvens(list);
        //result2 = multiply(list, multiplier);
        result3 = "These are the even numbers of the array list: " + result1 + "<br>" + "This is the array list multiplied by 3: " + result2;
        document.getElementById("outputDiv").innerHTML = result3;
    }
    // This function will find the even intergers in the array.
    function countEvens(list) {
        var evens = [];
        for (var i = 0; i < list.length; ++i) {
            if ((list[i] % 2) === 0) {
                evens.push(list[i]);
                return evens;
            }
        }

    }
    /*Write a defining table and a function to multiply each element in an array by some value. The function must have this header: function multiply(list, multiplier)*/

    // This function will multiply the array list by a multiplier.
    function multiply(list, multiplier) {
        var products;
            products=list.map(function(list){return list * multiplier;});
        return products;
    }
 </script>
 </head>
 <h1>Find evens and multiply by multiplier.</h1>
 <h2>Array list [17, 8, 9, 5, 20]</h2>
 <h3>Click the Compute button to test.</h3>
 <button type="button" onclick="testFunctions()">Compute</button>
 <div id="outputDiv"></div>

 </html>

【问题讨论】:

    标签: javascript arrays math logic


    【解决方案1】:

    循环完成后需要返回:

    // This function will find the even intergers in the array.
    function countEvens(list) {
        var evens = [];
        for (var i = 0; i < list.length; ++i) {
            if ((list[i] % 2) === 0) {
                evens.push(list[i]);
            }
        }
        return evens;
    }
    

    【讨论】:

      【解决方案2】:

      嗨,这里出现了一个小的编码疏忽,用于计算偶数。您的“返回偶数”只是在将偶数推入“偶数数组”之后,所以基本上您过早地跳出了功能。请把下面的函数当作修改函数,(注意return语句)

      function countEvens(list) {
              var evens = [];
              for (var i = 0; i < list.length; ++i) {
                  if ((list[i] % 2) === 0) {
                      evens.push(list[i]);
                  }
              }
              return evens;
      
          }
      

      【讨论】:

      • 取消注释 result2 乘法语句以使乘法工作。
      • 谢谢!我现在得到另一个偶数。但是,乘法函数我仍然得到 0。
      • 您是否取消了“result2 = multiply(list, multiplier);”行的注释
      • 哇!我觉得我好笨。我已经在这里呆了好几个小时了,我再也看不清楚了。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多