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

193
Visualizações
Cómo resolver este problema de matrices anidadas js: dada una palabra y una matriz de esqueletos de cadenas, devuelve una matriz de posibles coincidencias de palabra esqueleto

después de trabajar en esto durante muchas horas, no sé cómo resolver el siguiente problema. Ofrezca soluciones con explicaciones o sugerencias para mejorar mi solución.

 /* instructions Given a word and an array of string skeletons, return an array of all of the skeletons that could be turned into the word by replacing the '-' with letters. If there are no possible matches return an empty string example: given the word 'hello' 'he---' would be a match, 'h--l' or 'g-llo' would not be a match */ // example test case: let word = 'hello'; let skeletons = ['h--l', 'he---', 'g-llo', 'm-llo', '--llo', 'hl--']; function findSkels(word, skeleton){ let goodSkels = []; skeleton = skeletons.filter(w => w.length === word.length); console.log(skeletons) for(let sw = 0; sw < skeletons.length; sw++){ let possibleMatch = true; for(let letter = 0; letter < word.length; letter++){ if(word[letter] !== skeletons[sw][letter] || skeletons[sw][letter] == '-'){ possibleMatch = false } } if(possibleMatch){ goodSkels.push(skeletons[sw]) } } return goodSkels; }

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

0

Estás cerca, pero

 if (word[letter] !== skeletons[sw][letter] || skeletons[sw][letter] == '-') { possibleMatch = false }

descalificará un esqueleto si alguna letra no coincide o si la letra es - . Así que esto descalificará cualquier palabra que no sea una coincidencia exacta. En su lugar, desea && : descalificar solo si la letra no coincide y la letra no es un - .

 /* instructions Given a word and an array of string skeletons, return an array of all of the skeletons that could be turned into the word by replacing the '-' with letters. If there are no possible matches return an empty string example: given the word 'hello' 'he---' would be a match, 'h--l' or 'g-llo' would not be a match */ // example test case: let word = 'hello'; let skeletons = ['h--l', 'he---', 'g-llo', 'm-llo', '--llo', 'hl--']; function findSkels(word, skeleton) { let goodSkels = []; skeleton = skeletons.filter(w => w.length === word.length); for (let sw = 0; sw < skeletons.length; sw++) { let possibleMatch = true; for (let letter = 0; letter < word.length; letter++) { if (word[letter] !== skeletons[sw][letter] && skeletons[sw][letter] !== '-') { possibleMatch = false } } if (possibleMatch) { goodSkels.push(skeletons[sw]) } } return goodSkels; } console.log(findSkels(word, skeletons));

O, refactorizado para verse mejor:

 const word = 'hello'; const skeletons = ['h--l', 'he---', 'g-llo', 'm-llo', '--llo', 'hl--']; const findSkels = (word, skeletons) => skeletons .filter(skel => ( skel.length === word.length && [...skel].every((char, i) => char === '-' || char === word[i]) )); console.log(findSkels(word, skeletons));

Los métodos de matriz son a menudo una buena manera de hacer que el código sea mucho más limpio que los bucles for imperativos basados en índices. Si en realidad no le importa el índice, solo el valor que se itera, a menudo puede deshacerse del for (let i = construya por completo, y use un método de matriz o for..of.

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