JavaScript Program to Count Positive and Negative Numbers in an Array Last Updated : 25 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given an array of numbers. Write a JavaScript program to count 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=4Brute Force ApproachWe will be using the javascript for loop to iterate in the array and make sure to count each positive and negative number.Example 1: This code counts positive and negative numbers from the given array using JavaScript for loop. Iterate each element in the list using a for loop and check if (num >= 0), the condition to check negative numbers. If the condition is satisfied, then increase the negative count else increase the positive count. JavaScript let numbers= [10,-12,89,56,-83,8,90,-8] let pos_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 in an array is ${pos_count}`) console.log(`The negative numbers in an array is ${neg_count}`) OutputThe positive numbers in an array is 5 The negative numbers in an array is 3Using RecursionExample: In this example, we are going to use the recursion approach that will help us to recursively call the CountNumbers Functions and it will find the positive and negative numbers and store them to the variables. JavaScript function countNumbers(numbers, index, pos_count, neg_count) { if (index < numbers.length) { if (numbers[index] < 0) { neg_count++; } else { pos_count++; } return countNumbers(numbers, index + 1, pos_count, neg_count); } else { return { pos_count, neg_count }; } } let numbers= [-8,10,23,44,-80,-15,-1] let counts = countNumbers(numbers, 0, 0, 0); console.log(`The positive numbers in an array is ${counts.pos_count}`) console.log(`The negative numbers in an array is ${counts.neg_count}`) OutputThe positive numbers in an array is 3 The negative numbers in an array is 4 Using filter() MethodExample: We are going to use the javascript built in filter() method that will filter out the positive and negative numbers respectively. JavaScript let numbers= [-8,10,23,44,-80,-15,-13,-1] let positiveNumbers = numbers.filter(function(number) { return number >= 0; }); let negativeNumbers = numbers.filter(function(number) { return number < 0; }); console.log(`The positive numbers in an array is ${positiveNumbers.length}`) console.log(`The negative numbers in an array is ${negativeNumbers.length}`) OutputThe positive numbers in an array is 3 The negative numbers in an array is 5 Comment More infoAdvertise with us Next Article JavaScript Program to Count Positive and Negative Numbers in an Array T tupakulateja3 Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2022 JavaScript-Questions +1 More Similar Reads C++ Program to Count Positive and Negative Numbers in an Array Given an array arr of integers of the size of N, our task is to find the count of positive numbers and negative numbers in the array. Examples: Input: arr[] = [-9,7,-5,3,2]Output: Positive elements = 3, Negative elements = 2 Input: arr[] = [5,4,-2,-1,-7]Output: Positive elements = 2, Negative elemen 5 min read Print all Negative numbers in a Range in JavaScript Array To print all the negative numbers in a range from the JavaScript array, we need to iterate over the array and apply the condition to filter out the negative numbers. Once the elements are extracted, we can print them in the console. Example:Input: arr = [-22, 45, 63, -88, -69]Range: [-1 to -70]Outpu 3 min read Javascript Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If the 3 min read Print all Positive Numbers in JavaScript Array In JavaScript, arrays are the data structures used to store multiple elements. In some cases, we need to extract the elements that satisfy a specified condition. We can use a condition to extract all the positive numbers from an array with the below approaches. Example:Input: arr = [-1, -12, 35, -8, 3 min read Print all Negative Numbers in JavaScript Array In JavaScript, arrays are the data structures used to store the elements. In some cases, we need to extract the elements that satisfy the condition. To print all the negative numbers from the JavaScript array, we need to iterate over the array and apply the condition to filter out the negative numbe 4 min read Rearrange given array to obtain positive prefix sums at exactly X indices Given an array arr[] consisting of N integers whose absolute values are distinct and an integer K, the task is to find such an arrangement of the given array by the following operations, such that it has positive prefix sum at exactly K places: Choose two integers i, j and swap the values of the ele 8 min read Create Array following condition such that exactly M prefixes have positive Sum You are given two integers N and M (M ⤠N), the task is to create an array of length N following the below-given conditions. |Ai| = i, Formally, A[i] will be either -i or i.Exactly M prefix arrays should be there, Such that sum of each prefix array should be greater than zero. Note: If there are mul 10 min read Count ways to obtain triplets with positive product consisting of at most one negative element Given an array arr[] of size N (1 ? N ? 105), the task is to find the number of ways to select triplet i, j, and k such that i < j < k and the product arr[i] * arr[j] * arr[k] is positive. Note: Each triplet can consist of at most one negative element. Examples: Input: arr[] = {2, 5, -9, -3, 6 5 min read Noble integers in an array (count of greater elements is equal to value) Given an array arr[], find a Noble integer in it. An integer x is said to be Noble in arr[] if the number of integers greater than x is equal to x. If there are many Noble integers, return any of them. If there is no, then return -1. Examples : Input : [7, 3, 16, 10] Output : 3 Number of integers gr 15+ min read Number of subsequences with negative product Given an array arr[] of N integers, the task is to find the count of all the subsequences of the array that have a negative products. Examples: Input: arr[] = {1, -5, -6} Output: 4 Explanation {-5}, {-6}, {1, -5} and {1, -6} are the only possible subsequences Input: arr[] = {2, 3, 1} Output: 0 Expla 6 min read Like