JavaScript program to count positive and negative numbers in an array

Given an array of numbers. Write a JavaScript program to find positive and negative numbers in an array.
Example:
Input: numbers_array1= [10,20, -1,22,99,20, -9]
Output: positive no’s=5, negative no’s =2Input: numbers_array2= [-121, – 78, -13, 54, -23]
Output: positive no’s=1, negative no’s=4
Example 1: This code count positive and negative numbers from the given array using JavaScript for loop. Iterate each element in the list using for loop and check if (num >= 0), the condition to check negative numbers. If the condition satisfies, then increase negative count else increase the positive count
Javascript
| <script>        varnumbers=[10,-12,89,56,-83,8,90,-8]    varpos_count=neg_count=0    for(let i=0;i<numbers.length;i++)    {      if(numbers[i]<0)       neg_count++;      else       pos_count++;    }    console.log(`The positive numbers inan array is ${pos_count}`)    console.log(`The negative numbers inan array is ${neg_count}`)</script> | 
The positive numbers in an array is 5 The negative numbers in an array is 3
Example 2: The following code uses JavaScript while loop.
Javascript
| <script>       varnumbers=[7,-8,55,-87,28,74,-21,54,4]    varpos_count=neg_count=i=0    while(i<numbers.length)    {      if(numbers[i]<0)       neg_count++;      else       pos_count++;      i++;    }    console.log(`The positive numbers inan array is ${pos_count}`)    console.log(`The negative numbers inan array is ${neg_count}`)</script> | 
The positive numbers in an array is 6 The negative numbers in an array is 3
Example 3: The following code uses JavaScript using forEach Loop
Javascript
| varnumbers=[-8,10,23,44,-80,-15,-13,-1]varpos_count=neg_count=0numbers.forEach(element => {  if(element<0)   neg_count++;  else   pos_count++;});console.log(`The positive numbers inan array is ${pos_count}`)console.log(`The negative numbers inan array is ${neg_count}`) | 
The positive numbers in an array is 3 The negative numbers in an array is 5
Example 4:Using map method
Javascript
| varnumbers=[10,-12,89,56,-83,8,90,-8]varpos_count=neg_count=0numbers.map(function(element){  if(element<0)   neg_count++;  else   pos_count++;});console.log(`The positive numbers inan array is ${pos_count}`)console.log(`The negative numbers inan array is ${neg_count}`) | 
The positive numbers in an array is 5 The negative numbers in an array is 3
Example 5:Using Recursion
Javascript
| functioncountNumbers(numbers, index, pos_count, neg_count) {    if(index < numbers.length) {        if(numbers[index] < 0) {            neg_count++;        } else{            pos_count++;        }        returncountNumbers(numbers, index + 1, pos_count, neg_count);    } else{        return{ pos_count, neg_count };    }}varnumbers=[-8,10,23,44,-80,-15,-1]varcounts = countNumbers(numbers, 0, 0, 0);console.log(`The positive numbers inan array is ${counts.pos_count}`)console.log(`The negative numbers inan array is ${counts.neg_count}`) | 
The positive numbers in an array is 3 The negative numbers in an array is 4
Efficient Code:-
This code is more efficient because it eliminates the need for recursion and the function calls that come with it, and instead uses a simple for loop to iterate through the array. This should result in faster execution and less memory usage.
Javascript
| functioncountNumbers(numbers) {    let pos_count = 0;    let neg_count = 0;    for(let i = 0; i < numbers.length; i++) {        if(numbers[i] < 0) {            neg_count++;        } else{            pos_count++;        }    }    return{ pos_count, neg_count };}varnumbers = [-8, 10, 23, 44, -80, -15, -1]              varcounts = countNumbers(numbers);console.log(`The positive numbers inan array is $ {counts.pos_count}`)console.log(`The negative numbers inan array is $ {counts.neg_count}`) | 
C++
| #include <iostream>usingnamespacestd;structCounts {    intpos_count;    intneg_count;};Counts countNumbers(intnumbers[], intsize) {    Counts counts = {0, 0};    for(inti = 0; i < size; i++) {        if(numbers[i] < 0) {            counts.neg_count++;        } else{            counts.pos_count++;        }    }    returncounts;}intmain() {    intnumbers[] = {-8, 10, 23, 44, -80, -15, -1};    intsize = sizeof(numbers) / sizeof(numbers[0]);    Counts counts = countNumbers(numbers, size);    cout << "The positive numbers in an array is "<< counts.pos_count << endl;    cout << "The negative numbers in an array is "<< counts.neg_count << endl;    return0;} | 
Java
| classMain {    staticclassCounts {        intpos_count;        intneg_count;    }    staticCounts countNumbers(int[] numbers) {        Counts counts = newCounts();        counts.pos_count = 0;        counts.neg_count = 0;        for(inti = 0; i < numbers.length; i++) {            if(numbers[i] < 0) {                counts.neg_count++;            } else{                counts.pos_count++;            }        }        returncounts;    }    publicstaticvoidmain(String[] args) {        int[] numbers = {-8, 10, 23, 44, -80, -15, -1};        Counts counts = countNumbers(numbers);        System.out.println("The positive numbers in an array is "+ counts.pos_count);        System.out.println("The negative numbers in an array is "+ counts.neg_count);    }} | 
C#
| usingSystem;classMainClass {    structCounts {        publicintpos_count;        publicintneg_count;    }    publicstaticCounts countNumbers(int[] numbers) {        Counts counts = newCounts { pos_count = 0, neg_count = 0 };        for(inti = 0; i < numbers.Length; i++) {            if(numbers[i] < 0) {                counts.neg_count++;            } else{                counts.pos_count++;            }        }        returncounts;    }    publicstaticvoidMain(string[] args) {        int[] numbers = newint[] {-8, 10, 23, 44, -80, -15, -1};        Counts counts = countNumbers(numbers);        Console.WriteLine("The positive numbers in an array is "+ counts.pos_count);        Console.WriteLine("The negative numbers in an array is "+ counts.neg_count);    }} | 
The positive numbers in an array is $ {counts.pos_count}
The negative numbers in an array is $ {counts.neg_count}
Example 7: using the Array.prototype.filter() method to filter out positive and negative numbers
Javascript
| // defining the array of numbersvarnumbers=[-8,10,23,44,-80,-15,-13,-1]// creating a new array containing only positive numbers using filter methodvarpositiveNumbers = numbers.filter(function(number) {  // return only numbers greater than or equal to 0  returnnumber >= 0;});// creating a new array containing only negative numbers using filter methodvarnegativeNumbers = numbers.filter(function(number) {  // return only numbers less than 0  returnnumber < 0;});// printing the count of positive numbers in the arrayconsole.log(`The positive numbers inan array is ${positiveNumbers.length}`)// printing the count of negative numbers in the arrayconsole.log(`The negative numbers inan array is ${negativeNumbers.length}`) | 
The positive numbers in an array is 3 The negative numbers in an array is 5
Time complexity: O(n)
Auxiliary Space: O(n) 
 
				 
					

