How can this code be wrong

O

Otto Wyss

I've a simple if statement yet it executes even if condition is false.
What can be wrong?

var j = -1;
for (var i in files) {
var f = files.split('/', 1);
alert (j+','+i+','+(j == -1) || (folders[j] != f));
if ((j == -1) || (folders[j] != f)) {
alert (j+','+i);
j += 1;
folders[j] = f;
}
};

The second alert is shown even if the first shows condition = false.

O. Wyss
 
S

shimmyshack

I've a simple if statement yet it executes even if condition is false.
What can be wrong?

var j = -1;
for (var i in files) {
var f = files.split('/', 1);
alert (j+','+i+','+(j == -1) || (folders[j] != f));
if ((j == -1) || (folders[j] != f)) {
alert (j+','+i);
j += 1;
folders[j] = f;
}
};

The second alert is shown even if the first shows condition = false.

O. Wyss


how can j ever not be -1 in the code above, so the || condition is
always true, and when j is manually changed to say -2, the second
alert doesnt show.
 
W

wyo

if ((j == -1) || (folders[j] != f)) {
alert (j+','+i);
j += 1;
folders[j] = f;
}
The second alert is shown even if the first shows condition = false.

how can j ever not be -1 in the code above, so the || condition is
always true, and when j is manually changed to say -2, the second
alert doesnt show.- Hide quoted text -
j isn't alway -1 since after the first true condition j is set to 0 in
"j += 1". Besides the alert before really shows "false" for the full
condition.

O. Wyss
 
W

wyo

Figured out what was wrong, string compares works only with strings.

if ((j == -1) || (String(folders[j]) != String(f))) {

O. Wyss
 
L

-Lost

wyo said:
Figured out what was wrong, string compares works only with strings.

if ((j == -1) || (String(folders[j]) != String(f))) {

O. Wyss

For the record, what were you parsing? My guess was arrays you
populated via server-side code.
 
R

ron.h.hall

I've a simple if statement yet it executes even if condition is false.
What can be wrong?

var j = -1;
for (var i in files) {
var f = files.split('/', 1);
alert (j+','+i+','+(j == -1) || (folders[j] != f));
if ((j == -1) || (folders[j] != f)) {
alert (j+','+i);
j += 1;
folders[j] = f;
}
};

The second alert is shown even if the first shows condition = false.


You may wish to consider why the first alert:

alert (j+','+i+','+(j == -1) || (folders[j] != f));

gives a different result from:

alert (j+','+i+','+((j == -1) || (folders[j] != f)));

and decide which one would be the correct one to use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top