1

I'm trying to make a challenge, but is not just about the challenge it self but is more about the fact the I would like to understand how to do it.

Given an anagram (string): abba

and an array of words: ['aabb', 'abcd', 'bbaa', 'dada']

To find which one is an anagram and which not.

So I thought I can simply take the string (abba) and with includes and map throught it and just splice the false ones.

  const res = word.includes(
    words.map(item =>
      item.split('').map(x => {
        return x
      })
    )
  );
  console.log('res', res);

But I always get one false cause map just returns the array (i.e.) 'abcd' and not (i.e.) 'a' and the 'b' and so on... so that makes impossible to includes() to actually look into the string.

How can I solve it??

Where I'm mistaking???

Thanks!

user3297291
  • 22,592
  • 4
  • 29
  • 45
Marco Disco
  • 527
  • 7
  • 22
  • Your code is not syntactically correct. Please click `[<>]` snippet editor and provide a [mcve] with correctly spelled code – mplungjan Jan 11 '21 at 12:25
  • `.map()` always returns an array and `.include()` always expects a string. There is no way to use them together like you tried. You can probably do a `.map()` with `.include()` as part of the callback. However, `.map()` isn't the correct tool here - it's useful if you want to transform an array into another array. Since you want to just produce a single value, you can use `.some()`/`.every()`. `.reduce()`/`.reduceRight()` are also an option but the specialised methods are usually better when applicable. – VLAZ Jan 11 '21 at 12:29
  • thanks a lot. I also checked the solution but I would like to try in another way. I'll defenitely try other methods – Marco Disco Jan 12 '21 at 08:40

0 Answers0