-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge3.php
More file actions
34 lines (24 loc) · 855 Bytes
/
challenge3.php
File metadata and controls
34 lines (24 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
/*
Challenge 3: Calculate the average students grade from an array of students. Each student has their own array with the key 'grades'.
1. Create an array of students with their names and grades (0 - 100)
john 85,90,92,88
jane 95,88,91,87
joe 75,82,79,88
2. Iterate over the students array with a foreach loop
3. Calculate the average grade for each student
*/
$students = [
['name' => 'John','grades' => [85,90,92,88]],
['name' => 'Jane','grades' => [95,88,91,87]],
['name' => 'Joe','grades' => [75, 82, 79, 88]]];
echo 'Average Grade:';
echo '<br>';
foreach ($students as $student) {
$name = $student['name'];
$grades = $student['grades'];
$average = array_sum($grades) / count($grades);
echo '$name: Average Grade = '. number_format($average, 2);
echo '<br>';
}
?>