# Mutations(Help Question)

I have no idea how to specify the index in the if statement. The hint says use a loop but the loop goes through the whole array, but I’m trying to compare the broken down characters. After the split, my array has every character in the two words split. Each character is assigned an index but now I have to differentiate the two words which fall in the same order of indexing? The comparison is the problem here and I don’t need to join the words after either I’m assuming

function mutation(arr) {
let newArr=arr.toString()
let newArr1=newArr.toLowerCase()
let newArr2=newArr1.split("")
for (let i=0;i<newArr2.length;i++) {
if (newArr2.indexOf(1)==newArr2[i]) {
return true;
} else {
return false
}
}
return newArr2
}

console.log(mutation(["hello", "hey"]));

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36.

Challenge: Mutations

Can you walk us through your algorithm that you are trying to recreate with JavaScript here?

Once you an explain your algorithm in the basic steps and logic you are going for, we can help guide you to a solution.

In the mean time, I will explain what your existing function is doing. Basically, you are always returning false regardless what is passed to the function.

You should add some console.log statements to see what values you are creating at various points in your function. For example, I suggest adding:

console.log(newArr2)

after the following line:

let newArr2=newArr1.split("")

I think you might be surprised by what you see.

1 Like

In the first line I take the given array and turn it into a string, the second line makes the string all lowercase. The third line splits all the letters in the array into their own characters in “”, like ["hello", "hey"] is now [ 'h', 'e', 'l', 'l', 'o', ',', 'h', 'e', 'y' ]. I then create a loop, in this loop I want it to iterate over the first word and check to see if the second word has all the letters of the first word. The if statement is supposed to hold that comparison and return either true if it passes the test or false if the second word does not contain all the words the first word has. Where I am confused is how to use indexOf to specify the location of the first word, because newArr2.indexOf(0) is just "h" not 'h', 'e', 'l', 'l', 'o',. And newArr2[i] checks the whole array, so I would be testing the first word against the whole array of characters? Do I even need the loop?

The problem requires you to check if all the letters in the second string exists in the first string. I believe you would be better off not merging the two strings. This is how I solved it (algorithm):

1. Get array of two strings.
2. Copy strings into two local variables.
3. Lowercase the strings and split them into letter arrays.
4. Loop through the second array and check if the letter is absent in the first array.
5. If at any point, any letter is absent, immediately return false (automatically ends the loop and gets out of the function).
6. If the loop goes through till the end, it means all the letters are present. So, return true.

This is what I have so far:

function mutation(arr) {
let newArr1=arr[0]
let newArr2=arr[1]
let newArr11=newArr1.toString().toLowerCase().split("")
let newArr22=newArr1.toString().toLowerCase().split("")
for (let i=0;i<newArr22.length;i++) {
if (newArr22[i]==newArr11.indexOf(0)) {
return true;
}
return false
}
return newArr2
}

console.log(mutation(["hello", "hey"]));

I tried using indexOf() and couldn’t get it to work.
You could use the includes() method instead of indexOf():

function mutation(arr) {
let newArr1=arr[0].toString().toLowerCase().split("")
let newArr2=arr[1].toString().toLowerCase().split("")
for(let i = 0; i < newArr2.length; i++) {
if(!newArr1.includes(newArr2[i])) {
return false;
}
}
return true;
}