Write a PHP program to check if a given array of integers contains no 3 or a 5
<?php function test($nums) { $three = false; $five = false; for ($i = 0; $i < sizeof($nums); $i++) { if ($nums[$i] == 3) {$three = true;} if ($nums[$i] == 5) {$five = true;} if ($three && $five) return false; } return true; } var_dump(test([5, 5, 5, 5, 5])); var_dump(test([3, 3, 3, 3])); var_dump(test([3, 3, 3, 5, 5, 5])); var_dump(test([1, 6, 8, 10]));
Sample Output:
bool(true) bool(true) bool(false) bool(true)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer