admin管理员组文章数量:1414604
ok i did according to your suggestion but somewhat it looks amatuerish ha.. cos it repeats the same thing for each percentage group: css and message. I am wondering if there is another way to change it? If not, i am ok with this..
if (69 < percentDiscount && percentDiscount < 101) {
$(this).find("#percentoff").html('> 70% off');
$(this).find("#percentoff").addClass('badge70');
}
else if (49 < percentDiscount && percentDiscount < 70) {
$(this).find("#percentoff").html('> 50% off');
$(this).find("#percentoff").addClass('badge50');
}
else if (29 < percentDiscount && percentDiscount < 50) {
$(this).find("#percentoff").html('> 30% off');
$(this).find("#percentoff").addClass('badge30');
}
else if (19 < percentDiscount && percentDiscount < 30) {
$(this).find("#percentoff").html('> 20% off');
$(this).find("#percentoff").addClass('badge30');
}
ok i did according to your suggestion but somewhat it looks amatuerish ha.. cos it repeats the same thing for each percentage group: css and message. I am wondering if there is another way to change it? If not, i am ok with this..
if (69 < percentDiscount && percentDiscount < 101) {
$(this).find("#percentoff").html('> 70% off');
$(this).find("#percentoff").addClass('badge70');
}
else if (49 < percentDiscount && percentDiscount < 70) {
$(this).find("#percentoff").html('> 50% off');
$(this).find("#percentoff").addClass('badge50');
}
else if (29 < percentDiscount && percentDiscount < 50) {
$(this).find("#percentoff").html('> 30% off');
$(this).find("#percentoff").addClass('badge30');
}
else if (19 < percentDiscount && percentDiscount < 30) {
$(this).find("#percentoff").html('> 20% off');
$(this).find("#percentoff").addClass('badge30');
}
Share
Improve this question
edited Nov 18, 2010 at 4:48
joe
asked Nov 16, 2010 at 10:33
joejoe
1,1355 gold badges22 silver badges50 bronze badges
4 Answers
Reset to default 5You've got the > round the wrong way. Let's look at the first one as an example:
if (percentDiscount > 69 && percentDiscount > 101)
(I reversed the first condition because I think it makes what's next more obvious.)
So percentDiscount has to be greater than 69 AND greater than 101. You want
if (69 < percentDiscount && percentDiscount < 101)
This should do what you expect.
You're checking for a percentDiscount
that's both higher than both numbers, so the in your second if
check, there are no numbers left that are also above 69. It should look like this instead (keeping your exclusion logic):
if (percentDiscount > 69 && percentDiscount < 101) {
$(this).find("#percentoff").html('> 70% off');
$(this).find("#percentoff").addClass('badge70');
}
else if (percentDiscount > 49 && percentDiscount < 69) {
$(this).find("#percentoff").html('> 50% off');
$(this).find("#percentoff").addClass('badge50');
}
else if (percentDiscount > 29 && percentDiscount < 49) {
$(this).find("#percentoff").html('> 30% off');
$(this).find("#percentoff").addClass('badge30');
}
Swap the terms so they're in the same order like I have above, I think you'll find it's much easier to read. However, overall your terms exclude the 69
, and 49
points specifically, so I'd change your logic to this:
if (percentDiscount > 69) {
$(this).find("#percentoff").html('> 70% off');
$(this).find("#percentoff").addClass('badge70');
}
else if (percentDiscount > 49) {
$(this).find("#percentoff").html('> 50% off');
$(this).find("#percentoff").addClass('badge50');
}
else if (percentDiscount > 29) {
$(this).find("#percentoff").html('> 30% off');
$(this).find("#percentoff").addClass('badge30');
}
The first if
grabs those above 69, the next those above, 49, etc...much simpler :)
In the first test, you are testing whether percentDiscount is greater than 69 and greater than 101. I think you meant this:
if (69 < percentDiscount && percentDiscount < 101) ...
The remaining tests have the same problem.
Take a look at this line:
if (69 < percentDiscount && percentDiscount > 101) {
There are two problems here. First: you're doing the parisons wrong. From this if statement, you're checking if percentDiscount
is bigger than 69 and if it's bigger than 101. So, this will hold only if it's bigger than 101. What you probably wanted is this:
if (69 < percentDiscount && percentDiscount < 101) {
This can be written better as:
if (percentDiscount > 69 && percentDiscount < 101) {
But, there's still one problem. Operator precedence. The last example should be written as
if ( (percentDiscount > 69) && (percentDiscount < 101) ) {
to avoid ambiguities.
本文标签: javascriptOperators not workingStack Overflow
版权声明:本文标题:javascript - Operators not working? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745194048a2647050.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论