Click to See Complete Forum and Search --> : does a if condition within a for loop only get executed at the end of the for loop?


K Lo
11-21-2003, 04:59 AM
Hi, can anyone tell me what is wrong with the following code:
function ValidateAgainstList(field1){
ListA= GetFieldValue("DisplayStaffinCustomer");
ListAArray = ListA.split("\n");
var maxX = ListAArray.length;
var z;

for (z=0 ; z < maxX ; z++) {
if (field1.toLowerCase() ==
ListAArray[z].toLowerCase() )
{
alert('duplicate')
break;
}
}
}


the value of field1 is Kevin; and the ListA values returned are Kevin;Paul;Steve,
but with this function, i can never get my alert statement?

Is it true that the if condition within a for loop only get executed at the end of the for loop?
anyone has any idea to solve this problem?

Thanks in advance for any help!

Gollum
11-21-2003, 05:07 AM
No, the condition to a for loop is executed first, before entering the loop code, but that's not your problem...

Unless I've understood things wrong, the problem lies in the fact that

"Kevin;Paul;Steve".split("\n") == ["Kevin;Paul;Steve"]

in other words, you have an array of one string that is certainly not equal to "Kevin".

K Lo
11-21-2003, 05:37 AM
but i have tried a alert statment after entering the for loop:
the for loop is executed three times and the first alert shows me "Kevin", second alert shows me "Paul" and the last alert shows me "Steve"

One more strange thing is that if ListAArray happens to be "Paul; Steve; Kevin", then i will get my alert('Duplicate
) statement working..

Strange...

Gollum
11-21-2003, 06:23 AM
can you squeeze this problem down to a small (but complete) example and post that? It's difficult to see what is going wrong with the code you have posted.

K Lo
11-21-2003, 08:32 AM
ok see if i can explain further. i am calling this function from a button in a Lotus Domino form.

the field1 is Kevin and the ListA returns Kevin;Paul;Steve
So from my first 2 alerts, i always get the following prompt:
Kevin, Kevin
Kevin, Paul
Kevin, Steve
by right, i shall be getting the "Duplicate" msg on the first comparison but i never do.

*****************
function ValidateAgainstList(field1){
ListA= GetFieldValue("DisplayStaffinCustomer");
ListAArray = ListA.split("\n");
var maxX = ListAArray.length;
var z;

for (z=0 ; z < maxX ; z++) {
alert(field1);
alert(ListAArray[z]);

if (field1.toLowerCase() ==
ListAArray[z].toLowerCase() )
{
alert('duplicate')
break;
}
}
}