Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you check for a type in an array?

I need to tell the program to search an array(that’s meant to be only numbers) if it contains any element that’s a string.

Also, the array is made up of the arguments to a function. Can someone please help? I’ve been trying to figure this out for at least an hour! This is what i did so far:

const sumAll = function(…args){
  const newArray = Array.from(args)
  for(let i = 0; i < newArray.length; i++){
    if(newArray[i] === NaN){
    return “ERROR”
    }
  }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re looking for the function isNaN

const sumAll = function(...args){
  const newArray = Array.from(args)
  for(let i = 0; i < newArray.length; i++){
    if(isNaN(newArray[i])){
      return "ERROR"
    }
  }
}

console.log(sumAll(1,2,3)) // no output - undefined

console.log(sumAll(1,"two",3)) // error
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading