Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi! I'm writing a function that counts all even numbers of an array, and it works pretty well, but, I don't know how to avoid counting empty strings or empty arrays as O while counting the number 0 as even. Here is my code:

function is_an_even_number(array){
	var count = 0;
	for(let i = 0 ; i < array.length; i++) {
		if (array[i] % 2 === 0){
			count++;
		}
	}
	return count;
}


Also if you could help me improve the code, I believe there has to be a shorterand more efficient way to write this. I'll be really thankful.

This is how it should behave:

//======================  EXAMPLE  ========================
is_an_even_number([1,5,9,33,65,[],'',0,66,['banana']])
2 // <======  EXPECTED OUTPUT
is_an_even_number(["100", 33, "Hello"])
1 // <======  EXPECTED OUTPUT
//=========================================================


And this is how it's working:

//======================  EXAMPLE  ========================
is_an_even_number([1,5,9,33,65,[],'',0,66,['banana']])
4 // <======  EXPECTED OUTPUT
is_an_even_number(["100", 33, "Hello"])
1 // <======  EXPECTED OUTPUT
//=========================================================



//Solved! I'm just excluding empty strings and arrays with the && comparison:
function is_an_even_number(array){
    var count = 0;
    for(let i = 0 ; i < array.length; i++) {
        if (array[i] % 2 === 0 && array[i] != "" && array[i] != []){
            count++;
        }
    }
    return count;
}


What I have tried:

function is_an_even_number(array){
	var count = 0;
	for(let i = 0 ; i < array.length; i++) {
		if (array[i] % 2 === 0){
			count++;
		}
	}
	return count;
}



//Solved! I'm just excluding empty strings and arrays with the && comparison:
function is_an_even_number(array){
    var count = 0;
    for(let i = 0 ; i < array.length; i++) {
        if (array[i] % 2 === 0 && array[i] != "" && array[i] != []){
            count++;
        }
    }
    return count;
}
Posted
Updated 17-Aug-21 3:16am
v2
Comments
CHill60 17-Aug-21 8:09am    
check to see if each element is a number first - see the isNaN() function
gabriel19971029 17-Aug-21 9:06am    
Thanks a lot! I tried this but I believe is not properly writen, because always returns 0.
function is_an_even_number(array){
var count = 0;
for(let i = 0 ; i < array.length; i++) {
if (array[i] % 2 === 0 && array[i] === !isNaN){
count++;
}
}
return count;
}
gabriel19971029 17-Aug-21 9:13am    
Nevermind I got it now. Thanks!

I would suggest you go to JavaScript Tutorial[^] and work through the tutorials.
 
Share this answer
 
If you only needed to test for actual numbers, this would be simple.

However, things are complicated by the fact that you want to treat a string containing the decimal representation of a number as the number it represents.

I'd suggest creating a separate function to test for even numbers, or string representations of even numbers:
JavaScript
function isAnEvenNumber(value){
    if (typeof value === "number" || value instanceof Number) {
        return value % 2 === 0;
    }
    
    if (typeof value === "string" || value instanceof String) {
        const number = Number(value);
        return !isNaN(number) && number % 2 === 0;
    }
    
    return false;
}
NB: Don't use parseInt or parseFloat to try to convert the string to a number; both functions ignore trailing non-numeric characters. For example, parseInt("221b") will return 221, whereas Number("221b") will return NaN.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900