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

206
Views
find occurrences of char case insensitive code wars

Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contain any char.

Someone posted their answer is like this but I don't understand the part x && x.length. Can someone explain this?

function XO(str) {
  let x = str.match(/x/gi);
  let o = str.match(/o/gi);
  return (x && x.length) === (o && o.length);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

str.match(/x/gi) returns an array of matches if there are any matches. But if there aren't any matches, it returns null. You can't get the length of null, so x && x.length makes use of short-circuiting to prevent that error. It will be null if x === null, otherwise it will be the lenth of the array.

The same goes for o && o.length. So if both return matches, it compares the number of matches. If both have zero matches, it turns into null === null, which is true. And if only one of them has matches, null will not be equal to the length of the matches.

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!