15,302,455 members

See more:

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:

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:

And this is how it's working:

**//Solved! I'm just excluding empty strings and arrays with the && comparison:**

**What I have tried:**

**//Solved! I'm just excluding empty strings and arrays with the && comparison:**

Copy 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:

Copy Code

//====================== 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:

Copy Code

//====================== 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 //=========================================================

Copy Code

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; }

Copy 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; }

Copy Code

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; }

Comments

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:

**NB:** Don't use

However, things are complicated by the fact that you want to treat

I'd suggest creating a separate function to test for even numbers, or string representations of even numbers:

JavaScript

Copy Code

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; }

`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`

.
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

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;

}