Created
October 7, 2019 17:28
-
-
Save susanBuck/c00b38f0753e35693d7e353f96d4f699 to your computer and use it in GitHub Desktop.
High-low example for Stephanie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$answer = rand(1, 50); | |
$guess = rand(1, 50); | |
$newGuess = null; | |
$status = null; | |
$guesses = []; | |
$statuses = []; | |
while ($guess != $answer) { | |
# Prevent the same number from being guessed twice | |
do { | |
$newGuess = rand(1, 50); | |
$guess = $newGuess; | |
} while (in_array($guess, $guesses)); | |
# Check the guess | |
if ($guess == $answer) { | |
$status = 'correct.'; | |
} elseif ($guess < $answer) { | |
$status = 'too low.'; | |
} elseif ($guess > $answer) { | |
$status = 'too high.'; | |
} | |
# Record the results | |
$guesses[] = $guess; | |
$statuses[] = $status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment