Я пытаюсь создать машину случайных кавычек, где пользователь может вставить слово темы, алгоритм ищет это слово в списке кавычек, создает новый список кавычек, содержащих это слово, а затем случайным образом возвращаетцитата из отфильтрованного списка.Если в списке нет слова, он возвращает любую случайную цитату.Я не могу понять, как сделать отфильтрованный список в JavaScript.Пока что у меня есть:
<body>
<p id="quoteDisplay"></p>
</body>
<form>
<input type="text" id="topic" />
<button onclick="wiseQuote()">Quote</button>
</form>
<script type="text/javascript">
var quotes = [
"You've gotta dance like there's nobody watching, Love like you'll never be hurt, Sing like there's nobody listening, And live like it's heaven on earth. - William W. Purkey",
"You know you're in love when you can't fall asleep because reality is finally better than your dreams. - Elbert Hubbard",
"Being deeply loved by someone gives you strength, while loving someone deeply gives you courage - Lao Tzu",
"In three words I can sum up everything I've learned about life: it goes on. - Robert Frost",
"Everything in moderation, including moderation - Oscar Wilde",
"Life isn't about finding yourself. Life is about creating yourself. - George Bernard Shaw",
"Life is like riding a bicycle. To keep your balance, you must keep moving. - Albert Einstein",
"The truth is rarely pure and never simple. - Oscar Wilde",
"A lie can travel half way around the world while the truth is putting on its shoes.- Mark Twain",
"Perhaps one did not want to be loved so much as to be understood. - George Orwell",
"Facts do not cease to exist because they are ignored. - Aldous Huxley",
"Above all, don't lie to yourself. The man who lies to himself and listens to his own lie comes to a point that he cannot distinguish the truth within him, or around him, and so loses all respect for himself and for others. And having no respect he ceases to love. - Fyodor Dostoevsky",
"A thing is not necessarily true because a man dies for it. - Oscar Wilde",
"The unexamined life is not worth living. - Socrates"
]
if (topic != null){
filteredQuotes = quotes.filter(function(){
return ;
});
}
if (filteredQuotes.length) {
randomIndex = Math.floor(Math.random() * filteredQuotes.length);
filteredQuote = filteredQuotes[randomIndex];
} else {
randomIndex = Math.floor(Math.random() * quotes.length);
filteredQuote = quotes[randomIndex];
}
document.getElementById('quoteDisplay').innerHTML = filteredQuote;
}
</script>
Это первый проект, который я создаю, поэтому я надеюсь, что этот код не слишком беспорядок !!Заранее всем большое спасибо :)