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

214
Visualizações
Finding specific string is odd or even from an array JavaScript

I am trying to find an odd string from an given array.

Code :

const friendArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];

function oddFriend(friendArray) {
  for (let i = 0; i < friendArray.length; i++) {
    if (friendArray[i].length % 2 != 0) {
      return friendArray[i];
    }
  }
}
const myFriend = oddFriend(friendArray);
console.log(myFriend);

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can apply this:

const friendArray = ["agdum","bagdum","chagdum","lagdum","jagdum","magdum",];

  function oddFriend(friendArray) {
    if (friendArray.length % 2 != 0) {
      return friendArray;
    }
  }
  const myFriend = friendArray.filter(oddFriend);
  console.log(myFriend);
about 4 years ago · Juan Pablo Isaza Relatório

0

.flatMap() and a ternary callback makes a simple and readable filter. It's not entirely clear if you wanted only odd or odd and even so there's both versions.

Odd length strings

const fArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];

let odd = fArray.flatMap(o => o.length % 2 === 1 ? [o] : []);

console.log(odd);

Odd & even length strings

const fArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];

let oe = [[], []];

fArray.forEach(o => o.length % 2 === 1 ? oe[0].push(o) : oe[1].push(o));

console.log(oe);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.filter() method which will create a new array with all odd friends that pass the test implemented by the provided function.

// Input array
const friendArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];

// Filtered result
console.log(friendArray.filter((friend) => friend.length % 2 !== 0));

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