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

208
Views
If else not working as intended, is there something wrong with my logic?

I'm trying to loop through an array and if there are any matching elements, it should push true to a new array else return false.

const wordPerformance = []
const wordsReviewed   = "candy, cattle, cat, call, cheat";
const wordsIncorrect  = "candy, cattle, call, cheat";

wordsReviewed.split(/,\s?/).forEach((word) => {
  if (wordsIncorrect.includes(word)) {
    wordPerformance.push(false);
  } else {
    console.log(word) //unreachable, though 'cat' should be logged
    wordPerformance.push(true);
  }
});

console.log(wordPerformance);

By this logic, wordPerformance should return

[false, false, true, false, false]

however, it is returning

[false, false, false, false, false]

Maybe there is something I'm not seeing?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You have to first split the wordsIncorrect string the same way you did it with the wordsReviewed so it does compare whith the item and not include strings which have something at the end like matching "cat" with "cattle"

This is the fixed example

const wordPerformance = []
const wordsReviewed = "candy, cattle, cat, call, cheat";
const wordsIncorrect = "candy, cattle, call, cheat";
const wordsIncorrectSplitted = wordsIncorrect.split(/,\s?/);

wordsReviewed.split(/,\s?/).forEach((word) => {
  if (wordsIncorrectSplitted.includes(word)) {
    wordPerformance.push(false);
    } else {
    console.log(word) //unreachable, though 'cat' should be logged
    wordPerformance.push(true);
  }
});

console.log(wordPerformance);

enter image description here

about 4 years ago · Juan Pablo Isaza Report

0

You are forgetting to split your incorrectWords string into an array as well. Right now, you are checking if the string includes the word instead of as an array. As a result, String.includes carries out pattern matching across the whole string rather than individual elements. Since every incorrect word includes the pattern cat in them, it will think cat is included and return false:

const wordPerformance = []
const wordsReviewed   = "candy, cattle, cat, call, cheat";
const wordsIncorrect  = "candy, cattle, call, cheat";

wordsReviewed.split(/,\s?/).forEach((word) => {
  if (wordsIncorrect.split(/,\s?/).includes(word)) { //split incorrectWords
    wordPerformance.push(false);
   } else {
     console.log(word) //unreachable, though 'cat' should be logged
     wordPerformance.push(true);
   }
 });

console.log(wordPerformance);
about 4 years ago · Juan Pablo Isaza Report

0

You need to split wordsIncorrect as well. if (wordsIncorrect.split(",").includes(word))

Output:

cat
[ false, false, true, false, false ]

about 4 years ago · Juan Pablo Isaza 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!