Question

in JavaScript - Create a function titled "counter" that takes an array of numbers as the...

in JavaScript - Create a function titled "counter" that takes an array of numbers as the parameter. The function is then to return an array holding the numbers of negative elements, zeros, and values greater than zero in the given array parameter.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please do comment if you have any doubt regarding this program

Program ScreenShot:

Sample Output:

Program Code:

<!DOCTYPE html>
<html>
<head>
   <title>Array numbers</title>
</head>
<body>
   <!-- paragraphs to display the elements count -->
   <p id = "arraynumbers"></p>
   <p id = "negitivenumbers"></p>
   <p id = "positivenumbers"></p>
   <p id = "zeronumbers"></p>
   <!-- JavaScript Code -->
   <script>
       // decalre and initialize an array
       array = [2, 0, -1, 6, 10, -6, 3];
       // display the array in the id = arraynumbers paragraph
       document.getElementById("arraynumbers").innerHTML = "The array numbers are " + array
       // decalre and initialize the countes for postive negitive and zero elements
       var negitiveCounter = 0, positiveCounter = 0, zeroCounter = 0;
       // Lopp through array length
       for(var i = 0; i < array.length ; i++){
           // if number is negitive increase negitiveCounter count
           if(array[i] < 0 ){
               negitiveCounter++;
           }
           // if number is positive increase negitiveCounter count
           else if(array[i] > 0){
               positiveCounter++
           }
           // if number is zero increase zeroCounter count
           else{
               zeroCounter++;
           }
       }
       // display the negitivenumbers count in the id = negitivenumbers paragraph
       document.getElementById("negitivenumbers").innerHTML = "The numbers of negative elements in array : " +negitiveCounter
       // display the positivenumbers count in the id = positivenumbers paragraph
       document.getElementById("positivenumbers").innerHTML = "The numbers of Positive elements in array : " +positiveCounter
       // display the zeronumbers count in the id = zeronumbers paragraph
       document.getElementById("zeronumbers").innerHTML = "The numbers of zero elements in array : " +zeroCounter

   </script>

</body>
</html>

Add a comment
Know the answer?
Add Answer to:
in JavaScript - Create a function titled "counter" that takes an array of numbers as the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT