babylikesburgh
asked on
I need help with indexOf
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>
Small change in hielo code
I think this may solve your problem
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>
On what I posted, get rid of:
var letters = new Array(26);
It is not needed.
var letters = new Array(26);
It is not needed.
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];
}
for( i in letters)
{
if(letters[i])
result += "\n"+i + ": "+letters[i];
}
to:
for( i in letters)
{
result += "\n"+i + ": "+letters[i];
}
ASKER
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>
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.
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.
ASKER
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!! =)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
heilo's rank says it all ;)
It's Hielo.
Glad to help.
Glad to help.
Open in new window