Katerin1122
Katerin1122

Reputation: 75

Javascript conditions issues

I have a situation in my code where I have several conditions for 3 variables, let's say a, b and c. I've implemented else if conditions for each case when one of three is false and for when all three are true and for when all three are false. But the problem is that I need to implement conditions also for when 2 out of 3 are false and following the order of a, b and c I need to show specific message for the first in order that is false. For example, a becomes false, then b becomes false and c remains true, I need to show a specific message for a false. Other case: when b becomes false then a becomes false and c remains true, I need to show a specific message for a false, when c becomes false and b becomes false, I need to show a specific message for b. How can this be achieved?

Code example

if (a && b && c) {
      setOrder(format(order));
    } else if (!a && b && c) {
      setOrder("orderAlternateNumber");
    } else if (!b && a && c) {
      setOrder("orderNewNumber");
    } else if (!c && a && b) {
      setOrder("someOtherNumber");
    } else if (!c && !a && !b) {
      setOrder("");
    } 

Upvotes: 0

Views: 72

Answers (3)

3limin4t0r
3limin4t0r

Reputation: 21130

I am not entirely sure what your scenario is, you example is somewhat vague because you don't give a lot information about your actual issue. However based on:

For example, a becomes false, then b becomes false and c remains true, I need to show a specific message for a false.

I would suggest the following. Instead of checking all combinations a, b and c. You can also check them separately.

let error;
if (!a) error ??= "something wrong with a";
if (!b) error ??= "something wrong with b";
if (!c) error ??= "something wrong with c";

if (error) {
  // do something with error message
} else {
  // handle success
}

The above stores a single error message depending on the first error it encounters. ??= only assigns a value if the current value if nullish. But you could also collect them by storing them in an array.

const errors = [];
if (!a) errors.push("something wrong with a");
// ...
if (errors.length != 0) {

This might not be what your looking for, it's hard to say what is since you withhold your actual scenario.

Upvotes: 1

georg
georg

Reputation: 214969

You can combine these variables into a single number with ones and zeroes and use a simple dictionary lookup for the order parameter:

let n = 1000 + (a * 100) + (b * 10) + (c * 1)

order = {
    1111: format(order), // true,true,true,
    1011: "orderAlternateNumber", // false,true,true
    1101: "orderNewNumber", // true,false,true etc...
    1110: "someOtherNumber",
    1000: '',
}

setOrder(order[n])

Upvotes: 2

Sagi Rika
Sagi Rika

Reputation: 2979

On the function that changes a, b and c, you need to have a condition.

If one of them is going to be set to false, you have to check if another one, and only one is already false. If there is another false, you can set the message according to this one and commit the change for the second parameter to be false.

If im not clear, you might want to add code example for the function that controls a, b and c for my better understanding.

Upvotes: 0

Related Questions