We help IT Professionals succeed at work.

I need help with indexOf

992 Views
Last Modified: 2013-11-19
I need this code to take in some text, go through and count each letter of the text totaling the number of occurences of each letter.  I've created a 26 element array for the letters, but I cannot figure out where to go from where I am!  Please let me know which portion of my code is wrong, I did check it with W3C validator and all was fine.  
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns = "http://www.w3.org/1999/xhtml">
	<head>
		<title>Using IndexOf</title>
		<script type = "text/javascript">
  		   <!--
	
			var letters = new Array(26);
			var count;
			var result
		
			var letters = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
			
 
			function searchClick()
			{
				var input = document.getElementById( "inputVal" );
				var x = 0
 
 
				for( x = 0; x < letters.length; ++x );
					count = letters.indexOf(x)
					if( count != -1)
						++result 
					else
						return
				document.getElementById( "output" ).value = x + " " + result;
			}
                    //-->
		</script>
	</head>
	<body>
 
	<form action = "">
		<p>Enter text here that is to be searched:
		<input id = "inputVal" type = "text" />
		<input type = "button" value = "Search" onclick = "searchClick()" /></p>
 
		<p>The frequency of each letter is:
		<textarea id = "output" rows = "10" cols = "30">
		</textarea></p>
 
	</form>
	</body>
</html>

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
try this:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns = "http://www.w3.org/1999/xhtml">
      <head>
            <title>Using IndexOf</title>
            <script type = "text/javascript">
                 <!--
      
                  var letters = new Array(26);
                  var count;
                  var result
            
                  var letters = {a:0, b:0, c:0, d:0, e:0, f:0, g:0, h:0, i:0, j:0, k:0, l:0, m:0, n:0, o:0, p:0, q:0, r:0, s:0, t:0, u:0, v:0, w:0, x:0, y:0, z:0};
                  
 
                  function searchClick()
                  {
                        var str = document.getElementById( "inputVal" ).value; 
					for( var i=0; i < str.length; ++i){
						if( "undefined"!=typeof(letters[str.charAt(i)]) )
							++letters[str.charAt(i)];
					}
					var result="";
					for( i in letters)
					{
						if(letters[i])
							result += "\n"+i + ": "+letters[i];
					}
                        document.getElementById( "output" ).value = result.substring(1);
                  }
                    //-->
            </script>
      </head>
      <body>
 
      <form action = "">
            <p>Enter text here that is to be searched:
            <input id = "inputVal" type = "text" />
            <input type = "button" value = "Search" onclick = "searchClick()" /></p>
 
            <p>The frequency of each letter is:
            <textarea id = "output" rows = "10" cols = "30">
            </textarea></p>
 
      </form>
      </body>
</html>

Open in new window

CERTIFIED EXPERT

Commented:
Small change in hielo code

I think this may solve your problem
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns = "http://www.w3.org/1999/xhtml">
      <head>
            <title>Using IndexOf</title>
            <script type = "text/javascript">
                 <!--
      
                 
                  
 
                  function searchClick()
                  {
				   var letters = new Array(26);
                  var count;
                  var result
            
                  var letters = {a:0, b:0, c:0, d:0, e:0, f:0, g:0, h:0, i:0, j:0, k:0, l:0, m:0, n:0, o:0, p:0, q:0, r:0, s:0, t:0, u:0, v:0, w:0, x:0, y:0, z:0};
                        var str = document.getElementById( "inputVal" ).value; 
					for( var i=0; i < str.length; ++i){
						if( "undefined"!=typeof(letters[str.charAt(i)]) )
							++letters[str.charAt(i)];
					}
					var result="";
					for( i in letters)
					{
						if(letters[i])
							result += "\n"+i + ": "+letters[i];
					}
                        document.getElementById( "output" ).value = result.substring(1);
                  }
                    //-->
            </script>
      </head>
      <body>
 
      <form action = "">
            <p>Enter text here that is to be searched:
            <input id = "inputVal" type = "text" />
            <input type = "button" value = "Search" onclick = "searchClick()" /></p>
 
            <p>The frequency of each letter is:
            <textarea id = "output" rows = "10" cols = "30">
            </textarea></p>
 
      </form>
      </body>
</html>

Open in new window

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
On what I posted, get rid of:
var letters = new Array(26);

It is not needed.
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
and if you want to see the frequency for every letter, change:
for( i in letters)
{
      if(letters[i])
      result += "\n"+i + ": "+letters[i];
}

to:
for( i in letters)
{
      result += "\n"+i + ": "+letters[i];
}

Author

Commented:
Below is what I ended up with, I switched around some things to your suggestions.  I ran your code and it worked PERFECT.  But, in running my own code, it shows all letters as "0" times used.  With this being a class-related script, I don't want to just copy and paste it, but do you see where I deviated?  I did, of course, change the variable "i" to variable "x".  But thanks so much for your help, I'm glad to at least know I'm getting there and wasn't TOO far off =)    
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns = "http://www.w3.org/1999/xhtml">
	<head>
		<title>Using IndexOf</title>
		<script type = "text/javascript">
  		   <!--
	
 
			
 
			function searchClick()
			{
				var result;
				var letters = {a:0, b:0, c:0, d:0, e:0, f:0, g:0, h:0, i:0, j:0, k:0, l:0, m:0, n:0, o:0, p:0, q:0, r:0, s:0, t:0, u:0, v:0, w:0, x:0, y:0, z:0}
				var strInput = document.getElementById( "inputVal" ).value;
 
 
				for( var x = 0; x < strInput.length; ++x );
				{
					if( "undefined"!=typeof(letters[strInput.charAt(x)]) )
						++letters[strInput.charAt(x)]; 
				}
			
				result = "";
				for( x in letters )
				{
					result += "\n" + x + ": " + letters[x];
				}
				
				document.getElementById( "output" ).value = result.substring(1);
			}
                    //-->
		</script>
	</head>
	<body>
 
	<form action = "">
		<p>Enter text here that is to be searched:
		<input id = "inputVal" type = "text" />
		<input type = "button" value = "Search" onclick = "searchClick()" /></p>
 
		<p>The frequency of each letter is:
		<textarea id = "output" rows = "10" cols = "30">
		</textarea></p>
 
	</form>
	</body>
</html>

Open in new window

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
you have a trailing comma immediately after your for - get rid of it. When you do this:
for( var x = 0; x < strInput.length; ++x );

it equivalent to this:
for( var x = 0; x < strInput.length; ++x ){
}

notice that NOTHING is withing the {} so literally nothing happens 'within' the loop, other than the x is incremented but the letter frequecies are NOT incremented because it is outside the for.

Author

Commented:
Awesome... working fine now.  I can't believe I didn't look for that, I made the same mistake on a prior script.  One last thing... where would I convert the string to lowercase?  It's supposed to count uppercase AND lowercase.  I know to use toLowerCase(), I tried putting strInput.toLowerCase(); under var strInput = document...... but didn't work.  If you could throw me this last hint I'll be good to go!!  =)
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
heilo's rank says it all ;)
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
It's Hielo.
Glad to help.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.