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

4
  • 1
    And your question is ... ? The code shown here looks like it works. It is very inefficient, but it looks like it works. Commented Oct 22, 2017 at 10:18
  • Possible duplicate of PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" Commented Oct 22, 2017 at 10:22
  • The code doesnot produce expected points/weight per score sir-BartFriederichs Commented Oct 22, 2017 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. Commented Oct 22, 2017 at 10:26

2 Answers 2

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.

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

1 Comment

Thank you Sir-clemens321 ,you saved my time.Thanks once again
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>";

1 Comment

Thanks sir,I didnt come back on time.It helped a lot .

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.