Mutations + Array.prototype.indexOf()

I’m having hard time with Array.prototype.indexOf():

**I just want a small hint of what am I doing wrong? So far it passes, except for only one, which is: mutation([“hello”, “hey”]) should return false. **

Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, [“hello”, “Hello”], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments [“hello”, “hey”] should return false because the string “hello” does not contain a “y”.
Lastly, [“Alien”, “line”], should return true because all of the letters in “line” are present in “Alien”.

function mutation(arr) {
  var myArr = arr[0].toLowerCase().split('');
  var search = arr[1].toLowerCase().split('');
  for (var i = 0; i < myArr.length; i++){
    for (var j = 0; j < search.length; j++){
      return (myArr.indexOf(search[j], 0)!=-1);
    }
  }
}

mutation(["mary", "army"]);

Your browser information:

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


Link to the challenge:
https://www.freecodecamp.org/challenges/mutations

OK, I will give you a hint.

This line has a return statement in it. When a return statement is encountered anywhere inside a function, then it will send back whatever is on the right side of it and then exit the function immediately. It will exit the function even if it is not finished iterating through a for loop.:

return (myArr.indexOf(search[j], 0)!=-1);

If you were to put a console.log(search[j]); on the line before this return statement, you would see what the current letter being evaluated in the search array. My question for you is, how many letters do want to see displayed in the console (Ctrl + Shft + I in Chrome)?

See if this helps you figure out where the issue is. Ask more questions if you get stuck.

1 Like

I tried by returning outside of ForLoop, no luck. I need to return true(if the chars on the right side matches with the chars on the left side) or false. Maybe I shouldn’t use For Loop? Am i in the right direction?

Thank you! It worked.

Sorry, I have few more questions, If you don’t mind.

Why in this Basic Code Solution: there are no “{” after if statement , it’s only after return false ? I mean I understand why… but I don’t understand How?

function mutation(arr) {
  var test = arr[1].toLowerCase();
  var target = arr[0].toLowerCase();
  for (i=0;i<test.length;i++) {
    if (target.indexOf(test[i]) === -1)
      return false;
  }
  return true;
 }

mutation(["mary", "army"]);

And I also saw your Advanced solution freeCodeCamp Algorithm Challenge Guide: Mutations
Looks like I have to learn a lot about RegExp :sweat_smile: I tried before MDN RegExp, but can’t learn a lot from it)). Can you tell me some kind of guide from where I can learn deep about RegExp?

If there is only one line inside the if statement you can skip the {} brackets .In the solution the } is the closing bracket for for loop

1 Like

Thank you! you opened my eyes! That what happens to me when I try to solve algorithms at 5 am :sweat_smile: I didn’t see the picture all the way.

Thank you! Thank you soo much. I’m really excited :star_struck:

Nice! I didn’t know that you can skip { } even for For Loop Now my mind is blowing)) How?? I need to start thinking outside of the box as well. :thumbsup:

My solution:

function mutation(arr) {
var val;
var tem = “”;
var newVar = [];

temp = arr[0];
temp = temp.toLowerCase();
arr[0] = temp;
temp = arr[1];
temp =temp.toLowerCase();
arr[1] = temp;
strCheck = arr.join();

i= arr[0].length+1;

  strCheck = strCheck.slice(i,strCheck.length);
  newVar = strCheck.split("");
  for(i = 0; i< newVar.length;i++){
     val = arr[0].indexOf(newVar[i]);
     if (val == -1){
       return false;
     }
  } 
  return true;

}