Stuck at Basic Algorithm Challenge Mutations

Stuck at Basic Algorithm Challenge Mutations
0

#1

What is wrong with my code? It is working for all the values except for the first one.

function mutation(arr) {
  var lCap = [];
  
  for (var i=0; i<arr.length; i++){
   
    lCap[i]=arr[i].toLowerCase();
  
  }

  var split = lCap[1].split("");
  
  for (var j=0; j<arr.length; j++) {
    
   if (lCap[0].indexOf(split[j])!=-1) {
     
     return true;
   
   } else {return false; }
  
}

}

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

#2

So I was testing bunch of condition without understanding properly and the following code solved it. Now I am trying to figure out why it solved it but the other didn’t


function mutation(arr) {
  var lCap = [];
  
  for (var i=0; i<arr.length; i++){
   
    lCap[i]=arr[i].toLowerCase();
  
  }

  var split = lCap[1] ;
  
  for (var j=0; j<lCap[1].length; j++) {
    
   if (lCap[0].indexOf(split[j]) < 0)   
     
     return false;
   
}

   return  true;
   
}

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

#3

Using if Logic also solves the first one but doesn’t solve other. Ok what is happening here? :frowning:

   if (lCap[0].indexOf(split[j]) > 0)   
     
     
     
     return false;
   
     

  
}

   return  true;
  
  
}

#4

This doesn’t work either. Which is similar to the solution but I just changed the sign and condition


function mutation(arr) {
  var lCap = [];
  
  for (var i=0; i<arr.length; i++){
   
    lCap[i]=arr[i].toLowerCase();
  
  }

  var split = lCap[1] ;
  
  
  
 
   for (var j=0; j<lCap[1].length; j++) {
    
   if (lCap[0].indexOf(split[j]) > 0)   
     
     
     
     return true;
   
     

  
}

   return  false;
  
  
}

  

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


#5
 for (var j=0; j<lCap[1].length; j++) {
   if (lCap[0].indexOf(split[j]) > 0)   
     return true; 
}

So what this piece of code does is ,it to check if every character in split array is present in lCap[0] and if it is found just return true and exit out of the function which is contrary to want you want the algorithm to do.
The logic must be that if any character from split array is not found in lCap[0] return false and exit from the function whereas if the character is present then do nothing and continue executing the for loop.
To check if the character is present in the string you could do this
if (lCap[0].indexOf(split[j]) == -1)
( .indexOf() returns -1 if the character is not found in the target string) and at last when the loop is over and control is still in the function then return true;

Instead of changing the conditions and signs randomnly ,sitback and try n understand the logic behind the code and is probably causing the problem.


#6
 for (var j=0; j<lCap[1].length; j++) {
   if (lCap[0].indexOf(split[j]) > 0)   
     return true; 
}

So what this piece of code does is ,it to check if every character in split array is present in lCap[0] and if it is found just return true and exit out of the function which is contrary to want you want the algorithm to do.
The logic must be that if any character from split array is not found in lCap[0] return false and exit from the function whereas if the character is present then do nothing and continue executing the for loop.
To check if the character is present in the string you could do this
if (lCap[0].indexOf(split[j]) == -1)
( .indexOf() returns -1 if the character is not found in the target string) and at last when the loop is over and control is still in the function then return true;

Instead of changing the conditions and signs randomnly ,sitback and try n understand the logic behind the code and is probably causing the problem.


#7

First of all, thank you for explaining the problem so well.

So, if I understood the logic correctly then when True is returned the loop doesn’t continue. It ends. This is why for first letter “h” it returns true and just ends.

If it’s given a return value of False then when it finds “h” has been found but it is supposed to return only value when it’s False, so it continues the loop.

Thank you again for this.

Instead of changing the conditions and signs randomnly ,sitback and try n understand the logic behind the code and is probably causing the problem

I may come out as I am putting values randomly. But I wasn’t actually doing that. My initial code solved almost all the values except for one. And I saw one guy using a different version that mine and it solved the code. This led me to experiment with different signs.

In my head I couldn’t understand the fact that the loop is stopped after returning true value. I confused myself a bit.


#8

Glad that my post helped!
Happy coding :smile:


#9

It did help. I asked the question at many places and nobody exactly explained it in a way that was concise and clear.
But your answer did the job. Thanks!