Friday, September 25, 2015

Count Words in a Sentence in JavaScript

This program will allow the user to enter a sentence and then the program will count how many words in a given sentence by the user.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.




Sample Program Output


Program Listing

<html>
<head>
<style>
body {
  background-color:lightgreen;
  font-family:arial;
  font-size:25px;
  color:blue;
  }
  label {
    float: left;
    width: 30%;
    text-align: left;
    margin-right: 1em;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
}
.form-field-no-caption {
    margin-le

input[type="radio"]{
 float:left;
}
</style>
<meta charset="UTF-8">
<title> Count Words in a Sentence </title>

 <script type="text/javascript">

        function count_words() {
        var input = document.getElementById("userInput").value;
              var number = parseInt(input);
                
                 count_word=input.split(" ")
                 document.getElementById("result").innerHTML =
                 "Number of words in a given sentence is " + 
                 count_word.length + ".";
                 }
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h2> Count Words in a Sentence </h2>
Enter a sentence
  <br>
    <textarea cols="40" id="userInput" autofocus></textarea>
    <br><br>
    <input type="submit" value="Check" 
    title="Click here to count the number of words in a sentence."
    onclick="count_words()" /> &nbsp; &nbsp; 
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br><br>
  <p id="result"></h1>
</body>
</html>




No comments:

Post a Comment