

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding even length numbers from an array in JavaScript
We are required to write a JavaScript function that takes in an array of Integers as the first and the only argument. The function should then construct and return a new array that contains only those elements from the original array that contains an even number of digits.
For example −
If the input array is −
const arr = [12, 6, 123, 3457, 234, 2];
Then the output should be −
const output = [12, 3457];
Example
The code for this will be −
const arr = [12, 6, 123, 3457, 234, 2]; const findEvenDigitsNumber = (arr = []) => { const res = []; const { length: l } = arr; for(let i = 0; i < l; i++){ const num = Math.abs(arr[i]); const numStr = String(num); if(numStr.length % 2 === 0){ res.push(arr[i]); }; }; return res; }; console.log(findEvenDigitsNumber(arr));
Output
And the output in the console will be −
[12, 3457]
- Related Questions & Answers
- Returning an array containing last n even numbers from input array in JavaScript
- Repeating only even numbers inside an array in JavaScript
- How to pull even numbers from an array in MongoDB?
- Finding matching pair from an array in JavaScript
- Finding missing element in an array of numbers in JavaScript
- Finding all possible combinations from an array in JavaScript
- Finding the nth missing number from an array JavaScript
- Finding all the longest strings from an array in JavaScript
- Finding all duplicate numbers in an array with multiple duplicates in JavaScript
- Odd even sort in an array - JavaScript
- C++ code to decrease even numbers in an array
- Finding desired numbers in a sorted array in JavaScript
- Finding unlike number in an array - JavaScript
- Finding tidy numbers - JavaScript
- Remove duplicates from an array keeping its length same in JavaScript
Advertisements