Find Numbers with Even Number of Digits
In this lab, you are required to write a function findNumbers
that takes an array of integers, nums
, as input and returns the count of numbers that have an even number of digits.
You can use the following examples and explanations to understand the problem and write your solution:
Example 1:
Input: nums = [12,345,2,6,7896]
Output: 2
Explanation:
12 contains 2 digits (even number of digits).
345 contains 3 digits (odd number of digits).
2 contains 1 digit (odd number of digits).
6 contains 1 digit (odd number of digits).
7896 contains 4 digits (even number of digits).
Therefore only 12 and 7896 contain an even number of digits.
Example 2:
Input: nums = [555,901,482,1771]
Output: 1
Explanation:
Only 1771 contains an even number of digits.
Challenge 1:
Export your findNumbers
function properly.
Challenge 2:
Implement the findNumbers
function with correct functionality. Example solutions are provided below, but it is encouraged to think and come up with your own solution.
Example solution 1: const findNumbers = nums => nums.filter(num => String(num).length % 2 === 0).length;
Example solution 2: function findNumbers(nums) { return nums.reduce((count, num) => count + (num.toString().length % 2 === 0 ? 1 : 0), 0); }
Remember to use ESM import/export everywhere and follow the rules mentioned in the initial instructions while building this lab. Good luck!