Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to get if a negative number is in a range

Posted on 2014-09-12
3
Medium Priority
?
87 Views
Last Modified: 2014-09-12
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";


}

Open in new window

0
Comment
Question by:Johnny
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
ChloesDad earned 2000 total points
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

by:Johnny
ID: 40320247
Thank you kind sir
0
 
LVL 15

Expert Comment

by:ChloesDad
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
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

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question