I`m get stuck on Mutations

I`m get stuck on Mutations
0
#1

https://www.freecodecamp.org/challenges/mutations

Not work “mutation([“hello”, “hey”]) should return false” but everyone else is working.

function mutation(arr) {
var test = arr[0].toLowerCase();
var tar = arr[1].toLowerCase();
for(i = 0;i < tar.length;i++){
if (test.indexOf(tar[i]) < 0){
return false;
}else{
return true;
}
}
}
mutation([“hello”, “hey”]);

0 Likes

#2

Instead indexOf the whole word, do it but for every letter of tar[1]. You can use method charAt(i) where for “i” you loop through every individual letter.

0 Likes

#3

You’re only testing the first letter.

return exits your function. In your case you test the first letter and return either true or false based on that one test.

Look at this post for same issue

Good luck!

0 Likes

#4

Also, when asking for help…

Do a quick search to see if your issue has already been answered on the forum.
That is the Search part of Read, Search, Ask

Format your code(wrap in back-ticks) so everyone can read it more easily. It is much easier to copy / paste formatted code for further testing too.

More about formatting in forum

0 Likes

#5

Hello I have a problem on this challenge… So I’m here to ask for help, here you can see my code :
function mutation(arr) {
for(let i=0; i<arr[0].length;i++) {
for(let j=0; j<arr[1].length;j++) {
if (arr[i]!==arr[j]) {
return false;
} else {
return true;
}
}
}
}
mutation([“hello”, “hey”]);
This is works for all the cases where the result should be true but not where it should be false… I read some solutions about it but I don’t understand what is wrong with my code if someone can help me please… Thank you :slight_smile:

0 Likes

#6

You have opened a thread of your own, you don’t need to comment on other threads after that, it will also help because if you post on one place only you will also have answers on one place only

0 Likes

#7

I am sorry about I will be careful for the next time. Have you got any idea how to solve my problem ?

0 Likes

#8

I answered you in the thread you opened

0 Likes