View Full Version : While Loop Question
07-19-2010, 05:03 PM
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.
while(localStorage.getItem(x) != null)
if(localStorage.getItem(x) == null)
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.
07-19-2010, 06:06 PM
I think this fits what you're intending:
while( localStorage.getItem( x++ ) != null )
localStorage.setItem( x, account.join(";") );
07-19-2010, 06:32 PM
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.
while($("#username").val() +";"+ $("#password").val() != localStorage.getItem(x));
if($("#username").val() +";"+ $("#password").val() == localStorage.getItem(x))
alert("Account Already Listed");
It includes jQuery and its confusing that's why I was asking more generally. This is what I intend:
while(username+password is not in localStorage)
if(username+password is in localstorage)
alert("account in storage");
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.
07-19-2010, 06:43 PM
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.
07-19-2010, 08:20 PM
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.
07-19-2010, 09:00 PM
Something along these lines although I'm not certain the logic fits your need:
var inUse = false;
while( x < localStorage.length && ( inUse = localStorage.getItem( x++ ) ) == null )
if( inUse )
alert( "Key aready in use" );
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.