简体   繁体   中英

Javascript if statement conditions not met?

i'm sure this is a stupid logic error but I've been unable to see what i've done wrong. while using any values besides 1024 the correct if statement fires, for some reason, the 1024 condition isn't met. thanks in advance!

var width = 1024; //screen.width;
var height = 768; //screen.height;
var subpageModifier;

if (width > 360 && width < 767) {
    subpageModifier = .28;
};
if( width > 767 && width < 800){
    subpageModifier = 0.15;
};
if (width > 800 && width < 961) {
    subpageModifier = .15;
}; 
if (width > 961 &&  width >1135){
    subpageModifier = 0.3;
};
if (width >1135 && width < 1535){
    subpageModifier = 0.15;
};

Note: I'm using AngularJS if that helps

(width > 961 &&  width >1135)

Second > should be < .

(width > 961 &&  width <1135)

PS: Use if-else , there is no need to check every condition if you found the match

if (width > 961 &&  width >1135){
    subpageModifier = 0.3;
};

should be less than 1135:

if (width > 961 &&  width < 1135){
    subpageModifier = 0.3;
};

Instead of

if (width > 961 &&  width >1135){

Use:

if (width > 961 &&  width < 1135){

Ideally everything should be:

var width = 1024; //screen.width;
var height = 768; //screen.height;
var subpageModifier;

if (width >= 360 && width <= 767) {
    subpageModifier = .28;
}else if( width > 767 && width <= 800){
    subpageModifier = 0.15;
}else if (width > 800 && width <= 961) {
    subpageModifier = .15;
}else if (width > 961 &&  width <= 1135){
    subpageModifier = 0.3;
}else if (width >1135 && width <= 1535){
    subpageModifier = 0.15;
}else{
    //do something
}

There are 3 updates.

  1. there should be no semi-colon after if statements.
  2. You should use if-else for the above conditions.
  3. Your if (width > 961 && width >1135){ has a problem with second condition.

Here's your problem:

if (width > 961 &&  width > 1135){
    subpageModifier = 0.3;
};

Both are > symbols

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM