0

I can't seem to list the number of even integers in the array, also I can't get a result when I multiply the array integers by a multiplier. I have been at it for a long time, I can't think anymore. Thanks in advance!

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

2 Answers 2

1

You need to return after the loop completes:

// 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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Hi Appears a small coding oversight here for counting the even numbers. Your "return evens" is just after pushing the even into the "evens array" so basically you are jumping out of function too early. Please take the below function as modified function, (note the return statement)

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

    }

4 Comments

Uncomment the result2 multiplication statement as well to get the multiply working.
Thanks! I am now getting the other even number. However, I am still getting 0 for the multiplication function.
Have you uncommented the line "result2 = multiply(list, multiplier);"
Wow! I feel stupid. I have been at this for hours I just can't see clearly anymore. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.