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 6 of 6
  1. #1
    New Coder
    Join Date
    May 2010
    Posts
    23
    Thanks
    9
    Thanked 0 Times in 0 Posts

    While Loop Question

    So I am wanting to loop through a set of data an undefined amount of times. For this I need a while loop. However, I want the loop to check for the given condition and continue looping on true, but when it breaks on false - to then execute a different command.

    Code:
    while(localStorage.getItem(x) != null)
    	{
    	x++;
    		if(localStorage.getItem(x) == null)
    		{
    		  localStorage.setItem(x, account.join(";"));
    		  break;
    		}
    	}
    }
    This works by continuing to loop everytime with the x++ and the nested if statement inside. The problem gets messier as now I need to perform a separate while loop around the entire thing checking for a different condition.

    To sum things up, what is the most efficient way to perform a while loop and have one action be performed on success and another if it breaks or reaches a defined condition.

  • #2
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    I think this fits what you're intending:
    Code:
    while( localStorage.getItem( x++ ) != null )
    ;
    
    localStorage.setItem( x, account.join(";") );

  • #3
    New Coder
    Join Date
    May 2010
    Posts
    23
    Thanks
    9
    Thanked 0 Times in 0 Posts
    While that code does work, it is not what I am looking for. the .getItem() will return null if the key does not exist. My code tests for that correctly. The problem I am having is before I even get to that, I want a separate loop that tests the inputed username+password and checks to see that it has not already been submitted.

    Something like:

    Code:
    while($("#username").val() +";"+ $("#password").val() != localStorage.getItem(x));
    		{
    			if($("#username").val() +";"+ $("#password").val() == localStorage.getItem(x))
    			{
    			break;
    			alert("Account Already Listed");
    			}
    			x++;
    		}
    It includes jQuery and its confusing that's why I was asking more generally. This is what I intend:

    Code:
    while(username+password is not in localStorage)
    {  
      if(username+password is in localstorage)
      {
      break;
      alert("account in storage");
      }
      x++
    }
    I can't get it to break correctly and it continues looping. Maybe I am ordering or wording it wrong. The while loop does good while the condition is true, but I want a specified action to happen when it fails.

  • #4
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Your algorithm doesn't really make sense.

    If you want to check for a key in an array you must scan the entire array and stop when either you find the tested value or you reach the end, so both those conditions must be combined in the loop's test condition.

  • #5
    New Coder
    Join Date
    May 2010
    Posts
    23
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Having both conditions sounds like a good idea, I just have no idea how to accomplish that. It seems that we have figured out how to test for the specified value. But do you have an idea of how you would stop at the end?

    The problem is that you cannot use a for statement without a defined start and end point because the number of items(username and passwords) is dynamic. A test for localStorage.length would return the number of items. A for loop could be used then with a distinct number of keys as the endpoint. hmm

    The only other question would be how to include both of those in the conditional statement

    ... I am just thinking out loud and I figure if I do it here, anybody's input could only help.

  • #6
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Something along these lines although I'm not certain the logic fits your need:
    Code:
    var inUse = false;
    
    while( x < localStorage.length && ( inUse = localStorage.getItem( x++ ) ) == null  )
    ;
    
    if( inUse )
     alert( "Key aready in use" );

  • Users who have thanked RandomUser531 for this post:

    swieder (07-19-2010)


  •  

    Posting Permissions

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