Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a script based on the program in Exercise 11.13 that inputs several lines

ID: 670290 • Letter: W

Question

Write a script based on the program in Exercise 11.13 that inputs several lines of text and uses String method indexOf to determine the total number of occurances of each letter of the alphabet in the text. Uppercase and lowercase letter should be counted together. Store the totals for each letter in an array, and print the values in tabular format in an HTML5 textarea after the totals have been determined.

*Exercise 11.13 was to write a script that inputs several lines of text and a search character and uses String method indexOf to determine the number of occurances of the character in the text. *

and here is the code i used for 11.13, thanks in advance!!

<html>

<head>

<script language="JavaScript"><!--

function countInstances(string, word)

{

var substrings = string.split(word);

return substrings.length - 1;

}

//--></script>


</head>

<body>

<form>

<input type="text" name="string" value="input text">

<br>

<input type="text" name="word" value="insert character to be searched">

<br>

<input type="button" value="count occurance">

</form>

</body>

</html>

Explanation / Answer

HTML:

<html>

<body>

<div id="result">

Input Text

<input type="text" name="string" value="">

<br>

<br>

<input type="button" value="Count Occurance">

    </div>

</body>

</html>

JavaScript:

function countInstances(str)

{

var results = "";

var alpha = new Array();

alpha[0]="a";alpha[1]="b";alpha[1]="c";alpha[3]="d";alpha[4]="e";alpha[5]="f";alpha[6]="g";alpha[7]="h";alpha[8]="i";alpha[9]="j";alpha[10]="k";alpha[11]="l";alpha[12]="m";alpha[13]="n";alpha[14]="o";alpha[15]="p";alpha[16]="q";alpha[17]="r";alpha[18]="s";alpha[19]="t";alpha[20]="u";alpha[21]="v";alpha[22]="w";alpha[23]="x";alpha[24]="y";alpha[25]="z";

var len=str.length;

var count[]=new Array();

for(j=0;j<26;j++)

{

               count[j]=0;

                              if(parseInt(str.indexOf(alpha[j]) ||str.indexOf(alpha[j].toLowerCase()))>-1)

                              {

                              count[j]++;

                              }

}

     results = "<table>";

for (var i=0; i<26; i++) {

    results += "<tr><td>" +alpha[i] + "</td>";

    results += "<td>" + count[i] + "</td></tr>";

}

results += "</table><br /> <br />";

var t=document.getelementById("result");  

    t.innerHTML=results;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote