Created
April 30, 2025 17:19
-
-
Save tatsuyax25/4b7b51c06da3dbd58d7b85d54f40078c to your computer and use it in GitHub Desktop.
Given an array nums of integers, return how many of them contain an even number of digits.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @return {number} | |
*/ | |
var findNumbers = function(nums) { | |
let count = 0; // Initialize a counter for numbers with an even number of digits | |
for (let num of nums) { // Loop through each number in the array | |
// Convert the number to a string (absolute value to handle negatives) and get its length | |
let digitCount = Math.abs(num).toString().length; | |
// If the length is even, increase the counter | |
if (digitCount % 2 === 0) { | |
count++; | |
} | |
} | |
return count; // Return the total count of numbers with an even number of digits | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment