Even numbers in javascript

Javascript Program to Check if a Number is Odd or Even

To understand this example, you should have the knowledge of the following JavaScript programming topics:

Even numbers are those numbers that are exactly divisible by 2.

The remainder operator % gives the remainder when used with a number. For example,

const number = 6; const result = number % 4; // 2 

Hence, when % is used with 2, the number is even if the remainder is zero. Otherwise, the number is odd.

Example 1: Using if. else

// program to check if the number is even or odd // take input from the user const number = prompt("Enter a number: "); //check if the number is even if(number % 2 == 0) < console.log("The number is even."); >// if the number is odd else
Enter a number: 27 The number is odd.

In the above program, number % 2 == 0 checks whether the number is even. If the remainder is 0, the number is even.

Читайте также:  Menu bars with css

In this case, 27 % 2 equals to 1. Hence, the number is odd.

The above program can also be written using a ternary operator.

Example 2: Using Ternary Operator

// program to check if the number is even or odd // take input from the user const number = prompt("Enter a number: "); // ternary operator const result = (number % 2 == 0) ? "even" : "odd"; // display the result console.log(`The number is $.`);
Enter a number: 5 The number is odd. 

Источник

Determine Odd / Even Numbers using JavaScript

This is how you can determine odd / even numbers using JavaScript.

By the way, I have a tutorial on it so make sure to check that out

Top comments (6)

If you care about performance, it’s actually quicker to use a bitwise test:

const isOdd = i => i&1 const isEven = i => 1&i^1 

3 likes Like Comment button

I’m a passionate Developer and Team Leader, I get excited by others successes. My downtime is filled with Social Dancing and Wilderness Backpacking.

I’ve never really understood bitwise operators.

1 like Like Comment button

I’m a passionate Developer and Team Leader, I get excited by others successes. My downtime is filled with Social Dancing and Wilderness Backpacking.

Nifty! What other ways might you determine if a number is even or odd?
How might you use this to determine if a number is divisible by 5?

1 like Like Comment button

To Determine number is divisible by 5, you can try

let number = 150 if(number % 5 === 0) console.log(`$number> is divisible by five`) >else console.log(`$number> is not divisible by 5`) > // Output: 150 is divisible by five 

2 likes Like Comment button

I’m a passionate Developer and Team Leader, I get excited by others successes. My downtime is filled with Social Dancing and Wilderness Backpacking.

2 likes Like Comment button

👨‍💻 Software Developer 📝 Tech Writer @LogRocket @get_livecycle 📹 Content Creator All things tech and programming 💻

Works like a charm I use this code often when trying to separate odd from even.

2 likes Like Comment button

For further actions, you may consider blocking this person and/or reporting abuse

Level Up Your AWS CDK Development: Demystifying integ-runner and integ-test

Patrón de diseño Adapter en JavaScript

Create a form on Tally

CodewithGuillaume — Jun 19

Mastering Object-Oriented Programming with TypeScript: Encapsulation, Abstraction, Inheritance, and Polymorphism Explained

More from Dhairya Shah

Once suspended, dhairyashah will not be able to comment or publish posts until their suspension is removed.

Once unsuspended, dhairyashah will be able to comment and publish posts again.

Once unpublished, all posts by dhairyashah will become hidden and only accessible to themselves.

If dhairyashah is not suspended, they can still re-publish their posts from their dashboard.

Once unpublished, this post will become invisible to the public and only accessible to Dhairya Shah.

They can still re-publish the post if they are not suspended.

Thanks for keeping DEV Community safe. Here is what you can do to flag dhairyashah:

dhairyashah consistently posts content that violates DEV Community’s code of conduct because it is harassing, offensive or spammy.

Unflagging dhairyashah will restore default visibility to their posts.

DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey.

Built on Forem — the open source software that powers DEV and other inclusive communities.

Made with love and Ruby on Rails. DEV Community © 2016 — 2023.

We’re a place where coders share, stay up-to-date and grow their careers.

Источник

Even numbers in javascript

Last updated: Dec 26, 2022
Reading time · 6 min

banner

# Find the Even or Odd Numbers in an Array in JavaScript

To find the even or odd numbers in an array:

  1. Use the Array.filter() method to iterate over the array.
  2. Check if each number has a remainder when divided by 2.
  3. The filter method will return a new array containing only the even numbers.
Copied!
// ✅ Find the EVEN numbers in an array const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = arr.filter(number => return number % 2 === 0; >); console.log(even); // 👉️ [2, 4, 6, 8]

The same approach can be used to find the odd numbers in the array.

Copied!
// ✅ Find the ODD numbers in an array const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const odds = arr.filter(number => return number % 2 !== 0; >); console.log(odds); // 👉️ [1, 3, 5, 7, 9]

The function we passed to the Array.filter method gets called with each element in the array.

On each iteration, we use the modulo (%) operator to check if the number doesn’t have a remainder when divided by 2 .

Copied!
console.log(8 % 2); // 👉️ 0 console.log(7 % 2); // 👉️ 1 console.log(6 % 2); // 👉️ 0 console.log(5 % 2); // 👉️ 1

If there is no remainder when the number is divided by 2 , it’s an even number.

Only even numbers satisfy the condition and get added to the new array.

Conversely, if there is a remainder when dividing by 2 , then the number is odd.

If you have to find the even numbers in an array often, define a reusable function.

Copied!
function findEvenNumbers(array) return array.filter(number => return number % 2 === 0; >); > const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = findEvenNumbers(arr); console.log(even); // 👉️ [ 2, 4, 6, 8 ]

