Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Parallel Arrays and Counting

1. ## Parallel Arrays and Counting

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:
Code:
```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?

• 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.
Code:
```//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";
}
}

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:
Code:
```foo
window.foo
window["foo"]```
In case of functions:
Code:
```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.

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

• Sorry for the late reply. It worked fine after I put in the missing opening bracket. Thanks, Glenn!

• Glad you got it working. But did you also do rwedge's other suggestion?

• 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.

• 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.

• I'm 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?
Code:
```var letters="abcdef"
document.write(letters[3]);```

• 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?

• 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.

• I have no FF here at work. Can you post a basic sample page that shows that thing works?

• ??

It doesn't work in ie7.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•