JavaScript Anagram checker I have written a code that finds whether the entered words are anagrams or not. I tested and it works, but I know that there are some details that I must change or modify. So what do you think I have to change in this code?

function findAnagram (firstWord, secondWord) {
var testWord = "";
var c = 0;
while (c &lt; secondWord.length || c &lt; firstWord.length) {
    while (secondWord[c] == " " || firstWord[c] == " ") {
    secondWord = secondWord.replace(" ", "");
    firstWord = firstWord.replace(" ", "");
}
c++;
}

if (firstWord.length == secondWord.length) {
    for (var i = 0; i &lt; firstWord.length; i++) {
    for (var k = 0; k &lt; secondWord.length; k++) {
        if (firstWord[i] == secondWord[k]) {
            testWord += firstWord[i];
            secondWord = secondWord.replace(secondWord[k], "")
            break;   
        }
    }
}
}
if (firstWord == testWord){
    return "Anagram !"; 
}
else {
    return "Not an Anagram";
}
}
console.log(findAnagram("funeral", "real fun"));
Your function has a high complexity because of the 2 nested loops.

Here is my implementation. I'm not sure if it's more efficient, but I think it's more readable.

function findAndAppendCharacter(countArray, currentChar) {
    let foundAt = -1

    countArray.forEach((pair, i) =&gt; {
        if(pair.letter == currentChar) {
            foundAt = i
        }
    })


    if(foundAt == -1) {
        countArray.push({
            letter: currentChar,
            count: 1
        })

        return
    }

    countArray[foundAt].count++
}

function findAnagram(firstWord, secondWord) {
    firstWord = firstWord.replace(" ", "")
    secondWord = secondWord.replace(" ", "")

    if(firstWord.length != secondWord.length) {
        return "Not an anagram!"
    }

    let letterCountArray = {
        first: [],
        second: []
    }

    for(let i = 0; i &lt; firstWord.length; i++) {
        findAndAppendCharacter(letterCountArray.first, firstWord.charAt(i).toLowerCase())
        findAndAppendCharacter(letterCountArray.second, secondWord.charAt(i).toLowerCase())
    }


    if(letterCountArray.first.length != letterCountArray.second.length) {
        return "Not an anagram!"
    }

    for(let i = 0; i &lt; letterCountArray.first.length; i++) {
        let firstPair = letterCountArray.first[i]
        let secondPair = letterCountArray.second[i]

        if(letterCountArray.first.filter(pair =&gt; pair.letter == secondPair.letter &amp;&amp; pair.count == secondPair.count).length != 1) {
            return "Not an anagram!"
        }

        if(letterCountArray.second.filter(pair =&gt; pair.letter == firstPair.letter &amp;&amp; pair.count == firstPair.count).length != 1) {
            return "Not an anagram!"
        }
    }

    return "Anagram!"
}


I'm also curious about a better solution :)