0

I have question about else if in my case last else not working

var pakeEach = $(".Thide").each(function () {
    var cat = $(this).val();
    if (cat.indexOf(PCatId) == -1) {
        console.log("Pilih tidak ada di your aksess")
    } else {
        if (cat.indexOf(PCatId) != -1) {
            console.log("Show only " + cat);
        } else {
            console.log("Remove");//why not working last else
        }
    }
});
b4dQuetions
  • 1,549
  • 6
  • 18
  • 28

4 Answers4

1

Starting with your logic:

if (value == -1) {
    ...
} else {
    if (value != -1) {
        ...
    } else {
        ...
    }
}

you can simplify it to:

if (value == -1) {
    ...
} else if (value != -1) {
    ...
} else {
    ...
}

As you can see, you're saying "if value is -1 then ..., otherwise if value is not -1..." That covers both possible circumstances: value cannot be anything but -1 or not -1 (as discussed by A. Wolff in the comments).

The else can never be hit, since all possible branches have already been covered.

If you want to include the remove logic in one of the other branches (probably the branch where the item was found), you should use something like:

if (value === -1) {
    console.log("Pilih tidak ada di your aksess")
} else {
    console.log("Show only " + cat);
    console.log("Remove!");
}

This is a much more concise way of writing the same thing. Note that the === operator is generally preferred over ==, especially when you know you'll be comparing objects of the same type.

Because you are calling indexOf, the only possible result is the item is in the collection (indexOf(foo) > -1) or not in the collection (indexOf(foo) === -1). There is no third option here, unless you care about where in the collection it is.

For example, to remove the item if it is not the first item present in the collection, you would use:

if (value === -1) {
    console.log("Pilih tidak ada di your aksess")
} else if (value === 0) {
    console.log("Show only " + cat);
} else {
    console.log("Remove!");
}

Because the if conditions only match a small subset of the possible values, the else can actually be hit in this case.

ssube
  • 47,010
  • 7
  • 103
  • 140
0

Try this:

    var pakeEach = $(".Thide").each(function () {
    var cat = $(this).val();
    if (cat.indexOf(PCatId) == -1) 
    {
        console.log("Pilih tidak ada di your aksess");
         console.log("Remove");
    } 
    else 
    {
            console.log("Show only " + cat);
    } 
});
Sami
  • 572
  • 8
  • 22
0

What you are trying to do is this:

var pakeEach = $(".Thide").each(function () {
    var cat = $(this).val();
    if (!cat) {
        console.log("Pilih tidak ada di your aksess")
    } else if (cat === PCatID) {
        console.log("Show only " + cat);
    } else {
        console.log("Remove");
    }
});

First check if cat is empty, then branch according to its value.

Joanis
  • 1,669
  • 3
  • 20
  • 32
  • Hi Joanis, I want just cheking if(cat == PCatId){display)else(remove) but if(cat noting in PCatid){Display All) – b4dQuetions May 04 '15 at 17:35
  • How about `if (!cat) { display all } else if (cat.localeCompare(PCatID) == 0) { display } else { remove }` ? The first if checks if `cat` is empty, the second checks if cat == PCatID. – Joanis May 04 '15 at 17:48
  • Check http://stackoverflow.com/questions/2167602/optimum-way-to-compare-strings-in-javascript and http://stackoverflow.com/questions/154059/how-do-you-check-for-an-empty-string-in-javascript – Joanis May 04 '15 at 17:50
  • I updated my answer to reflect my new understanding of your problem. I'm using `===` to compare strings instead of `localCompare`, as this seems to be more frequently used. – Joanis May 04 '15 at 18:02
0

Algorithmically you are saying if cat.indexOf(PCatId) is equal to -1 then execute the first if block

else execute the second block which is logically treating the case where cat.indexOf(PCatId) is different then -1.

I mean in the first else you can never ever get cat.indexOf(PCatId) == -1 because you are catching it in the first if here is your answer with some modifications and comments :

var pakeEach = $(".Thide").each(function () {
    var cat = $(this).val();
    if (cat.indexOf(PCatId) == -1) {
        // this part of code is executed if cat.indexOf(PCatId) == -1
        console.log("Pilih tidak ada di your aksess")
    } else {
        // here at this level we can never get cat.indexOf(PCatId) == 1
        if (cat.indexOf(PCatId) != -1) {
            console.log("Show only " + cat);
        }
        // this else should be removed !!!! because it is useless
        else {
            console.log("Remove");//why not working last else
        }
    }
});
ismnoiet
  • 4,129
  • 24
  • 30