Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!