JavaScript Program to Count Positive and Negative Numbers in an Array
Last Updated :
25 Sep, 2024
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 =2
Input: numbers_array2= [-121, - 78, -13, 54, -23]
Output: positive no's=1, negative no's=4
Brute Force Approach
We 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 3
Using Recursion
Example: 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() Method
Example: 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
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