0

Hello I have two functions here,the first Function will receive scores of students,and convert all scores into Grades,I want the second function to receive and assign each grade to points(weight). Here is what I have tried so far

   function gradeArray($score) {
     if     ($score >= 70)  return "A";
     elseif ($score >= 50)  return "B";
     elseif ($score >= 40)  return "C";
     else                   return "F";
 }
 function grade($grade) {
 $grade=gradeArray($score);
  if     ($grade == "A")  return "1";
 elseif ($grade == "B")  return "2";
  elseif ($grade =="C")  return "3";
   else                   return "4";
 }

  // scores received from HTML form`

    $scores = array (55, 68, 43, 78);

 //Display result in a tabular form
     echo "<table border='1'><th>Score</th><th>Grade</th>";


     foreach ($scores as $score) {
    echo "<tr><td>$score</td><td>" . gradeArray($score) . "</td>
     <td>" .       grade($grade) . "</td></tr>";

      }

      echo "</table>";

intended output

      Score  Grade       Points
      55        B            2
      68        B            2
      43        C            3
      78        A            1

After running above code, i get the following errors

Notice: Undefined variable: grade in C:\xampp\htdocs\TEST.php on line 24

Notice: Undefined variable: score in C:\xampp\htdocs\TEST.php
on line 10

Despite of the errors uncounted ,still I get the following results

    Score   Grade       Points
     55     B            4
     68     B            4
     43     C            4
     78     A            4

where did I go wrong?,Help please , i am very junior at php programing

M0ns1f
  • 2,705
  • 3
  • 15
  • 25
Ansy
  • 47
  • 1
  • 7
  • 1
    And your question is ... ? The code shown here looks like it works. It is very inefficient, but it looks like it works. – Bart Friederichs Oct 22 '17 at 10:18
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – BenRoob Oct 22 '17 at 10:22
  • The code doesnot produce expected points/weight per score sir-BartFriederichs – Ansy Oct 22 '17 at 10:24
  • 1
    Please edit your question to remove the errors and expected results from the code block. It makes the question hard to understand. – Mathieu VIALES Oct 22 '17 at 10:26

2 Answers2

1

Either use the score as base for both method calls, then use:

-function grade($grade) {
+function grade($score) {
 $grade=gradeArray($score);
(...)
 echo "<tr><td>$score</td><td>" . gradeArray($score) . "</td>
-<td>" .       grade($grade) . "</td></tr>";
+<td>" .       grade($score) . "</td></tr>";

Or use the the returned grade again (better, as it calls gradeArray() only once)

 function grade($grade) {
-$grade=gradeArray($score);
(...)
-echo "<tr><td>$score</td><td>" . $grade = gradeArray($score) . "</td>
+echo "<tr><td>$score</td><td>" . ($grade = gradeArray($score)) . "</td>
 <td>" .       grade($grade) . "</td></tr>";

It would be even better if you get $grade and $weight right after your opening foreach and use the variables instead of method calls in your templating code.

clemens321
  • 2,103
  • 12
  • 18
0

you have two problem in you code :

first you have a variable $grade that is not initialised.

second in grade($grade) function you have a variable named $score that is not initialised in the function so to correct that change you code to this :

    function gradeArray($score) {
         if     ($score >= 70)  return "A";
         elseif ($score >= 50)  return "B";
         elseif ($score >= 40)  return "C";
         else                   return "F";
     }
     function grade($grade) {
      if     ($grade == "A")  return "1";
     elseif ($grade == "B")  return "2";
      elseif ($grade =="C")  return "3";
       else                   return "4";
     }

      // scores received from HTML form`

        $scores = array (55, 68, 43, 78);

     //Display result in a tabular form
         echo "<table border='1'><th>Score</th><th>Grade</th><th>points</th>";


         foreach ($scores as $score) {
         $grade = gradeArray($score);
        echo "<tr><td>$score</td><td>" . gradeArray($score) . "</td>
         <td>" .       grade($grade) . "</td></tr>";

          }

          echo "</table>";
M0ns1f
  • 2,705
  • 3
  • 15
  • 25