javascript - Operators not working? -
ok did according suggestion looks amatuerish ha.. cos repeats same thing each percentage group: css , message. wondering if there way change it? if not, ok 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'); }
you're checking percentdiscount
that's both higher both numbers, in second if
check, there no numbers left above 69. should instead (keeping 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 terms they're in same order have above, think you'll find it's much easier read. however, overall terms exclude 69
, , 49
points specifically, i'd change logic 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 above 69, next above, 49, etc...much simpler :)
Comments
Post a Comment