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

207
Visualizações
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 Respostas
Responde à pergunta

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 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