How to count the numbers with even number of digits in an array with Node.js

1 Answer

0 votes
function count_even_numbers(arr) {
    let result = 0;
    let size = arr.length;
    
    for (let i = 0; i < size; i++) {
        let total_digits = Math.floor((Math.log10(arr[i]))) + 1;
        if (total_digits % 2 == 0) {
            result++;
        }
    }
    
    return result;
}

const array = [564, 90, 7, 1248, 47325, 834921, 1234567];

console.log(count_even_numbers(array));




/*
run:

3

*/

 



answered Jul 21, 2023 by avibootz
edited Jul 21, 2023 by avibootz

Related questions

...