Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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"; 
      }
      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:
    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.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    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)]++;
      }

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for the late reply. It worked fine after I put in the missing opening bracket. Thanks, Glenn!
    Last edited by aarghhelp; 06-11-2007 at 12:57 AM. Reason: spelling

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Glad you got it working. But did you also do rwedge's other suggestion?

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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.

  • #8
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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]);

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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?

  • #10
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by aarghhelp; 06-11-2007 at 07:15 PM. Reason: firefox spelling

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    I have no FF here at work. Can you post a basic sample page that shows that thing works?

  • #12
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ??

    It doesn't work in ie7.
    Last edited by aarghhelp; 06-11-2007 at 08:07 PM.


  •  

    Posting Permissions

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