Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created April 30, 2025 17:19
Show Gist options
  • Save tatsuyax25/4b7b51c06da3dbd58d7b85d54f40078c to your computer and use it in GitHub Desktop.
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.
/**
* @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