Mutations challenge is mutating me!

Mutations challenge is mutating me!
0.0 0

#1

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.

Link to the challenge:


#2

Oh Doh! Solved!

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!

Oh well - it is a good lesson :slight_smile:


#3

Was just about to say that: you are returning way too early; you should return true only after finishing looping through the whole word.

The reason why
"Hello Hey" returns true is because the first letters ‘h’ is present without even looking at the remaining letters.


#4

Thanks Marmiz,

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.


#5
 *******Mutations (Solution)********

function mutation(arr) {

var x = arr[0].toLowerCase().split(’’);

var y = arr[1].toLowerCase().split(’’);

return y.every(function(val){

return x.includes(val) ;

});
}


#6

Thanks ahsanwaseem, I shall study this solution, it is much more elegant than the one I came up with!


#7
function mutation(arr) {
  let arr1 = arr[1].toLowerCase().split(''),
      arr2 = arr[0].toLowerCase();      
  
  return arr1.every(el => arr2.indexOf(el) !== -1);
}

mutation(["hello", "hey"]);

#8

@spzHades - Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.


#9

Ok, sorry. I didn’t knew that. I posted some solutions without blur. I will blur the code solutions from now. Thanks!


#10

In general, we prefer to give hints to members who ask questions about their code instead of full solutions. This way, they still figure most of the solution out on their own.


#11

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.


#12

I think they are working on adding those back in, but I could be mistaken. All the solutions are still in the Guide.


#13

Thanks spzHades, I will also study this solution - so elegant.