我不确定我在这里做错了什么。当我第一次使用indexOf时,它完全正常,但是当我第二次使用它时,它没有返回我期望的结果。
function mutation(arr) {
//return arr;
res = "";
for (var x=0; x<arr[1].split("").length; x++) {
if (arr[0].indexOf(arr[1].split("")[x]) !== -1) {
res += "t";
} else {
res += "f";
}
}
// res = ttt
if (res.indexOf("f") !== -1) {
return true;
} else {
return false;
}
}
mutation(["hello", "hey"]);
// this returns true instead of false
mutation(["floor", "loo"]);
// returns false instead of true
如果arr[1]中的元素不在arr[0]中,则mutation应返回false,否则返回true。
.indexOf()
。参考链接:https://dev59.com/j3A65IYBdhLWcg3wyh57 - user4090029