我试图将句子从一个地方推到另一个地方。搜索对象和.push到新位置
这一切都取决于找到哪些关键字。如果一个句子有两个关键字,那么它会被推送到第一个关键字。
- 猫 - 推猫在他们所有的句子keywordsFound.cats
- 狗 - 推动与狗在他们所有的句子keywordsFound.dogs
- hamst - 全押句子仓鼠他们keywordsFound.hamsters
var data = "I have an oriental cat. I have both a dog and a cat. I always takes care of my American shorthair cat. I have a Birman cat. My parents bought me two Golden Retriever dogs. My dog is a German Shepherd. I always wanted a hamster. I don't have a pet."
var userInput = {
all: [
"I have an oriental cat.",
"I have both a dog and a cat.", //Should only push the first one - dog
"I always take care of my American shorthair cat.",
"I have a Birman cat.",
"My parents bought me two Golden Retriever dogs.",
"My dog is a German Shepherd.",
"I always wanted a hamster.",
"I don't have a pet."
],
keywordsFound: {
cats: [/*Push all sentences with cat here*/],
dogs: [/*Push all sentences with dog here*/],
hamsters: [/*Push all sentences with hamster here*/]
},
noKeywordsFound: [],
}
function search(term) {
if (userInput.all.indexOf(term) !== -1) {
if (term == 'cat') {
}
if (term == 'dog') {
}
if (term == 'hamster') {
}
}
else {
//Push to noKeywordsFound as an array
}
}
//Which ever comes first gets pushed
search('cat')
search('dog') //Contains
search('hamster') //Contains
您同时在'search'功能您正在寻找的'如果(长期== '仓鼠')调用'搜索( 'hamst')' {'。您必须更正测试或通话以使其正常工作。 –
为了改善你的代码,使用'switch'而不是几个'if'。阅读和表现更好。 –
无论仓鼠搜索文字如何,代码都不起作用......我是编程新手,我试图在我走的时候弄明白。我会研究开关是什么...我仍然是这个初学者 –