...

View Full Version : Parallel Arrays and Counting



aarghhelp
06-10-2007, 03:31 AM
I'm trying to take the results of a user input string and compare it to an array of letters a-z and count how many times each letter in the string appears by storing the count in a parallel array. For instance, if if the user typed "help me", the output would be a0...e2 h1 l1 m1 p1...z0 etc.(all the letters are displayed, in alphabetical order).

I thought it might look something like this:

var input = prompt("enter string");
var letters = new Array("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");
var lettCount = new Array(26);

for (var i = 0; i <= letters.length; i++) {
for (var p = 0; p <= input.length; p++) {
var count=0;
if (letters[i]==input[p]){
count++;
lettCount.push(count);
}

} What am I doing wrong?

glenngv
06-10-2007, 06:02 AM
You need to have a counter variable for each letter. As you loop through the entered string, increment the corresponding variable. To easily create 26 variables, you can use the static method String.fromCharCode 65 to 90 which represents A to Z.

//create and initialize counter variables ctrA, ctrB, ... ctrZ.
for (var i=65; i<=90; i++){
window["ctr"+String.fromCharCode(i)] = 0;
}

function countLetter(letters)
letters = letters.toUpperCase();
for (var p = 0; p < letters.length; p++) {
window["ctr"+letters[p]]++;
}
var counts="";
for (var i=65; i<=90; i++){
counts += String.fromCharCode(i) + "=" + window["ctr"+String.fromCharCode(i)] + "\n";
}
alert(counts);
}

var input = prompt("enter string", "");
countLetter(input);
Global variables (and functions for that matter) are all members of the window object. If you have a global variable named foo, it can be accessed in these ways:

foo
window.foo
window["foo"]
In case of functions:

blah()
window.blah()
window["blah"]()

The last one is the technique I used in creating ctrA to ctrZ variables. As you notice, this technique is powerful and dynamic as the string inside the brackets can be expressions as I did in my solution. This technique is called Square Bracket notation and more info can be found in my sig.

rwedge
06-10-2007, 08:57 PM
glenngv, you are missing an opening bracket in your countLetter function :)
function countLetter(letters) {and wouldn't the string characters need to be evaluated to get the desired results
for (var p = 0; p < letters.length; p++) {
window["ctr"+letters.charAt(p)]++;
}

aarghhelp
06-11-2007, 12:56 AM
Sorry for the late reply. It worked fine after I put in the missing opening bracket. Thanks, Glenn!

glenngv
06-11-2007, 09:42 AM
Glad you got it working. But did you also do rwedge's other suggestion?

aarghhelp
06-11-2007, 03:11 PM
If you mean changing letters[p] to letters.charAt(p), then yes and it doesn't seem to make a difference. Otherwise, I guess I don't understand what he said.

glenngv
06-11-2007, 05:53 PM
rwedge is right. letters.charAt(p) will get one letter at a time, while letters[p] will only work if 'letters' is an array of characters which is not in this case.

aarghhelp
06-11-2007, 06:57 PM
I'm confused. :confused: letters is input, which is a string of characters and it works with that, is that because letters is functioning like an array? Why does this work?


var letters="abcdef"
document.write(letters[3]);

glenngv
06-11-2007, 07:01 PM
That should not work, your code will return undefined because letters in that case is not an array. What browser are you testing it with? :confused:

aarghhelp
06-11-2007, 07:06 PM
FF 2.0.0.4



Why did it change the second F in the Firefox abbreviation to lowercase? FFFFF. I edited it again and it's gone.

glenngv
06-11-2007, 07:16 PM
I have no FF here at work. Can you post a basic sample page that shows that thing works?

aarghhelp
06-11-2007, 07:54 PM
??

It doesn't work in ie7.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum