Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
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”
    }
  }
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const sampleErrorArr = ['zero', 1, 2, 3, 4,]
const sampleArr = [1, 2, 3, 4, 5]

function sumAll(arr) {
    let sum = 0
    let hasNumber = true
    arr.forEach((item, index) => {
        if (typeof item === 'number') {
            let temp = sum + item
            sum = temp
        }
        else {
            hasNumber = false
        }
    })

    return hasNumber == true ? sum : "Error"
}

console.log(sumAll(sampleErrorArr)) //Error
console.log(sumAll(sampleArr)) // 15
about 4 years ago · Juan Pablo Isaza Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório

0

let foundString = arrayWithPossiblyString.find(i=>isNaN(5-i));

Explanation:

  1. 5-"a" is a NaN.
  2. isNaN function can be used to chec if something is NaN
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda