Need some insight with Mutations Challenge
function existsMoreThanOnce(array, element) {
var counter = 0;
for (var i = 0; i < array.length; i++) {
if (element === array[i])
++counter;
}
return counter;
}
function mutation(arr) {
var mainWord = arr[0].toLowerCase().split("");
var secondaryWord = arr[1].toLowerCase().split("");
var matchedLetters = [];
var counter = 0;
// Loop through main word
for (var i = 0; i < mainWord.length; i++) {
// Loop through second array
for (var j = 0; j < secondaryWord.length; j++) {
// If element of the mainWord matches the element of the second array
if (mainWord[i] === secondaryWord[j]) {
// If the element is not in matchedLetters array
if (matchedLetters.indexOf(mainWord[i]) === -1) {
// If the matched element exists more than once in the mainWord array
if (existsMoreThanOnce(mainWord, mainWord[i]) > 1 && existsMoreThanOnce(secondaryWord, secondaryWord[j]) > 1) {
// Then push that element existed times
for (var k = 0; k < existsMoreThanOnce(mainWord, mainWord[i]); k++) {
matchedLetters.push(mainWord[i]);
++counter;
}
} else {
matchedLetters.push(mainWord[i]);
++counter;
}
}
}
}
}
if (counter == secondaryWord.length)
return true;
// Else
return false;
}
mutation(["hello", "hey"]);