+ 3
What's wrong with this code?
It's supposed to 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. function mutation(arr) { var str = arr.toString(); arr = str.toLowerCase(); arr = str.split(""); if (str[0] != str[1]) { return true; } else { return false; } } mutation(["hello", "hey"]); Thanks in advance.
3 Answers
+ 4
Absolutely....
Remove and type into
if(arr[0].toLowerCase() == arr[1].toLowerCase())return true;
return false;
is enough.(Or shorten it more as you want)
+ 3
https://code.sololearn.com/W6g4BTOIEuAu/#js
Here you go. I'm looping through the array and making it lowercase, then using the new array to check if it's the same words or not. No need to convert your array into a string. Hope this helps. If if you use the loop, it'll scale to however many elements you have in your array.
function mutation(arr) {
var arrF = [];
for (var i = 0; i < arr.length; i++) {
arrF.push(arr[i].toLowerCase());
}
if (arrF[0] != arrF[1]) {
alert("Not same - return true");
return true;
} else {
alert ("Same word - return false");
return false;
}
}
mutation(["hello", "HELLO"]); // returns false - same word
+ 1
Thanks for the help guys.