Q - 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”.
CODE
function mutation(arr) {
let test = arr[1].toLowerCase();
let target = arr[0].toLowerCase();
for(let i = 0; i < test.length; i++){
if(target.indexOf(test[i]) < 0){
return false;
}
return true
}
}
mutation(["hello", "hey"]);
All other test work but
mutation([“hello”, “hey”]) does not
notice this: once a return statement is met, the function stops and return a value
that means that a value is returned at first iteration of the loop, and only first character is checked
Hello
I cant seem to understand what you are trying to say. It is indeed meant to return false and stop right there if it does not match
you have if (...) {return false} return true
, one of the two will always execute at first iteration and the function stops after checking only first character
2 Likes
Oh thank you so much. It seems so silly now but I went and went over it and couldnt notice. Most of the tests were passed by this code, only one was not and if it hadnt been there I wouldve gotten through with this faulty silly code.
1 Like
The return true;
needed to be outside of the closing brace for the for
loop.
1 Like
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
Thank you for understanding.
Got it! Thanks for letting me know.