Tell us what’s happening:
Im kind of stuck since a moment there. My code return true when the 1st letter’s word match the ones in the second one. Though, my “else” statement seems not to worck, since it doesnt return false when it’s false. Any advice?
Your code so far
function mutation(arr) {
//makes strings 1 & 2 split by characters
let s1 = arr[0].split("");
let s2 = arr[1].split("");
//loop to search into the array
for (let i = 0; i < arr.length; i++) {
//check if I can find the letters in the 1st word, in the second one.
if (arr[0].indexOf(arr[2] == true)){
return true;
}else{
return false;
}
}
}
mutation(["hello", "CAPS"]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0.
Hello
your function should be case in-sensitive
and you need just to iterate through the second string and make your search on the first.
so change arr.length with arr[1].length you can use the every() method.
Thanks for the answer.
I assume i have to put a arr.toLowerCase(), but im not sure how & where and IF i have to store this in another variable. Plus, i end up now with this code:
function mutation(arr) {
//makes strings 1 & 2 split by characters
let s1 = arr[0].split("");
let s2 = arr[1].split("");
//loop to search into the array
for (let i = 0; i < arr[1].length; i++) {
//check if I can find the letters in the 1st word, in the second one.
if (arr[0].indexOf(arr[1]) == true){
return true;
}else{
return false;
}
}
}
mutation(["hello", "CAPS"]);
This now return me false when it should return false, but it doesnt return true anymore when it should. Any advice?
function mutation(arr) {
//makes strings 1 & 2 split by characters
arr.toString().toLowerCase();
let s1 = arr[0].split("");
let s2 = arr[1].split("");
//loop to search into the array
for (let i = 0; i < s2.length; i++) {
//check if I can find the letters in the 1st word, in the second one.
if (s1.indexOf(s2) == true){
return true;
}else{
return false;
}
}
}
mutation(["hello", "CAPS"]);
Do i really have to “toString” arr, since arr is an array and to take toLowerCase? I did the replacement you suggested in the for block. It still doesnt return me true when it should. Any advice? Thanks for yout time & patience by the way.
function mutation(arr) {
//makes strings 1 & 2 split by characters
let s1 = arr[0].toLowerCase().split("");
let s2 = arr[1].toLowerCase().split("");
//loop to search into the array
for (let i = 0; i < s2.length; i++) {
//check if there is one character in s2 that is not in s1
if (s1.indexOf(s2[i]) === -1){
return false;
}
}
return true;
}
Thank you for the complete solution. So, if indexOf.s2 doesnt find the charachter in s1, it return a -1.
I tried to make an if else statement instead of just returning true if the function doesnt find aynthing egal to -1, but it doesnt worck. I can’t figure why though. Any suggestion?
The Idea is : if there is at least one character in s2 not present in s1, the function stop execution at the first occurrence and return false, else it continue the execution until they
rich the return true statement , this mean that all elements of s2 are also elements of s1.