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

91
Visualizações
Sparse Array HackerRank Exercise

I'm trying to resolve some exercises from hacker rank, and I'm struggling with the array map and filter methods.

This function supposes to search a value from queries array into strings array and when I use the map method it returns an array with undefined values, and when I use the filter method it returns an empty array.

var strings = ['ab','ab','bca','acb']
var queries = ['ab','abc','bc']

function matchingStrings(strings, queries) {
let retArr = []
for(let n = 0; n<queries.length; n++){
       var equalelem = strings.filter(function(queries){queries[n] === [...strings]})
       retArr.push(equalelem.length);
    }
    return equalelem 
}

console.log(matchingStrings(strings, queries))
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Looking only at your syntax, and without analyzing the algorithm, the following things should change:

  • Your filter function always returns undefined. Any function that doesn't have an explicit return statement returns undefined. Maybe you wanted to use an arrow function expression, which doesn't require a return statement if you omit the curly braces?

    var equalelem = strings.filter((queries) => queries[n] === [...strings])
    
  • Inside the filter function, you seem to be comparing an element from list queries to a copy of list strings. Comparing an element of a list (in this case, a single string) to a list will never be true in your case.

  • The variable letArr doesn't seem to serve any purpose.

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