Horje
Print all Odd Numbers in a Range in JavaScript Array

Odd numbers are numbers that cannot be divided into equal parts. If we divide the odd number by 2, the remainder is 1. In JavaScript, if we want to print all Odd numbers in a range, we can print it by iterating over the array, applying the condition, and printing the odd numbers. There are various approaches to printing all odd numbers in a range in a JavaScript array which are as follows:

Using for Loop

The for loop is used to iterate over each element of the array and using the conditional block, check if the element satisfies the condition of odd numbers and also the range bound. If all the conditions are satisfied, then the odd numbers are printed using the console.log() function.

Syntax:

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

Example: The example uses the for Loop to print all odd numbers in a range of 1 to 50 in a JavaScript array.

JavaScript
let arr = [2, 5, 1, 33, 55, 45, 34, 90, -12, -44];

let start = 1;
let end = 50;
for (let i = 0; i < arr.length; i++) {
      if (arr[i] >= start && arr[i] <= 
          end && arr[i] % 2 !== 0) {
        console.log(arr[i]);
  }
}

Output
5
1
33
45

Using forEach method

The forEach method to go through each element of the array and by using the conditional statements, check if the element is odd and is within the ranges. If all the conditions are satisfied then the odd numbers are printed in the specific range.

Syntax:

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

Example: The example uses the forEach method to print all odd numbers in a range of 1 to 50 in a JavaScript array.

JavaScript
let arr = [2, 5, 1, 33, 55, 45, 34, 90, -12, -44];

let start = 1;
let end = 50;
arr.forEach((num) => {
  if (num >= start && num <= end && num % 2 !== 0) {
    console.log(num);
  }
});

Output
5
1
33
45

Using filter method

The filter() method is used to create the array which consists of only the elements that satisfy the condition of odd elements along with the range bound. The new output array is printed as output.

Syntax:

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

Example: The code example uses the filter method to print all odd numbers in a range of 1 to 50 in a JavaScript array.

JavaScript
let arr = [2, 5, 1, 33, 55, 45, 34, 90, -12, -44];
let start = 1;
let end = 50;
let res = arr.filter((num) => num >= start &&
num <= end && num % 2 !== 0);
console.log(res);

Output
[ 5, 1, 33, 45 ]

Using for…of Loop

The for…of the loop is used to iterate through the array elements, and using the condition we are checking if the element is an odd number and is within the start and end range. If all the conditions are true, then we are printing the odd number as output.

Syntax:

for (variable of iterable) {
  // code
}

Example: The example uses the for…of Loop to print all odd numbers in a range of 1 to 50 in a JavaScript array.

JavaScript
let arr = [2, 5, 1, 33, 55, 45, 34, 90, -12, -44];
let start = 1;
let end = 50;
for (let num of arr) {
  if (num >= start && num <= end && num % 2 !== 0) {
    console.log(num);
  }
}

Output
5
1
33
45

Using recursion

In this approach we define a recursive function printOdd that takes start and end parameters. Inside the function, we check if the current number (start) is odd, If it is we log it to the console. If start is greater than end, we stop the recursion.

Example: The example uses recursion to print all odd numbers in a range of 1 to 50 in a JavaScript array.

JavaScript
function printOdd(arr, start, end, index = 0) {
    if (index >= arr.length) {
        return;
    }

    if (arr[index] >= start && arr[index] <= end && arr[index] % 2 !== 0) {
        console.log(arr[index]);
    }

    printOdd(arr, start, end, index + 1);
}

let arr = [2, 5, 1, 33, 55, 45, 34, 90, -12, -44];
let start = 1;
let end = 50;

printOdd(arr, start, end);

Output
5
1
33
45



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Print ASCII Value of a Character in JavaScript Print ASCII Value of a Character in JavaScript
How to Create a Typed Array from an Object with Keys in TypeScript? How to Create a Typed Array from an Object with Keys in TypeScript?
How to Remove Multiple Object from Nested Object Array ? How to Remove Multiple Object from Nested Object Array ?
What is the use of the parseFloat() function in JavaScript? What is the use of the parseFloat() function in JavaScript?
What is local scope in JavaScript? What is local scope in JavaScript?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16