0

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

asked Jul 17, 2015 at 17:09
2
  • Why not chain these in an else-if? Commented Jul 17, 2015 at 17:10
  • 2
    Typo here: width >1135. Incidentally, if your value is exactly 767, 800, etc, none of these will work. Commented Jul 17, 2015 at 17:12

5 Answers 5

4
(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

answered Jul 17, 2015 at 17:11
Sign up to request clarification or add additional context in comments.

Comments

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

should be less than 1135:

if (width > 961 && width < 1135){
 subpageModifier = 0.3;
};
answered Jul 17, 2015 at 17:11

Comments

2

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
}
answered Jul 17, 2015 at 17:13

1 Comment

You don't need to check lower bound if you just checked it as an upper bound in a case above.
1

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.
answered Jul 17, 2015 at 17:13

Comments

1

Here's your problem:

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

Both are> symbols

answered Jul 17, 2015 at 17:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.