Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-19-2010, 04:03 PM   PM User | #1
swieder
New Coder

 
Join Date: May 2010
Posts: 23
Thanks: 9
Thanked 0 Times in 0 Posts
swieder is an unknown quantity at this point
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.
swieder is offline   Reply With Quote
Old 07-19-2010, 05:06 PM   PM User | #2
RandomUser531
New Coder

 
Join Date: Jul 2010
Posts: 61
Thanks: 0
Thanked 21 Times in 21 Posts
RandomUser531 is on a distinguished road
I think this fits what you're intending:
Code:
while( localStorage.getItem( x++ ) != null )
;

localStorage.setItem( x, account.join(";") );
RandomUser531 is offline   Reply With Quote
Old 07-19-2010, 05:32 PM   PM User | #3
swieder
New Coder

 
Join Date: May 2010
Posts: 23
Thanks: 9
Thanked 0 Times in 0 Posts
swieder is an unknown quantity at this point
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.
swieder is offline   Reply With Quote
Old 07-19-2010, 05:43 PM   PM User | #4
RandomUser531
New Coder

 
Join Date: Jul 2010
Posts: 61
Thanks: 0
Thanked 21 Times in 21 Posts
RandomUser531 is on a distinguished road
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.
RandomUser531 is offline   Reply With Quote
Old 07-19-2010, 07:20 PM   PM User | #5
swieder
New Coder

 
Join Date: May 2010
Posts: 23
Thanks: 9
Thanked 0 Times in 0 Posts
swieder is an unknown quantity at this point
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.
swieder is offline   Reply With Quote
Old 07-19-2010, 08:00 PM   PM User | #6
RandomUser531
New Coder

 
Join Date: Jul 2010
Posts: 61
Thanks: 0
Thanked 21 Times in 21 Posts
RandomUser531 is on a distinguished road
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" );
RandomUser531 is offline   Reply With Quote
Users who have thanked RandomUser531 for this post:
swieder (07-19-2010)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:00 PM.


Advertisement
Log in to turn off these ads.