Skip to content

Instantly share code, notes, and snippets.

@susanBuck
Created October 7, 2019 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save susanBuck/c00b38f0753e35693d7e353f96d4f699 to your computer and use it in GitHub Desktop.
Save susanBuck/c00b38f0753e35693d7e353f96d4f699 to your computer and use it in GitHub Desktop.
High-low example for Stephanie
<?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