1

I making a condition in javascript that if a value or b value is greater than 59 and c1, c2, c3, c4 any of these are greater than 21 than my condition will be true. It's working fine but when I entering a value 50 b value 30 and c1 value 25 it is going to else statement.

Here is the code:

if((a > 59) || (b > 59) &&  (c1 > 21) &&  (c2 > 21) &&  (c3 > 21) &&  (c4 > 21)){
    total = a + b + c1 + c2 + c3 + c4;
}
    else {

    }

Can you guyz help me out how to fulfill my conditions?

5
  • 2
    if b is 30, then (b > 59) is false, also (a > 59) is false since a is 50 - why are you surprised you end up in the else branch? Commented May 20, 2018 at 12:12
  • @UnholySheep I am making this statement like a or b should be greater than 59 and c1,c2,c3 or c4 should be greater than 21 than it would be true statement Commented May 20, 2018 at 12:14
  • 1
    At the moment, you're checking that ALL of c1, c2, c3, and c4 are greater than 21 because you're using the AND operator Commented May 20, 2018 at 12:15
  • Instead of placing each test in parenthesis (which doesn't add any value to the statement), use parenthesis around each separate condition. For example, if((a > 59) || (b > 59) && ... should be: if((a > 59 || b > 59) && ... This will help you understand your conditions more easily and it will create more accurate execution. Commented May 20, 2018 at 12:19
  • As a sidenote, for this kind of checks Math comes in handy: Math.max(a, b) > 59 && Math.max(c1, c2, c3, c4) > 21 - in case you really meant to check all cX values for a minimum of 21: Math.max(a, b) && Math.min(c1, c2, c3, c4) > 21 Commented May 20, 2018 at 12:21

2 Answers 2

3

As per your comment

@UnholySheep I am making this statement like a or b should be greater than 59 and c1,c2,c3 or c4 should be greater than 21 than it would be true statement

This is what you are actually looking for:

(a > 59 || b > 59) && (c1 > 21 || c2 > 21 || c3 > 21 || c4 > 21)

This is how it works:

  • (a > 59 || b > 59) check whether a or b is greater than 59
  • (c1 > 21 || c2 > 21 || c3 > 21 || c4 > 21) is true if one of them is greater than 21
  • the && operator checks if both expressions are true, only in that case it returns true

let a=60,b=30,c1=30;

console.log((a > 59 || b > 59) && (c1 > 21 || c2 > 21 || c3 > 21 || c4 > 21));

Sign up to request clarification or add additional context in comments.

5 Comments

"This is what you are actually looking for" Indeed -- but why? Explanations can be really powerful.
@T.J.Crowder I was working on it just now, thanks for the reminder though!
Better to wait until your answer is complete before posting, rather than finishing it afteward.
@T.J.Crowder I just tried it and I gave a value 50 b value 30 and c1 value 30 it is going to else statement
@Jason Because 50 < 59. Check this with something like a=60,b=30,c1=30
0

Assuming your expectations are right, then I would say your statement is wrong as per your expectations.

"I am making this statement like a or b should be greater than 59 and c1,c2,c3 or c4 should be greater than 21 than it would be true statement."

The "and" in your statment below is causing your expectations to fail. The first part "a or b should be greater than 59" is evaluating it false when a is 50 and b is 30.

The second part "c1, c2, c3 or c4 should be greater than 21" is evaluating to true when c1 is 25.

So, your whole condition expression inside the if(...) is boiling down to: false and true. This would always result in false thats why your control flow is going in else part.

You need to either correct your statement or your expectations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.