Tell us what’s happening:
The first test is not passing. I got desperate and looked at the solution on get a hint and I have pretty much what they have for their solution so I tried their solution and it passes but mine does not. I do not notice any major differences besides variable names and they are doing < 0 and im doing > -1.
Also, when i run the console log i have commented out it responds back -1, so it should return false.
Your code so far
function mutation(arr) {
var arrOne = arr[1].toLowerCase();
var arrZero = arr[0].toLowerCase();
for (var i = 0; i < arrOne.length; i++) {
if (arrZero.indexOf(arrOne[i]) > -1) {
return true;
}
return false;
}
//console.log(arrZero.indexOf(arrOne[i]));
}
mutation(["hello", "hey"]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.
The reason your code above fails the first test is because in the first iteration of the for loop, i = 0 and arrOne[0] is “h”. arrZero.indexOf(“h”) is 0 and 0 is greater than -1, so you are returning true and the function exits. No other letters get checked in the word “hey” because you are exiting the function too early. You actually just got lucky on the othe tests.
Thanks for the advice, I initially thought that as well, but like I said the solution on the get hint section is pretty much the same. I did however remove the curly braces from the if statement and made it < 0 instead of > -1. Those changes got everything to pass. It would not pass with the curly braces on the if statement no matter what I did.
To be honest I am not sure how that made a difference I thought you had to have the curly braces for the if statement.
function mutation(arr) {
var arrOne = arr[1].toLowerCase();
var arrZero = arr[0].toLowerCase();
for (var i = 0; i < arrOne.length; i++) {
if (arrZero.indexOf(arrOne[i]) < 0)
return false;
}
return true;
}
//console.log(arrZero.indexOf(arrOne[i]));
mutation(["hello", "hey"]);
this is the solution in the hint
function mutation(arr) {
var test = arr[1].toLowerCase();
var target = arr[0].toLowerCase();
for (var i=0;i<test.length;i++) {
if (target.indexOf(test[i]) < 0)
return false;
}
return true;
}
The issue is where the second return statement is. In hint section solution, the return true is outside the for loop. In the first solution you post,you have the return statement inside the for loop. That is a big difference in terms of logic.