0

I am trying to make a grading scales for a grouping of variables. I'm running in to a few problems and am completely brain dead at the moment. Could anyone possibly give me some help on how to accomplish this? Very much appreciated!

function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
if ($talent || $physical || $entertainment || $reputation || $overness >= 90) {
return "Grade: A";
} elseif ($talent || $physical || $entertainment || $reputation || $overness >= 80) {
return "Grade: B";
} elseif ($talent || $physical || $entertainment || $reputation || $overness >= 70) {
return "Grade: C";
} elseif ($talent || $physical || $entertainment || $reputation || $overness >= 60) {
return "Grade: D";
} elseif ($talent || $physical || $entertainment || $reputation || $overness >= 50) {
return "Grade: E";
} elseif ($talent || $physical || $entertainment || $reputation || $overness <= 49) {
return "Grade: F";
} else {
return "N/A";
}
}

echo ovr_grade();
5
  • 2
    what exactly is the problem? do you want to check if either one of the variables is above 90, 80, etc? You are just checking the existence of all variables except $overness Commented Jul 18, 2013 at 3:48
  • 2
    your not setting your function arguments in your function call. Commented Jul 18, 2013 at 3:49
  • 4
    You are comparing $talent,$physical,$entertainment,$reputation as a boolean value. You should restructure the code to ($talent >= 90 || $physical >= 90 || $entertainment >= 90 || $reputation >= 90 || $overness >= 90) Commented Jul 18, 2013 at 3:49
  • Surely there's a more elegant way of doing this than a huge if statement. Commented Jul 18, 2013 at 4:09
  • Most of these worked, however I was looking for a way to display each grade by themselves. Basically echo out a grade for $talent, one for $physical, etc. It seems as though it is group them and I can't call just one. Any suggestions? Commented Jul 18, 2013 at 9:46

8 Answers 8

2

try:

if (max($talent,$physical,$entertainment,$reputation) >= 90 )
{
       return "Grade: A";
}
elseif ...........
Sign up to request clarification or add additional context in comments.

2 Comments

or store the $max = max($talent,$physical,$entertainment,$reputation) in a variable so you have if($max >= 90){...}elseif($max >= 80) :)
This worked, however I was looking for a way to display each grade by themselves. Basically echo out a grade for $talent, one for $physical, etc. It seems as though it is group them and I can't call just one. Any suggestions?
1
function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
    $values = array($talent, $physical, $entertainment, $reputation, $overness);
    $average = array_sum($values) / count($values);
    if ($average >=90)
        return "Grade: A";
    if ($average >=80)
        return "Grade: B";
    if ($average >=70)
        return "Grade: C";
    if ($average >=60)
        return "Grade: D";
    if ($average >=50)
        return "Grade: E";
    if ($average <=49)
        return "Grade: F";
    else
        return "N/A";
}

Comments

0

i aaprreciate "James Anderson" Answer so +1 him,but max() calculation can be reduced

$grades=  max($talent,$physical,$entertainment,$reputation);

if($grades>90)
{
return "Grade: A";
} 
elseif ($grades>80)
{
return "Grade: B";
} 
.
.
.
.
.
.
else 
{
return "N/A";
}

update:

unfortunately i cant get deep in to your "comment" anyway , i think you need a general function that returns grade for each factor (eg:physical,entertainment ... )

function  getGrades($factor)
{

$grades= $factor;

if($grades>90)
{
return "Grade: A";
} 
elseif ($grades>80)
{
return "Grade: B";
} 
.
.
.
.
.
.
else 
{
return "N/A";
}
}

and make call to get grade for each

echo "Talent : " . getGrades($talent);
echo "Physical : " . getGrades($physical);

1 Comment

This worked, however I was looking for a way to display each grade by themselves. Basically echo out a grade for $talent, one for $physical, etc. It seems as though it is group them and I can't call just one. Any suggestions?
0

I think you should restructure your conditions to this:

