我試圖將句子從一個地方推到另一個地方。搜索對象和.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'。閱讀和表現更好。 –
無論倉鼠搜索文字如何,代碼都不起作用......我是編程新手,我試圖在我走的時候弄明白。我會研究開關是什麼...我仍然是這個初學者 –