JavaScript anagram comparison

前端 未结 16 2114
走了就别回头了
走了就别回头了 2020-12-17 02:30

I\'m trying to compare two strings to see if they are anagrams.

My problem is that I\'m only comparing the first letter in each string. For example, \"Mary\" and \"

16条回答
  •  死守一世寂寞
    2020-12-17 03:07

    First of all, you can do the length check before the for loop, no need to do it for each character...

    Also, "break" breaks the whole for loop. If you use "continue" instead of "break", it skips the current step. That is why only the first letters are compared, after the first one it quits the for loop.

    I hope this helps you.

提交回复
热议问题