bihire boris
bihire boris

Reputation: 1662

Trying to check if an array has an empty elements or not a number

trying to check for an empty string or an element that is not a number when run a Number() over it

const latLong = ['1', 'g']

function findError(obj) {
    // console.log(Number(obj))

     return obj === '' || Number(obj) === NaN
    }
console.log(latLong.find(findError))

It seems to be catching the empty string but not the Number() part, any help will be appreciated

Upvotes: 1

Views: 52

Answers (2)

Ritu
Ritu

Reputation: 732

let latLong = ['1', 'g'];
latLong = latLong.filter(el => {
  return el === '' || isNaN(el);
});
  • isNaN() is a method in javascript which is used to determine whether a particular value is a number or not.
  • Above code returns if any element in the array is empty or Not a Number.

Upvotes: 0

Saransh Kataria
Saransh Kataria

Reputation: 1497

For checking Nan, you can use the inbuilt isNaN() function. Check the documentation here .

Upvotes: 1

Related Questions