How can this code be wrong

Discussion in 'Javascript' started by Otto Wyss, Apr 26, 2007.

  1. Otto Wyss

    Otto Wyss Guest

    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
    Otto Wyss, Apr 26, 2007
    #1
    1. Advertising

  2. Otto Wyss

    shimmyshack Guest

    On Apr 26, 8:00 pm, Otto Wyss <> wrote:
    > 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.
    shimmyshack, Apr 26, 2007
    #2
    1. Advertising

  3. Otto Wyss

    wyo Guest

    On Apr 26, 11:53 pm, shimmyshack <> wrote:
    > On Apr 26, 8:00 pm, Otto Wyss <> wrote:
    >
    > > 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.- 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
    wyo, Apr 27, 2007
    #3
  4. Otto Wyss

    wyo Guest

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

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

    O. Wyss
    wyo, Apr 27, 2007
    #4
  5. Otto Wyss

    -Lost Guest

    wyo wrote:
    > 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.

    --
    -Lost
    Remove the extra words to reply by e-mail. Don't e-mail me. I am
    kidding. No I am not.
    -Lost, Apr 27, 2007
    #5
  6. Otto Wyss

    Guest

    On Apr 26, 12:00 pm, Otto Wyss <> wrote:
    > 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.

    --
    ../rh
    , Apr 27, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew
    Replies:
    7
    Views:
    642
    Priscilla Walmsley
    Jan 7, 2005
  2. David. E. Goble
    Replies:
    9
    Views:
    453
    David. E. Goble
    Feb 2, 2005
  3. Taras_96
    Replies:
    3
    Views:
    311
    Taras_96
    Jan 29, 2008
  4. Replies:
    14
    Views:
    795
    Mike Treseler
    Jan 10, 2009
  5. kiran
    Replies:
    12
    Views:
    1,079
    Scott Sauyet
    Dec 7, 2011
Loading...

Share This Page