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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2012
    Posts
    137
    Thanks
    1
    Thanked 1 Time in 1 Post

    Having trouble with this logic. Where are the smart people?

    Hello,

    I am having problems with something that seemed really simple at first. I have an XML file (I am not including the entire file, it IS properly formed in its entirety). This file holds data for a glossary. What I am trying to do is populate a div with these terms. I can get it to show all of the terms, what I am having problems with is the 'A', 'B', 'C',.... title at the beginning of each block of terms. In other words, when the 'B' terms begin I want a row that says 'B'. When a letter is not present in the XML (no 'Y' words) I still want the 'Y' title to appear.

    The code below is what I have so far and it works for the most part but I have redone it a few times and I am lost. Can someone please help me out here?

    Code:
    <item term="AAA" def="Not the American Automobile Association" rollover="Anti-aircraft Artillery" />
    
    <item term="AC" def="Alternating Current" />
    
    <!-- B -->
    
    <item term="BAS" def="Building Automation System" />
    
    
    <item term="BBC" def="British Broadcasting Company" />
    
    
    <!-- C -->
    
    
    
    <!-- D -->
    
    <item term="DARPA" def="Defense Advanced Research Projects Agency" />
    
    
    <item term="dB" def="Decibel" />
    Code:
    	function parseGlossXML(xml)
    	{
    		glossaryXML = xml;
    		
    		var thisLetter = $(glossaryXML).find(':first').attr('term').substr(0,1).toUpperCase();
    		var letterArr2 = ['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 letterArrIndex = 0;
    		var continueLoop = true;
    		
    		for(var t = 0; t < letterArr2.length; t++)
    		{
    			if(thisLetter == letterArr2[letterArrIndex])// A == A
    			{alert(thisLetter);
    				$("#glossaryContent").append("<br /><span class='glossAcro'>" + "wtf1 " + thisLetter + "</span><br />");
    				$(glossaryXML).find('item').each(function()
    				{			
    					if(continueLoop)
    					{
    						//$("#glossaryContent").append("<br /><span class='glossAcro'>" + thisLetter + "</span><br />");
    						//letterArrIndex++;
    						continueLoop = false;
    					}
    					else if(!continueLoop)
    					{
    						thisLetter = $(this).attr('term').substr(0,1).toUpperCase();
    						//$("#glossaryContent").append("<br /><span class='glossAcro'>" + thisLetter + "</span><br />");
    						//letterArrIndex++;
    						
    					}
    					$("#glossaryContent").append("<br /><span class='glossAcro'>" + $(this).attr('term') + "</span>" + "<span class='glossDef'>" + $(this).attr('def') + "</span><br />");
    					//letterArrIndex++;
    				});
    				letterArrIndex++;
    			}
    			else if(thisLetter != letterArr2[letterArrIndex])
    			{
    				$("#glossaryContent").append("<br /><span class='glossAcro'>" + "WTF2 " + letterArr2[letterArrIndex] + "</span><br />");
    				letterArrIndex++;
    			}
    		}
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    What was wrong with the answer I gave you in your other thread?

    Suggestions for how to look at first letter, add letter block

    If it didn't work, at least respond in that same thread and say why not and/or what happened.

    If it did work, it would still be nice to get a response.
    Last edited by Old Pedant; 02-19-2013 at 08:21 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Okay, I see the difference. Now you want the letter to appear even if there are no matches in the XML feed.

    See, that would have been *SO EASY* for you to have said in response to my prior post. But for all I know, you didn't bother to read my prior post.

    This looks trivial. Not much harder than the other problem, where I purposely omitted the missing letters.

    Code:
        var curLetter = "@";
        $(glossaryXML).find('item').each(function()
        {
            var first = ($(this).attr('term').charAt(0).toUpperCase();
            while ( first > curLetter )
            {
                // bump the curLetter by one (NOTE: "@" + 1 ==>> "A")
                curLetter = String.fromCharCode( curLetter.charCodeAt(0) + 1 );
    
                if ( curLetter > "Z" ) break; // don't go past last letter (shouldn't happen)
    
                // output a header for curLetter
                $("#glossaryContent").append(... curLetter ... ); 
            }
            $("#glossaryContent").append(...); // output from XML
        } );
    Maybe you could actually respond, this time?
    Last edited by Old Pedant; 02-19-2013 at 08:35 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    Jun 2012
    Posts
    137
    Thanks
    1
    Thanked 1 Time in 1 Post
    Pedant,

    I appologize. I forgot I posted that on Thursday. Around 3:30 that day someone found a problem so I had to jump through hoops correcting it and for got about the post.

    Thanks for the help, I will test it now and get the working code back here.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Quote Originally Posted by m2244 View Post
    ... I had to jump through hoops correcting it and for got about the post.
    Well, that has certainly never happened to me. Today.

    And I've never forgotten a post. In the last hour.

    <grin/>

    Understood. All's good. Hope this one works.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Regular Coder
    Join Date
    Jun 2012
    Posts
    137
    Thanks
    1
    Thanked 1 Time in 1 Post
    Pedant you son of a gun, it works.

    Code:
    var curLetter = "@";
    		$(glossaryXML).find('item').each(function()
    		{
    			var first = ($(this).attr('term').charAt(0).toUpperCase());
    			while ( first > curLetter )
    			{
    				// bump the curLetter by one (NOTE: "@" + 1 ==>> "A")
    				curLetter = String.fromCharCode( curLetter.charCodeAt(0) + 1 );
    
    				if ( curLetter > "Z" ) break; // don't go past last letter (shouldn't happen)
    
    				// output a header for curLetter
    				$("#glossaryContent").append("<span class='glossLetter'>" + curLetter + "</span>"); 
    			}
    			$("#glossaryContent").append("<span class='glossAcro'>" + $(this).attr('term') + "</span>" + "<span class='glossDef'>" + $(this).attr('def') + "</span>"); // output from XML
    		} );

  • #7
    Regular Coder
    Join Date
    Jun 2012
    Posts
    137
    Thanks
    1
    Thanked 1 Time in 1 Post
    Nope. I missed something. The last 3 letters (x, y, z) do not show up. Let me play around with it.

  • #8
    Regular Coder
    Join Date
    Jun 2012
    Posts
    137
    Thanks
    1
    Thanked 1 Time in 1 Post
    There is probably a more elegant way to do this but here is my fix. I put thisafter the 'find' loop above:

    Code:
    while(curLetter.charCodeAt(0) < 90)
    		{
    			curLetter = String.fromCharCode( curLetter.charCodeAt(0) + 1 );
    			$("#glossaryContent").append("<span class='glossLetter'>" + curLetter + "</span>");
    		}

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    No, I think that's a fine way to do it.

    It's as short as I think you could make the code.

    Only thing I (personally) might change:
    Code:
    while ( curLetter <= "Z" )
    Just to be consistent with the other code. But it truly doesn't matter in the least.

    Great!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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