Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# how to get if a negative number is in a range

Posted on 2014-09-12
Medium Priority
86 Views
i have a number that will sometimes fall below 0 (negative) how do i check for that give like this please
``````// \$percent_color=getColor(-0.4);
function getColor(\$n){

// Return default (black) for all other numbers
if(\$n==0) return "black";

// Is number between 1 and 5?
if(\$n>=0.1 && \$n<=10) return "green";

// Is number between 6 and 50?
if(\$n>=11 && \$n<=20) return "orange";

// Is number greater than 50
if(\$n>21) return "red";

// negitive lookup

// Is number between 1 and 5?
if(\$n>=-0.1 && \$n<=-10) return "green";

// Is number between 6 and 50?
if(\$n>=-11 && \$n<=-20) return "orange";

// Is number greater than 50
if(\$n>-21) return "red";

}
``````
0
Question by:Johnny
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 15

Accepted Solution

ID: 40320232
This bit looks wrong, you have the logic the wrong way around. I have corrected

// Is number between 1 and 5?
if(\$n<0 && \$n>=-10) return "green";

// Is number between 6 and 50?
if(\$n<-11 && \$n>=-20) return "orange";

// Is number greater than 50
if(\$n<-20) return "red";
0

Author Closing Comment

ID: 40320247
Thank you kind sir
0

LVL 15

Expert Comment

ID: 40320273
Thanks

this bit I edited wrong

// Is number between 6 and 50?
if(\$n<-11 && \$n>=-20) return "orange";

should be

// Is number between 6 and 50?
if(\$n<-10 && \$n>=-20) return "orange";

otherwise you will miss if the value is -11
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
###### Suggested Courses
Course of the Month7 days, 3 hours left to enroll