I'm making a webpage that displays a motivational quote based on a "mood keyword".
I have an object with arrays:
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'
]}
and this is the code I'm writing to check the user input against the object with arrays. When there's a match it displays a random motivational quote from that specific array. However if I write an else statement, why only the last array inside the object is being considered?
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 = '';
}
});