Mutations + Array.prototype.indexOf()

Mutations + Array.prototype.indexOf()
0.0 0

#1

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:


#2

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.


#3

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?


#4

Let me share a basic algorithm written in pseudo-code using your variable names. See if your code follows this logic:

Iterate through each letter of search and check if each letter is in myArr.

If a letter in search is not found in myArr return false.

If all letters in search have been traversed, this means all letters were found, so return true.


#5

Thank you! It worked.


#6

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?


#7

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


#8

Here are a few resources to get your started:

https://regexone.com/

Honestly, you just have to practice it. For example, I went back through many of the FCC challenges and anytime I saw a challenge involving string manipulation or searching through a string, I tried to solve it using RegExp. I learned what did and did not work by testing different things until I got it to work.


#9

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.


#10

The following with the for loop { } missing would also work, because the if statement is the only line in the for loop.

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;
}

and even this long line of a for loop statement is valid:

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;
}

#11

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


#12

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:


#13

4 posts were split to a new topic: Mutations - curly braces positioning


#14

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;

}