How can I get a percentage of right answers if I have 2 arrays in PHP or javascript, preferably in PHP?
So, I have these two arrays and I want to compare a quiz result with the correct answers and get a percentage score:
$quiz_results = array( 'q1' => 'no',
'q2' => 'yes',
'q3' => 'no',
)
$answers = array( 1 => 'yes',
2 => 'no',
3 => 'yes'
)
Run through the answers and compare them to the questions. If they are the same increment the correct answer count.
$quiz_results = array( 'q1' => 'yes',
'q2' => 'yes',
'q3' => 'no',
);
$answers = array( 1 => 'yes',
2 => 'no',
3 => 'yes'
);
$totalquestions = count($answers);
$correct = 0;
foreach($answers as $key => $answer){
//q + the key should do it. Its easier if they are the same obviously
if($answer == $quiz_results['q'.$key]){
// correct
$correct++;
}
}
echo 100 / $totalquestions * $correct; //returns 33.333333%