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

112
Visualizações
Iterating over array to check whether strings ```includes``` another string

using the includesmethod to check whether strings in an array include another string. Basically like a search function type of thing.

Heres what I have so far

this.findIngredient = function(ingredientName) {
          for (i=0; i < this.ingredients.length; i++) {
               if (this.ingredients[i].includes(ingredientName)) {
                     return true
                     break
                 } else {
                    return false
                   }
                }
            }

what im unsure about is why when the this.ingredient does hold a string that includes ingredientName it still outputs false.

For context this is the this.ingredient array

this.ingredients = ["500g plain flour", "1 tsp fine salt", "2 heaped tsp baking powder"]
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Removing the else block from within your loop should resolve the issue.

Explanation -

The else block is run when this.ingredients[i].includes(ingredientName) is false. And in your code, when the else block returns false which means your loop will not have the opportunity to check every element in the ingredients array since the function will stop execution when it sees a return statement.

about 4 years ago · Santiago Trujillo 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