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

184
Visualizações
Very large string arrays not working as expected

I have an array with 58112 words. However, when I try to check if a word is in the list, it always returns false, except for the first word. I'm not going to post my code because that would be too large, but here is some of the main stuff:

isWord("a") //true
isWord("hello") //false??

function isWord(word) {
  word = word.toLowerCase();
  for (let i = 0; i < words.length; i++) {
    if (word == words[i]) {
      return true;
    } else {
      return false;
    }
  }
}

words[] is the list of 58112 words. The first word is "a", of course. When I do isWord("a"), it returns true, like expected. If I do anything other than "a", It returns false. Why does this occur? Is it because I exceeded the maximum array limit? I don't think so.
The words I used are from this (I had to add the "a" and the "i" because they didn't have one).

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

0

I noticed you made it

return false;

That would make it so that anything after the return statement does not execute (and as a result your code stops executing after one iteration). I would recommend replacing it with a print statement or storing the result in a Boolean variable and printing it elsewhere, OR maybe returning the Boolean variable so you don't have to change a lot of the other code. That can be done this way:

isWord("a") //true
isWord("hello") //false??

function isWord(word) {
  var boolean check = false;   //setting it to false by default removes the need for the "else" statement
  word = word.toLowerCase();
  for (let i = 0; i < words.length; i++) {
    if (word == words[i]) {
      check = true;
    }
  }
  return check;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I may be wrong, but I think this is because when you are checking whether the word is in the words array or not.

In the first iteration of the array if it finds that the word that you entered is a which is the value of words[0], it returns true. Otherwise, if it it returns false and exits out of the function.

So essentially it only checks whether the element you entered is equal to a or not.

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is a faster and more concise way to achieve the same functionality.

function isWord(word) {
    word = word.toLowerCase()
    var i = word.length;
    while (i--) {
       if (words[i] === word) {
           return true;
       }
    }
    return false;
}
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