Open In App

Print all Positive Numbers in a Range in JavaScript Array

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To print all the possible numbers in a range from the JavaScript array, we need to iterate over the array and apply the condition to filter out the positive 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]
Output: 45, 63, all positive numbers lies inside the range

Using for Loop

In this approach, we are using the for loop to iterate over each element of the array and check if each element is less than 0, which means it is a negative number.

Syntax:

for (initialization; condition; increment/decrement) {
// code
}

Example: The below code example uses the for Loop to print all positive numbers in a range of 10 to 40 in a JavaScript array.

Javascript




let arr =
    [12, 34, 13, -1, -4, 45, -55, -66];
let start = 10;
let end = 40;
 
for (let i = 0; i < arr.length; i++) {
    if (arr[i] > 0 && arr[i] >= start
        && arr[i] <= end) {
        console.log(arr[i]);
    }
}


Output

12
34
13

Using forEach method

In this approach, we are using the forEach method to iterate over each element of the array to get each of the negative numbers.

Syntax:

array.forEach(function(currentValue, index, array) {
// code
});

Example: The below code example uses the forEach method to print all positive numbers in a range of 10 to 20 in a JavaScript array.

Javascript




let arr =
    [12, 34, 13, -1, -4, 45, -55, -66];
let start = 10;
let end = 20;
 
arr.forEach(number => {
    if (number > 0 && number >= start
        && number <= end) {
        console.log(number);
    }
});


Output

12
13

Using filter method

In this approach, we are using the built-in filter method to create the new array which consists of only the negative numbers from the input array.

Syntax:

const res = array.filter(function(currentValue, index, array) {
// code
});

Example: The below code example uses the filter method to print all positive numbers in a range of 10 to 40 in a JavaScript array.

Javascript




let arr =
    [12, 34, 13, -1, -4, 45, -55, -66];
let start = 10;
let end = 40;
 
let res =
arr.filter(number => number > 0 &&
            number >= start &&
            number <= end);
 
console.log(res);


Output

[ 12, 34, 13 ]

Using for…of Loop

In this approach, we are using the for…of loop to iterative over each element of the input arr and check if the number of the array is less than 0. If the number is less than 0, then it is printed in the console.

Syntax:

for (variable of iterable) {
// code
}

Example: The below code example uses the for…of Loop to print all positive numbers in a range of 10 to 40 in a JavaScript array.

Javascript




let arr =
    [12, 34, 13, -1, -4, 45, -55, -66];
let start = 10;
let end = 40;
 
for (let number of arr) {
    if (number > 0 && number >= start && number <= end) {
        console.log(number);
    }
}


Output

12
34
13


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads