- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetch Numbers with Even Number of Digits JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the only argument.
The function should pick all those elements from the array that contains an even number of digits and return them in a new array.
For example −
If the input array is −
const arr = [34, 23, 112, 8, 3456, 345];
Then the output should be −
const output = [34, 23, 3456];
Example
const arr = [34, 23, 112, 8, 3456, 345]; const countDigits = (num, sum = 0) => { if(num){ return countDigits(Math.floor(num / 10), sum + 1); }; return sum; }; const isEven = num => num % 2 === 0; const returnEvens = (arr = []) => { const res = arr.filter(el => isEven(countDigits(el))); return res; }; console.log(returnEvens(arr));
Output
And the output in the console will be −
[34, 23, 3456]
- Related Articles
- Find Numbers with Even Number of Digits in Python
- Count Numbers with N digits which consists of even number of 0's in C++
- Find the Number With Even Sum of Digits using C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Sum of individual even and odd digits in a string number using JavaScript
- Total number of non-decreasing numbers with n digits
- Average of even numbers till a given even number?
- Fetch alternative even values from a JavaScript array?
- Returning number with increasing digits. in JavaScript
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- C++ code to count number of lucky numbers with k digits
- Number of even substrings in a string of digits in C++
- MySQL REGEXP to fetch string + number records beginning with specific numbers?
- Just smaller number with monotone digits in JavaScript
- Counting n digit Numbers with all unique digits in JavaScript

Advertisements