The findEvenNumbers() function takes an array as a parameter and finds all even numbers in the array.

The function can be slightly updated to find the odd numbers in the array.

Copied!
function findOddNumbers(array) return array.filter(number => return number % 2 !== 0; >); > const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // 👇️ [ 1, 3, 5, 7, 9 ] console.log(findOddNumbers(arr));

The findOddNumbers() function takes an array as a parameter and finds all odd numbers in the array.

An alternative approach is to use the Array.forEach method.

# Find the Even or Odd Numbers in an Array using Array.forEach()

This is a four-step process:

  1. Declare a variable and initialize it to an empty array.
  2. Use the Array.forEach() method to iterate over the array.
  3. Check if each number has a remainder when divided by 2.
  4. If there is no remainder, push the number into the even numbers array.
Copied!
// ✅ Find the EVEN numbers in an array const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = []; arr.forEach(number => if (number % 2 === 0) even.push(number); > >); console.log(even); // 👉️ [2, 4, 6, 8]

The same approach can be used to find the odd numbers in the array.

Copied!
// ✅ Find the ODD numbers in an array const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const odds = []; arr.forEach(number => if (number % 2 !== 0) odds.push(number); > >); console.log(odds); // 👉️ [1, 3, 5, 7, 9]

The function we passed to the Array.forEach method gets called with each element in the array.

However, the forEach() method doesn’t return an array like filter() does.

On each iteration, we check if the current number doesn’t have a remainder when divided by 2 .

If the condition is met, we push the number into the even numbers array.

Once the forEach() method has iterated over the entire array, the even variable stores all even numbers from the original array.

If you have to do this often, define a reusable function.

Copied!
function findEvenNumbers(array) const even = []; array.forEach(number => if (number % 2 === 0) even.push(number); > >); return even; > const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = findEvenNumbers(arr); console.log(even); // 👉️ [ 2, 4, 6, 8 ]

The findEvenNumbers() function takes an array as a parameter and returns the even numbers in the array.

You can slightly tweak the function to find all odd numbers in the array.

Copied!
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; function findOddNumbers(array) const onlyOdd = []; array.forEach(number => if (number % 2 !== 0) onlyOdd.push(number); > >); return onlyOdd; > const result = findOddNumbers(arr); // 👇️ [ 1, 3, 5, 7, 9 ] console.log(result);

The findOddNumbers() function takes an array as a parameter and returns the odd numbers in the array.

You can also use a for. of loop to find the even numbers in an array.

# Find the Even or Odd Numbers in an Array using for. of loop

This is a three-step process:

  1. Use a for. of loop to iterate over the array.
  2. Check if each number has a remainder when divided by 2 .
  3. If the condition is met, push the number into a new array.
Copied!
// ✅ Find the EVEN numbers in an array const even = []; const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; for (const number of arr) if (number % 2 === 0) even.push(number); > > // 👇️ [ 2, 4, 6, 8 ] console.log(even);

The same approach can be used to find the odd numbers in the array.

Copied!
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const oddNumbers = []; for (const number of arr) if (number % 2 !== 0) oddNumbers.push(number); > > // 👇️ [ 1, 3, 5, 7, 9 ] console.log(oddNumbers);

The for. of statement is used to loop over iterable objects like arrays, strings, Map , Set and NodeList objects and generators .

On each iteration, we check if the current number doesn’t have a remainder when divided by 2 .

Copied!
console.log(4 % 2); // 👉️ 0 console.log(3 % 2); // 👉️ 1 console.log(2 % 2); // 👉️ 0 console.log(1 % 2); // 👉️ 1

If the number is even, we push it into the even array.

You can also extract the logic into a reusable function.

Copied!
function findEvenNumbers(array) const onlyEven = []; for (const number of array) if (number % 2 === 0) onlyEven.push(number); > > return onlyEven; > const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = findEvenNumbers(arr); console.log(even); // 👉️ [ 2, 4, 6, 8 ]

The function takes an array as a parameter and returns a new array containing only the even numbers of the original array.

The function can be slightly tweaked to return the odd numbers in the array.

Copied!
function findOddNumbers(array) const oddNumbers = []; for (const number of array) if (number % 2 !== 0) oddNumbers.push(number); > > return oddNumbers; > const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const odd = findOddNumbers(arr); // 👇️ [ 1, 3, 5, 7, 9 ] console.log(odd);

The function takes an array as a parameter and returns a new array containing only the odd numbers of the original array.

You can also use a basic for loop to find the even numbers in an array.

# Find the even or odd numbers in an Array using a for loop

This is a three-step process:

  1. Use a for loop to iterate over the array.
  2. Access each item using the index.
  3. If the number is even, push it into a new array.
Copied!
// ✅ find the EVEN numbers in an array const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = []; console.log(even); // 👉️ [ 2, 4, 6, 8 ] for (let index = 0; index arr.length; index++) if (arr[index] % 2 === 0) even.push(arr[index]); > > // 👇️ [ 2, 4, 6, 8 ] console.log(even);

We used a basic for loop to iterate over the array.

On each iteration, we access the array element at the current index and check if it is an even number.

If the condition is met, we push the element into the new array.

The same approach can be used to find the odd numbers in the array.

Copied!
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const odd = []; for (let index = 0; index arr.length; index++) if (arr[index] % 2 !== 0) odd.push(arr[index]); > > // 👇️ [ 1, 3, 5, 7, 9 ] console.log(odd);

Which approach you pick is a matter of personal preference. I’d use the Array.forEach() method as I find it quite direct and easy to read.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Оцените статью