0

I can tell that I'm making a really silly mistake here. Can someone please help me out by correcting my code? Probably an issue with my loop.

<?php

        $students = array("Sauer Jeppe", "Von Weilligh", "Troy Commisioner", "Paul Krugger", "Jacob Maree");
        $grades = array(75, 44, 60, 62, 70);

        for($i=0; $i<count($students); $i++){

            for($j=0; $j<count($grades); $j++){


                if($grades[$j] >= 70){

                    echo"$students[$i] scored a Distinction.";
                }

                elseif($grades[$j] >= 50){

                    echo"$students[$i] scored a Pass.";
                }

                elseif($grades[$j] >= 0){

                    echo"$students[$i] scored a Fail.";
                }

            }

        }

    ?>

It's meant to display:

Sauer Jeppe scored a Distinction.

Von Weilligh scored a Fail.

Troy Commisioner scored a Pass.

Paul Krugger scored a Pass.

Jacob Maree scored a Distinction.

Thank you.

1 Answer 1

2

There's no need for inner for loop, just take the value from $grades under the same key $i:

for($i=0; $i<count($students); $i++){
    $grade = $grades[$i];
    if($grade >= 70){
        echo"$students[$i] scored a Distinction.";
    } elseif ($grade >= 50){
        echo"$students[$i] scored a Pass.";
    } elseif ($grade >= 0){
        echo"$students[$i] scored a Fail.";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.