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
5 Answers 5
(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
Alexandru Severin
6,46812 gold badges54 silver badges79 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
JasonWilczak
2,4132 gold badges22 silver badges39 bronze badges
Comments
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
Roy M J
6,9367 gold badges54 silver badges79 bronze badges
1 Comment
Sergio Tulentsev
You don't need to check lower bound if you just checked it as an upper bound in a case above.
There are 3 updates.
- there should be no semi-colon after if statements.
- You should use if-else for the above conditions.
- Your
if (width > 961 && width >1135){has a problem with second condition.
answered Jul 17, 2015 at 17:13
Nikhil Aggarwal
28.5k4 gold badges47 silver badges61 bronze badges
Comments
Here's your problem:
if (width > 961 && width > 1135){
subpageModifier = 0.3;
};
Both are> symbols
answered Jul 17, 2015 at 17:15
durbnpoisn
4,6792 gold badges19 silver badges30 bronze badges
Comments
lang-js
else-if?width >1135. Incidentally, if your value is exactly767,800, etc, none of these will work.