Skip to main content

Word Counter

Problem Statement

Create a program that allows the user to enter a paragraph and count the number of words 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 words 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 words in the text.
  • When the user clicks the button, the program should count the number of words 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.
  • The program should be well-documented with clear and concise comments explaining each part of the code.

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 length property to get the number of words in the array. i.e. words.length