Tell us what’s happening:
I am stuck on the mutations challenge and have been for a while now!
The code is a boolean check of a two element array - are each of the elements of the second string found in the first. My code correctly returns all the test strings as true/ false - except one, the first one “hello, hey”.
For instance:
“Mary, Aarmy” - correctly returns true.
“hello, neo” - correctly returns false.
But…“hello, hey” should read false, but is returning true! I can’t work out why??? I have checked all the other posts I could find on the forum on this subject and done some google searches and made some changes, but it still won’t work on this one array!!
If anyone could please explain why this one instance isn’t returning correctly that would be great!
Thank- you.
Your code so far
function mutation(arr) {
arr[0] = arr[0].toLowerCase();
arr[1] = arr[1].toLowerCase().split('');
for (var i = 0; i < arr[1].length; i++) {
if (arr[0].indexOf(arr[1][i]) === -1) {
return false;
} else {
return true;
}
}
}
mutation(["hello", "hey"]);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.
I was so focused on the rest of the code that I didn’t realise the “return true” had crept up a bracket or two in the function! Watching out for tigers and getting bitten by mosquitoes instead!
I had been working on this code for a while as I started out with a “!== -1 return true” scenario and you realise how that would that have turned out! All this is quite tricky for someone with no computer science background at all. Good to know you were on the case, much appreciated.
Well to be honest i hate the fact that i can no longer see solutions for the exercises. I learned a lot from solutions. Usually my solutions were basic, and when i looked and intermediate and advanced solutions , i understood that i had to learn advanced methods. I like the new website, i don’t like that there are no solutions anymore :(, you have to use ask the forum.
function mutation(arr) {
var box1 = arr[0].toLowerCase().split("");
var box2 = arr[1].toLowerCase().split("");
for(let i = 0; i < box2.length; i++ ){
var result = box1.includes(box2[i]);
if(result === false){
break;
}
}
return result;
}
mutation(["hello", "hey"]);
The problem with the your code is, it returns true if the first letter matches and false if it doesn’t. But it won’t loop through all elements in the second array.