I am making a program in JavaScript that reads two words from the user and determines if the two are anagrams. My objects hold keys which are the letters of the word, and the values which is the amount of times it appears in the word.
Here is how I am filling my objects at runtime:
for(var i = 0; i<word1.length; i++){
const curLetter = word1[i];
if(!dictWord1[curLetter]){
dictWord1[curLetter]=1;
}
else
{
dictWord1[curLetter] += 1;
}
}
for(var i = 0; i<word2.length; i++){
const curLetter = word2[i];
if(!dictWord2[curLetter]){
dictWord2[curLetter]=1;
}
else
{
dictWord2[curLetter] += 1;
}
}
Here is how they look when I console.log() them (I am using the words evil and live for this example)
I know how to loop through an object and print the keys and values, but is there a way in JavaScript I can compare the two? The way I am determining if they are anagrams is that I am seeing if the keys are the same and the values are the same in each object. Thanks in advance!