many else if conditional operator not working

Bandarbola855

i want it trigger when number is 0. when number is 1 or 2, when number is 3 or 4, when number is 5 or 6. when trigger it will change height some element. for now it always return 70px.

    if(y == 0){
        $('slpage').style.height = '0px';
    }else if(y >= 1){
        $('slpage').style.height = '70px';
    }else if(y <=4){
        $('slpage').style.height = '140px';
    }else if(y >= 5){
        $('slpage').style.height = '210px';
    }

thanks

Suresh Atta

You should consider reordering your statements. So that it checks the highest first and then for lowest.

if(y == 0){
        $('slpage').style.height = '0px';
    }else if(y >= 5){
        $('slpage').style.height = '210px';
    }else if(y >=4){
        $('slpage').style.height = '140px';
    }else if(y >= 1){
        $('slpage').style.height = '120px';
    }

Otherwise, you end up triggering the lowest always before reaching the highest.

And I guess you made a type for the check in your third condition. Corrected.

switch if the integer dependent. its means it should 0,1,2,3,4,5,6. but my situation is if 0. it trigger. if 1-2. it trigger. if 3-4. it trigger. if 5-6. it trigger.

Don't restrict yourself using switch just because you have to cover 2 cases at a time. Switch have that facility too.

switch (y) {
    case 0:
        $('slpage').style.height = '0px';
        break;
    case 1:
    case 2:
        $('slpage').style.height = '120px';
        break;
    case 3:
    case 4:
         $('slpage').style.height = '140px';
        break;
    case 5:
    case 6:
         $('slpage').style.height = '240px';
         break;
}

Assuming you using Jquery, your selector of Jquery is wrong. You are missing # or . Ignore if you are not.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Ternary Conditional Operator for else if

Conditional logical operator working with =

Using Conditional Operator for If..Else

Convert if/else to conditional (ternary) operator

Nullpointer exception with conditional operator, (ternary operator) but not with if else

Bash script conditional, else not working

Conditional Operator for Email Client not working

Complexity: Conditional operator vs if-else

Change a ternary conditional operator to regular if else statement

Double false for Inline If-Else with Conditional Operator

Is JavaScript Conditional Operator better method for this if/else statement?

Conditional Operator inside the tool tip is not working

Why conditional operator is working only for false value?

Conditional operator less than is not working in Vuejs

null conditional operator not working with nullable types?

The Conditional (Ternary) Operator is not working as expected(Javascript)

AWK ternary operator conditional statement not working

{{ !$last && ', ' || '.'}} is this working as ternary operator or if-else condition

Code working with ternary operator but not with if else statements

Using if else conditional operator in asp.net razor pages

What is the diffecence between these two if-else block and conditional operator block?

How can I use a conditional operator in printf but without the "else" part?

MVC Partial view is not rendering when use Conditional operator (IF..ELSE)

How Can I Use Ternary Conditional Operator Without Else?

Can conditional operator " ? : " be considered as an alternative to if-else statement in JavaScript?

Conditional rendering react native :only else part is working

Vue-if conditional not working. if-Else block appears to be skipped

Vuejs Ternary Operator / Conditional Not Working in v-bind-style

Regular expression in bash not working in conditional construct in Bash with operator '=~'

TOP Ranking

HotTag

Archive