function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
if ($talent >= 90 || $physical >= 90 || $entertainment >= 90 || $reputation >= 90 || $overness >= 90) {
return "Grade: A";
} elseif ($talent >= 80 || $physical >= 80 || $entertainment >= 80 || $reputation >= 80 || $overness >= 80) {
return "Grade: B";
} elseif ($talent >= 70 || $physical >= 70 || $entertainment >= 70 || $reputation >= 70 || $overness >= 70) {
return "Grade: C";
} elseif ($talent >= 60 || $physical >= 60 || $entertainment >= 60 || $reputation >= 60 || $overness >= 60) {
return "Grade: D";
} elseif ($talent >= 50 || $physical >= 50 || $entertainment >= 50 || $reputation >= 50 || $overness >= 50) {
return "Grade: E";
} elseif ($talent <= 49 || $physical <= 49 || $entertainment <= 49 || $reputation <= 49 || $overness <= 49) {
return "Grade: F";
} else {
return "N/A";
}
}

echo ovr_grade(90,90,90,90,90);//input their respective values

So every value will compare to a numerical one and not by boolean.

2 Comments

you have to fix the function call too
that too. will revise.
0

First of all, you wrote few details about your requirements about the outcome you want from this function. I checked your function and it have some major condition problem.

Now, if your requirement is to show "Grade: A" if someone gets 90 in any category (e.g, 90 in $talent or 90 in $physical or 90 in $entertainment etc.) then the function you wrote is working fine. All you need to do is initialize the function like:

function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
if ($talent >= 90 || $physical >= 90 || $entertainment >= 90 || $reputation >= 90 || $overness >= 90) {
return "Grade: A";
} elseif ($talent >= 80 || $physical >= 80 || $entertainment >= 80 || $reputation >= 80 || $overness >= 80) {
return "Grade: B";
} elseif ($talent >= 70 || $physical >= 70 || $entertainment >= 70 || $reputation >= 70 || $overness >= 70) {
return "Grade: C";
} elseif ($talent >= 60 || $physical >= 60 || $entertainment >= 60 || $reputation >= 60 || $overness >= 60) {
return "Grade: D";
} elseif ($talent = 50 || $physical = 50 || $entertainment = 50 || $reputation = 50 || $overness >= 50) {
return "Grade: E";
} elseif ($talent <= 49 || $physical <= 49 || $entertainment <= 49 || $reputation <= 49 || $overness <= 49) {
return "Grade: F";
} else {
return "N/A";
}
}

echo ovr_grade(90,90,90,90,90);

Then you should get the desired result. But if you want to show "Grade: A" when someone gets 90 in all category, then you should edit your condition like below:

function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
if ($talent >= 90 && $physical >= 90 && $entertainment >= 90 && $reputation >= 90 && $overness >= 90) {
return "Grade: A";
} elseif ($talent >= 80 && $physical >= 80 && $entertainment >= 80 && $reputation >= 80 && $overness >= 80) {
return "Grade: B";
} elseif ($talent >= 70 && $physical >= 70 && $entertainment >= 70 && $reputation >= 70 && $overness >= 70) {
return "Grade: C";
} elseif ($talent >= 60 && $physical >= 60 && $entertainment >= 60 && $reputation >= 60 && $overness >= 60) {
return "Grade: D";
} elseif ($talent = 50 && $physical = 50 && $entertainment = 50 && $reputation = 50 && $overness >= 50) {
return "Grade: E";
} elseif ($talent <= 49 && $physical <= 49 && $entertainment <= 49 && $reputation <= 49 && $overness <= 49) {
return "Grade: F";
} else {
return "N/A";
}
}

echo ovr_grade(90,90,90,90,90);

I guess this will help you out. If need anything more, dont hesitate to ask.

Thanks

Comments

0

You could revise it to:

function ovr_grade($talent, $physical, $entertainment, $reputation, $overness) {
    $max_val = max($talent, $physical, $entertainment, $reputation, $overness);

    if ($max_val >= 90) {
        return "Grade A";
    } elseif ($max_val >= 80) {
        return "Grade B";
    } elseif ($max_val >= 70) {
        return "Grade C";
    } elseif ($max_val >= 60) {
        return "Grade D";
    } elseif ($max_val >= 50) {
        return "Grade E";
    } elseif ($max_val < 50) {
        return "Grade F";
    } else {
        return "N/A";
    }

Comments

0

i think there is no need to return "N/A"; when nothing is greater than 50. just return "Grade:F"

Comments

0

Inspired by James

 $grade = array(
                        'A'=>90,
                        'B'=>80,
                        'C'=>70,
                        'D'=>60,
                        'E'=>50,
                        'F'=>40,                
                        );

        $obtained = max($talent,$physical,$entertainment,$reputation);
        $grading_flag = floor($obtained / 10)*10;
        echo $grade[$gr];

    /* ambiguous about your fail  status */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.