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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.