0

Can anyone spot a light where am I going wrong?

This is the objective of the program:
To make a program that has a function that is passed 3 quiz scores and returns the average of the top 2 scores.

<?php
  function average ($quiz1, $quiz2, $quiz3) {
    $quiz1 = 100;
    $quiz2 = 50;
    $quiz3 = 80;

    $average1 = $quiz1 + $quiz3 / 2;
    $average2 = $quiz2 + $quiz3 / 2;
    $average3 = $quiz3 + $quiz1 / 2;

    echo $average1;
    echo $average2;
    echo $average3;

    return ($average);
  }

  echo max ($average1, $average2, $average3);
  average (100, 50, 80);

?>
2
  • 1
    You're returning $average from your function, but don't have an actual variable called $average in the function to return. Commented Mar 30, 2016 at 23:26
  • You don't call the function Commented Mar 30, 2016 at 23:29

1 Answer 1

1

The following function would take your 3 quiz scores and return the average of the top 2 scores.

It puts the 3 scores into an array, then reverse sorts them (greatest to least). Then it takes the first two items (the greatest) and provides the average of them both.

function average($quiz1, $quiz2, $quiz3) {

    $scores = [$quiz1,$quiz2,$quiz3];
    rsort($scores);
    return ($scores[0] + $scores[1]) / 2;

}

Example:

echo average(50,100,150);

Result:

125

Sign up to request clarification or add additional context in comments.

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.