Word Search
Problem Statement
Create a program that allows the user to enter a paragraph and search for a word in the paragraph. The program should have a simple user interface that includes an input field for the text and a button to submit the text. When the user clicks the button, the program should display the number of times the word appears in the text.
Acceptance Criteria
- The program should have an HTML file with the required user interface elements (input field, button) styled with CSS.
- The program should have a JavaScript function that counts the number of times the word appears in the text.
- When the user clicks the button, the program should count the number of times the word appears in the text and display it to the user.
- The program should handle any errors (e.g., submitting an empty text) gracefully and display an error message to the user.
Hints
- Use the
trim()method to remove any leading or trailing spaces from the text. i.e.text.trim() - Use the
split()method to split the text into an array of words. i.e.text.split(' ') - Use the
toLowerCase()method to convert the word to lowercase. i.e.word.toLowerCase() - Use the
includes()method to check if the word is in the array. i.e.words.includes(word) - Use the
filter()method to filter the array to only include the word. i.e.words.filter(w => w === word) - Use the
lengthproperty to get the number of words in the array. i.e.words.length