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.