Jump to content

need help averaging array integers within if else block


webguync

Recommended Posts

I almost have this right, but not quite. I am trying to echo out the average score if a person scored above 70 and my averaging isn't correct. It should echo out 89, but instead the number is 63.25. The averaging is the only part not working. Everything else works as intended. I might not have my curly braces in the proper order or something. I think it's something stupid I am forgetting. Here is my code.

 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Array Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
function array_average_nonzero($scores) { 
   return array_sum($scores) / count(array_filter($scores)); 
}
$scores["Tim"] = 98;
$scores["Tom"] = 80;
$scores["Mike"] = 50;
$scores["Jason"] = 25;
foreach( $scores as $key => $value)

        if($value < 70){
echo "$key scored a $value which is less than 70. <br />\n";
}
        elseif
            ($value < 70);
		{
echo "The average score of those who scored over 70 is: " .array_sum($scores) / count($scores) . "<br />";
}
        
    
?>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.