gpt4 book ai didi

javascript - 根据几个单词在数组中搜索单词

转载 作者:行者123 更新时间:2023-12-03 00:22:47 25 4
gpt4 key购买 nike

您好,我在如何搜索包含至少部分单词的单词时遇到问题。我的意思是,我在数组 javascript 中找到了包含单词的单词,没有内置函数,除了 PUSH 之外。

此任务不需要index.of、slice、substr、substring、regex等。只需PUSH

这是我到目前为止的代码:

function wordsChecker(sentences, search) {
var result = []
for (var i = 0; i < sentences.length; i++) {
var isCheck = false
for (var j = 0; j < sentences.length; j++) {
for (var k = 0; k < search.length; k++) {
if (sentences[i][j] == search[k]) {
isCheck = true
}
}
}
if (isCheck == true) {
result.push(sentences[i])
}
}
return result
}

console.log(wordsChecker(['broom, room, rox, show room, stroom, root, rote, brother'], 'roo'))

//expected output : [broom, room, show room, stroom, root]

提前致谢

最佳答案

首先你应该制作正确的数组。我希望这段代码对你有用

function wordsChecker(sentences, search) {
var result = [], k = 0;
for (var i = 0; i < sentences.length; i++) {
var isCheck = false;
for (var j = 0; j < sentences[i].length; j++) {
while (k < search.length) {
if (sentences[i][j] == search[k]) {
isCheck = true;
k++;
break;
}
else {
isCheck = false;
k = 0;
break;
}
}
}
if (isCheck === true && k === search.length) {
result.push(sentences[i]);
k = 0;
}
}
return result;
}

console.log(wordsChecker(['broom', 'room', 'rox', 'show room', 'stroom', 'root', 'rote', 'brother'], 'roo'));

关于javascript - 根据几个单词在数组中搜索单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54229632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com