数组数学逻辑JavaScript

Arrays Math Logic JavaScript

本文关键字:JavaScript 数组      更新时间:2023-09-26

我似乎无法列出数组中偶数整数的数量,而且当我将数组整数乘以乘法器时,我也无法得到结果。我已经做了很长时间了,我想不下去了。提前感谢!

<!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>

循环完成后需要返回:

// 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;
}

Hi在计算偶数时出现了一个小的编码疏忽。您的"return evens"是在将偶数推入"evens数组"之后,所以基本上您过早地跳出了函数。请将以下函数作为修改函数,(注意返回语句)

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