W3Cschool初级脚本算法(13.数组查询算法挑战)

数组查询算法挑战


问题:

  如果数组第一个字符串元素包含了第二个字符串元素的所有字符,则函数返回true。

举例,["hello", "Hello"]应该返回true,因为在忽略大小写的情况下,第二个字符串的所有字符都可以在第一个字符串找到。

["hello", "hey"]应该返回false,因为字符串"hello"并不包含字符"y"。

["Alien", "line"]应该返回true,因为"line"中所有字符都可以在"Alien"找到。


要求:

mutation(["hello", "hey"]) 应该返回 false.

mutation(["hello", "Hello"]) 应该返回 true.

mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]) 应该返回 true.

mutation(["Mary", "Army"]) 应该返回 true.

mutation(["Mary", "Aarmy"]) 应该返回 true.

mutation(["Alien", "line"]) 应该返回 true.

mutation(["floor", "for"]) 应该返回 true.

mutation(["hello", "neo"]) 应该返回 false


问题答案:

function mutation(arr) {
var s=[];
var str= arr[0].toLocaleLowerCase();
var str1=arr[1].toLocaleLowerCase();
for(var i=0;i<str1.length;i++){
if(str.indexOf(str1[i])>0 ||str.indexOf(str1[i])==0){
s.push(str.indexOf(str1[i]));
}else{
return false;
}
}
if(s.length==str1.length){
return true;
}
}

mutation(["hello", "hey"]);

 


题目链接:

https://www.w3cschool.cn/codecamp/mutations.html

猜你喜欢

转载自blog.csdn.net/qq_42044073/article/details/82463223