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
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.
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?
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 solutionfreeCodeCamp Algorithm Challenge Guide: Mutations
Looks like I have to learn a lot about RegExp 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?
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.
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;
}