比较数组中的2个字符串以匹配JS(Comparing 2 strings in array to match J.S)

编程入门 行业动态 更新时间:2024-10-27 17:19:40
比较数组中的2个字符串以匹配JS(Comparing 2 strings in array to match J.S)

“如果数组的第一个元素中的字符串包含数组第二个元素中字符串的所有字母,则返回true。” 出于某种原因,除了([“hello”,“hey”])之外,我可以通过其他任何检查点。 有小费吗?

function mutation(arr) { var firstIndex = arr[0].toLowerCase(); var secondIndex = arr[1].toLowerCase(); for(var i = 0; i < arr.length; i++) { if(firstIndex.indexOf(secondIndex.charAt(i)) !== -1) { return true; } else { return false; } } return arr; } mutation(["hello", "hey"]);

"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 some reason I can pass every other checkpoint except (["hello", "hey"]). any tips?

function mutation(arr) { var firstIndex = arr[0].toLowerCase(); var secondIndex = arr[1].toLowerCase(); for(var i = 0; i < arr.length; i++) { if(firstIndex.indexOf(secondIndex.charAt(i)) !== -1) { return true; } else { return false; } } return arr; } mutation(["hello", "hey"]);

最满意答案

这个想法是检查secondIndex每个字符,看它是否包含在firstIndex 。 因此,这里的逻辑将是:对于secondIndex任何字符,如果该字符不在firstIndex ,则返回false(结束函数)。 如果在检查完所有字符后函数没有结束,你知道在firstIndex中找到了每个字符,然后你可以返回true。

function mutation(arr) { var firstIndex = arr[0].toLowerCase(); var secondIndex = arr[1].toLowerCase(); for(var i = 0; i < secondIndex.length; i++) { if(firstIndex.indexOf(secondIndex[i]) === -1) { return false; } } return true; } mutation(["hello", "hey"])

The idea is to check every character in secondIndex to see if it is contained in firstIndex. So the logic here would be: for ever character in secondIndex, if the character is not in firstIndex, return false (ending the function). If the function doesn't end after checking all the characters, you know that each character was found in firstIndex, and you can then return true.

function mutation(arr) { var firstIndex = arr[0].toLowerCase(); var secondIndex = arr[1].toLowerCase(); for(var i = 0; i < secondIndex.length; i++) { if(firstIndex.indexOf(secondIndex[i]) === -1) { return false; } } return true; } mutation(["hello", "hey"])

更多推荐

本文发布于:2023-07-28 01:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298213.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   组中   JS   Comparing   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!