"気分キーワード"に基づいて動機付けの引用を表示するウェブページを作成しています。string.includes forループの最後の値をチェックするjavascript
私は配列を持つオブジェクトがあります。
const moodList = {
sad: [
'"Dream as if you\'ll live forever, live as if you\'ll die today." James Dean',
'"Life is a journey, and if you fall in love with the journey, you will be in love forever." Peter Hagerty',
'"I\'ve learned that people will forget what you said, people will forget what you did, but people will never forget how you made them feel." Maya Angelou'
],
angry: [
'"For every minute you remain angry, you give up sixty seconds of peace of mind."Ralph Waldo Emerson',
'"Speak when you are angry - and you\'ll make the best speech you\'ll ever regret." Laurence J. Peter',
'"Anger and intolerance are the enemies of correct understanding."Mahatma Gandhi'
]}
を、これは私が配列を持つオブジェクトに対するユーザー入力をチェックするために書いているコードです。一致があると、その特定の配列からランダムな動機付けの引用が表示されます。しかし、else文を書くと、なぜオブジェクトの中の最後の配列だけが考慮されていますか?
for (let key in moodList) {
const moodFeeling = key;
if (askBar.value.includes(moodFeeling)) {
for (let i = 0; i <moodList[moodFeeling].length; i += 1) {
motivQuoteResult.push(moodList[moodFeeling][i]);
const randomResult = Math.floor(Math.random() * motivQuoteResult.length);
printToPage(motivQuoteResult[randomResult]);
}
} else {
printToPage('<h3>Try again, I don\'t have that feeling on file</h3>');
}
}
motivQuoteResult = [];
askBar.value = '';
}
});
を私は "悲しい" のためのelse文を毎回取得します。 「怒っている」は正しく動作します。 – Amos
コードがありませんか?スニペットの最後に余分なコーディングカッコがあるようです... – Pineda
申し訳ありませんが、余分なコードはありません。私はこれを初めて知っており、コードを正しく貼り付ける方法を考え出していません。 – Amos