www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-28-2009, 08:53 PM
    scheric1 scheric1 is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    analyze text with objects

    Ok so I just want to start off by saying I know very little about javascript. I am currently in a beginners course for javascript and am just being introduced to objects.

    The goal of what I am trying to do is have a field were a user can type in text. From there after pressing a button The code needs to do two main things.

    1. Give a number of occurrences of each letter of the alphabet used in the text

    user enters: Hello world

    eg. A=0, B=0,.. E=1,.. L= 3, etc

    2. Length of words

    eg. 5-letter word = 2 (this needs to display only the ones that are found)

    Like I said I am just being introduced to objects and have a minimal foundation with functions and arrays. Hopefully someone can help me with this or at least give me some sort of direction on where to start.
    Reply With Quote
      #2  
    Old 11-29-2009, 09:35 AM
    Specht08 Specht08 is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 44
    Hi
    @1:
    you can use the split function with an empty string as attribute: string.split(""); put this into a variable and check every index of the array with a switch expression:
    Code:
    var a = "Hello World";
    var b = a.split("");
    for(i in b){
       switch(b[i]){
           case "a": ....; break;
           case "b": ....; break;
           .....
       }
    }
    @2: once again use split(), go trough the array and use string.length for comparision
    Reply With Quote
      #3  
    Old 11-29-2009, 05:56 PM
    scheric1 scheric1 is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    So would this count the string correctly for both?

    Code:
    var counter = new Array(26);
    	counter = [ ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    var counter2 = new Array()
    	counter2 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,]; 
    var a = "hello world";//user input test
    var b = a.split("");//Split at each character
    var c = a.split(" ");//Split at spaces
    
    	
    	for (var i in b){
    		switch(b[i]){
    			case "a": ++counter[1]; break;
    			case "b": ++counter[2]; break;
    			case "c": ++counter[3]; break;
    			case "d": ++counter[4]; break;
    			case "e": ++counter[5]; break;
    			case "f": ++counter[6]; break;
    			case "g": ++counter[7]; break;
    			case "h": ++counter[8]; break;
    			case "i": ++counter[9]; break;
    			case "j": ++counter[10]; break;
    			case "k": ++counter[11]; break;
    			case "l": ++counter[12]; break;
    			case "m": ++counter[13]; break;
    			case "n": ++counter[14]; break;
    			case "o": ++counter[15]; break;
    			case "p": ++counter[16]; break;
    			case "q": ++counter[17]; break;
    			case "r": ++counter[18]; break;
    			case "s": ++counter[19]; break;
    			case "t": ++counter[20]; break;
    			case "u": ++counter[21]; break;
    			case "v": ++counter[22]; break;
    			case "w": ++counter[23]; break;
    			case "x": ++counter[24]; break;
    			case "y": ++counter[25]; break;
    			case "z": ++counter[26]; break;
    			
    		}
    	}
    
    	for (var j in c){
    		switch(c[j]){
    			case a.length=1: ++counter2[0]; break;
    			case a.length=2: ++counter2[1];	break;
    			case a.length=3: ++counter2[2];	break;
    			case a.length=4: ++counter2[3];	break;
    			case a.length=5: ++counter2[4];	break;
    			case a.length=6: ++counter2[5];	break;
    			case a.length=7: ++counter2[6];	break;
    			case a.length=8: ++counter2[7];	break;
    			case a.length=9: ++counter2[8];	break;
    			case a.length=10: ++counter2[9]; break;
    			case a.length=11: ++counter2[10]; break;
    			case a.length=12: ++counter2[11]; break;
    			case a.length=13: ++counter2[12]; break;
    			case a.length=14: ++counter2[13]; break;
    			case a.length=15: ++counter2[14]; break;
    			case a.length=16: ++counter2[15]; break;
    			case a.length=17: ++counter2[16]; break;
    			case a.length=18: ++counter2[17]; break;
    			case a.length=19: ++counter2[18]; break;
    			case a.length=20: ++counter2[19]; break;
    			case a.length=21: ++counter2[20]; break;
    			case a.length=22: ++counter2[21]; break;
    			case a.length=23: ++counter2[22]; break;
    			case a.length=24: ++counter2[23]; break;
    		}
    	}
    Reply With Quote
      #4  
    Old 11-29-2009, 06:09 PM
    Specht08 Specht08 is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 44
    first one should basically work.
    within the second one you may misunderstood me.
    I meant the following:
    Code:
    var string = "Hello World";
    var words = string.split(" ");
    var len = 5;
    var counter = 0;
    for(var i in words){
         if(words[i].length == len)
              counter++;
    }
    Reply With Quote
      #5  
    Old 11-29-2009, 06:21 PM
    scheric1 scheric1 is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    Quote:
    Originally Posted by Specht08 View Post
    first one should basically work.
    within the second one you may misunderstood me.
    I meant the following:
    Code:
    var len = 5;
    
    for(var i in words){
         if(words[i].length == len)
              counter++;
    }
    I'm not sure if I follow the var len = 5. What if there is a different sentence such as "My name is jon doe". Now we have 2(2),3(2) and 4(1) character words. how would this work in that case? should var len be an Array instead?
    Reply With Quote
      #6  
    Old 11-29-2009, 06:39 PM
    Specht08 Specht08 is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 44
    ok sorry I misunderstood you, the way with the array is not very effective because you have a limited amount of lengths available. But it's the simplest. Your switch is wrong.
    Code:
    for (var j in c){
    		switch(c[j].length){
    			case 1: ++counter2[0]; break;
    Reply With Quote
      #7  
    Old 11-29-2009, 06:55 PM
    scheric1 scheric1 is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    ok so how about this. Do you think the counters will be right now?

    Code:
    var counter   = new Array(26);
         counter   = [ ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    var counter2 = new Array();
         counter2 = [ ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    var string     = "hello world";//user input test
    var letters    = string.split("");//Split at each character
    var words     = string.split(" ");//Split at spaces
    
    	
    	for (var i in letters){
    		switch(letters[i]){
    			case "a": ++counter[1]; break;
    			case "b": ++counter[2]; break;
    			case "c": ++counter[3]; break;
    			case "d": ++counter[4]; break;
    			case "e": ++counter[5]; break;
    			case "f": ++counter[6]; break;
    			case "g": ++counter[7]; break;
    			case "h": ++counter[8]; break;
    			case "i": ++counter[9]; break;
    			case "j": ++counter[10]; break;
    			case "k": ++counter[11]; break;
    			case "l": ++counter[12]; break;
    			case "m": ++counter[13]; break;
    			case "n": ++counter[14]; break;
    			case "o": ++counter[15]; break;
    			case "p": ++counter[16]; break;
    			case "q": ++counter[17]; break;
    			case "r": ++counter[18]; break;
    			case "s": ++counter[19]; break;
    			case "t": ++counter[20]; break;
    			case "u": ++counter[21]; break;
    			case "v": ++counter[22]; break;
    			case "w": ++counter[23]; break;
    			case "x": ++counter[24]; break;
    			case "y": ++counter[25]; break;
    			case "z": ++counter[26]; break;
    			
    		}
    	}
    
    	for (var j in words){
    			switch(words[j].length){
    				case 1: ++counter2[0]; break;
    				case 2: ++counter2[1]; break;
    				case 3: ++counter2[2]; break;
    				case 4: ++counter2[3]; break;
    				case 5: ++counter2[4]; break;
    				case 6: ++counter2[5]; break;
    				case 7: ++counter2[6]; break;
    				case 8: ++counter2[7]; break;
    				case 9: ++counter2[8]; break;
    				case 10: ++counter2[9]; break;
    				case 11: ++counter2[10]; break;
    				case 12: ++counter2[11]; break;
    				case 13: ++counter2[12]; break;
    				case 14: ++counter2[13]; break;
    				case 15: ++counter2[14]; break;
    				case 16: ++counter2[15]; break;
    				case 17: ++counter2[16]; break;
    				case 18: ++counter2[17]; break;
    				case 19: ++counter2[18]; break;
    				case 20: ++counter2[19]; break;
    				case 21: ++counter2[20]; break;
    				case 22: ++counter2[21]; break;
    				case 23: ++counter2[22]; break;
    				case 24: ++counter2[23]; break;
    				case 25: ++counter2[24]; break;
    				case 26: ++counter2[25]; break;
    				case 28: ++counter2[26]; break;
    			}
    	}
    Reply With Quote
      #8  
    Old 11-30-2009, 06:22 AM
    Kor's Avatar
    Kor Kor is online now
    Red Devil Moderator
     
    Join Date: Dec 2003
    Location: Bucharest, ROMANIA
    Posts: 11,522
    Could be as simple as that (on using Regular Expressions):
    Code:
    function counter(string){
    var ch=['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 NC={}, NW=string.length, reg, i;
    for(i=0;i<ch.length;i++){// build an object "character:occurance"
    	reg=new RegExp(ch[i],'gi');
    	NC[ch[i]]=string.match(reg)?string.match(reg).length:0;
    }
    NW=string.match(/\b/g)?string.match(/\b/g).length/2:string.length;//words counter
    return [NC,NW]
    }
    Here's an example:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>untitled</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function counter(string){
    var ch=['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 NC={}, NW=string.length, reg, i;
    for(i=0;i<ch.length;i++){// build an object "character:occurance"
    	reg=new RegExp(ch[i],'gi');// global and case insensitive
    	NC[ch[i]]=string.match(reg)?string.match(reg).length:0;
    }
    NW=string.match(/\b/g)?string.match(/\b/g).length/2:string.length;//words counter using the boundary mark
    return [NC,NW]
    }
    function results(string){
    var NC=counter(string)[0];
    var NW=counter(string)[1];
    var chTXT='', woTXT='Number of words is: '+NW;
    for(c in NC){
    chTXT+=c+'='+NC[c]+'<br>';
    }
    document.getElementById('characters').innerHTML=chTXT;
    document.getElementById('words').innerHTML=woTXT;
    }
    </script>
    </head>
    <body>
    <form action="">
    <input type="text" name="inp"><input type="button" value="Count" onclick="results(inp.value)">
    </form>
    <div id="characters"></div>
    <br>
    <div id="words"></div>
    </body>
    </html>
    More about Regular Expressions in javascript:
    http://lawrence.ecorp.net/inet/samples/regexp-intro.php

    Last edited by Kor; 11-30-2009 at 06:27 AM.
    Reply With Quote
      #9  
    Old 11-30-2009, 07:04 PM
    scheric1 scheric1 is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    Thanks, that works good but I don't quite understand it. Since this is for an assignment in my class I need to comment everything and I need to use stuff that I've used so far. Is there anyway to do it similar to what I have so far? Also how would I output this properly?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <script language="javascript" type="text/javascript">
    
    var counter    = new Array(26);
    	counter    = [ ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    var counter2   = new Array();
    	counter2   = [ ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    var letters    = string.split("");//Split at each character
    var words      = string.split(" ");//Split at spaces
    
    function gen(){
    //User input string
    	var userInput = document.getElementById("inputField");
    		userInput.toLowerCase();
    		
    		for (var i in letters){
    				switch(letters[i]){
    					case "a": ++counter[1]; break;
    					case "b": ++counter[2]; break;
    					case "c": ++counter[3]; break;
    					case "d": ++counter[4]; break;
    					case "e": ++counter[5]; break;
    					case "f": ++counter[6]; break;
    					case "g": ++counter[7]; break;
    					case "h": ++counter[8]; break;
    					case "i": ++counter[9]; break;
    					case "j": ++counter[10]; break;
    					case "k": ++counter[11]; break;
    					case "l": ++counter[12]; break;
    					case "m": ++counter[13]; break;
    					case "n": ++counter[14]; break;
    					case "o": ++counter[15]; break;
    					case "p": ++counter[16]; break;
    					case "q": ++counter[17]; break;
    					case "r": ++counter[18]; break;
    					case "s": ++counter[19]; break;
    					case "t": ++counter[20]; break;
    					case "u": ++counter[21]; break;
    					case "v": ++counter[22]; break;
    					case "w": ++counter[23]; break;
    					case "x": ++counter[24]; break;
    					case "y": ++counter[25]; break;
    					case "z": ++counter[26]; break;
    					
    				}
    		}
    	
    		for (var j in words){
    				switch(words[j].length){
    					case 2: ++counter2 [1]; break;
    					case 3: ++counter2 [2]; break;
    					case 4: ++counter2 [3]; break;
    					case 5: ++counter2 [4]; break;
    					case 6: ++counter2 [5]; break;
    					case 7: ++counter2 [6]; break;
    					case 8: ++counter2 [7]; break;
    					case 9: ++counter2 [8]; break;
    					case 10: ++counter2[9]; break;
    					case 11: ++counter2[10]; break;
    					case 12: ++counter2[11]; break;
    					case 13: ++counter2[12]; break;
    					case 14: ++counter2[13]; break;
    					case 15: ++counter2[14]; break;
    					case 16: ++counter2[15]; break;
    					case 17: ++counter2[16]; break;
    					case 18: ++counter2[17]; break;
    					case 19: ++counter2[18]; break;
    					case 20: ++counter2[19]; break;
    					case 21: ++counter2[20]; break;
    					case 22: ++counter2[21]; break;
    					case 23: ++counter2[22]; break;
    					case 24: ++counter2[23]; break;
    					case 25: ++counter2[24]; break;
    					case 26: ++counter2[25]; break;
    					case 28: ++counter2[26]; break;
    				}
    		}
    }
    
    
    //-->
    </script>
    </head>
    
    <body>
    <form action="" method="get">
    <input id="inputField" type="text" /><input name="Analyze" type="button" value="Analyze" onclick="gen()" />
    </form>
    </body>
    </html>
    Reply With Quote
    Reply

    Bookmarks

    Tags
    objects


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:28 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.