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

200
Visualizações
Looking for a function that takes three arguments (x, y, z) and checks if the type of all three is a string

Don't know what to put before the for loop. Don't know if I need an if/else statement. Trying to have it display in the console if items in an array are strings. So I know I need consol.log

var stringOne = isString('rob','bob','carl')

function isString() {
//I dont know what to put before for loop

    for(let i = 0; i < arguments.length; i++) {
        // Dont know if i need an if/else statement
        // Trying to have it display in the console if items in an array are strings
        // So I know I need consol.log
    }

}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

every would be appropriate here, and doing I/O (like console.log) is better left outside of the function. The name of the function suggests that it should return a boolean (true/false):

function isString(...args) {
    return args.every(s => typeof s === "string");
}

console.log(isString('rob','bob','carl'));

about 4 years ago · Juan Pablo Isaza Relatório

0

Because it seems like you're a beginner, I will expand upon the code that you currently have, although @trincot did the best solution.

In a for loop, you can return a value so the loop won't continue. Because you only need to check if any of them are false, may it be in position 0, 1 or 2 in the array, you can return "false" immediately.

If there are only strings, the loop will continue until it ends, and then return "true" at the end of the method.

So you don't need any code before the for loop, only an if statement that returns "false" if any of the items in the array isn't a string.

var stringOne = isString('rob','bob','carl')
var stringTwo = isString('rob','bob', 1)

function isString() {
    for(let i = 0; i < arguments.length; i++) {
      if (typeof arguments[i] != 'string') {
        return false
      }
    }

  return true
}

console.log({stringOne});
console.log({stringTwo});

about 4 years ago · Juan Pablo Isaza Relatório

0

Something like this in the loop ought to do it:

this_arg = arguments[i];
if (typeof this_arg === 'string') console.log("arg number " + i + " is a string");

It shows how to do it a bit here

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