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

90
Views
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 answers
Answer question

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