Write a script that reads several lines of text and prints a table indicating th
ID: 3560060 • Letter: W
Question
Write a script that reads several lines of text and prints a table indicating the number of one-letter words, two-letter words, three-letter words, etc. appearing in the text. For example, the phrase Whether 'tis nobler in the mind to suffer.
This needs to be in java script. I cannot get it to count the words.
<html>
<head>
<title>CountIt</title>
<script type="text/javascript">
<!--
function search1()
{
var str1 = document.getElementById("string1").value;
var str2 = document.getElementById("string2").value;
var len=str1.length;
var count=0;
var search=str1.indexOf(str2);
for(i=0;i<len;i++)
{
if(parseInt(search)!= -1)
{
count++;
var search=str1.indexOf(str2,parseInt(search)+1);
}
}
document.getElementById("ans").value= str2 + " Occurs " + count + " times";
}
// -->
</script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"> </td>
<h1>Exercise 11.13 CountIt</h1>
<hr />
<p>Press F5 or Refresh to load script again. This is a program to determine the number of occurrences of the character in the text .</p>
<form name = "search" action = "">
<table border = "1">
<caption>Search</caption>
<tr><td>Enter any String</td>
<td><input name = "string1" type = "text" />
</td></tr>
<tr><td>Enter search Character</td>
<td><input name = "string2" type = "text" />
</td></tr>
<tr><td>Search Result</td>
<td><textarea name="ans">
</textarea>
</td></tr>
<tr><td><input type = "button" value = "SearchIt"
/></td></tr>
</table>
</form>
</tr>
</table>
</body>
</html>
Explanation / Answer
This script will do these things:
a) Write a script that reads several lines of text from the keyboard and prints a table indicating the number of occurrences of each letter of the alphabet in the text. For example, the phrase
To be, or not to be: that is the question:
contains one
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.