Tell us what’s happening:
this test wont pass though the output is correct:If that username has been used before, it should print Welcome back, ! You have played <games_played> games, and your best game took <best_game> guesses., with being a users name from the database, <games_played> being the total number of games that user has played, and <best_game> being the fewest number of guesses it took that user to win the game
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
RANDOM_NUMBER=$(( RANDOM % 1000 + 1 ))
get_valid_guess() {
read USERS_GUESS
while [[ ! $USERS_GUESS =~ ^[0-9]+$ ]]
do
echo "That is not an integer, guess again:"
read USERS_GUESS
done
}
echo -e "\nEnter your username:\n"
read USER_NAME
USER_ID=$($PSQL "SELECT user_id FROM users WHERE user_name = '$USER_NAME'")
if [[ -z $USER_ID ]]
then
echo "Welcome, $USER_NAME! It looks like this is your first time here."
$PSQL "INSERT INTO users(user_name) VALUES('$USER_NAME')"
USER_ID=$($PSQL "SELECT user_id FROM users WHERE user_name='$USER_NAME'")
else
GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE user_id = $USER_ID")
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE user_id = $USER_ID")
echo "Welcome back, $USER_NAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
$PSQL "UPDATE users SET games_played = games_played + 1 WHERE user_id = $USER_ID"
echo -e "\nGuess the secret number between 1 and 1000:\n"
get_valid_guess
NUMBER_OF_GUESSES=1
while [[ $USERS_GUESS -ne $RANDOM_NUMBER ]]
do
if [[ $USERS_GUESS -gt $RANDOM_NUMBER ]]
then
echo "It's lower than that, guess again:"
else
echo "It's higher than that, guess again:"
fi
get_valid_guess
NUMBER_OF_GUESSES=$(( NUMBER_OF_GUESSES + 1 ))
done
echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $RANDOM_NUMBER. Nice job!"
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE user_id=$USER_ID")
if [[ -z $BEST_GAME || $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
then
$PSQL "UPDATE users SET best_game=$NUMBER_OF_GUESSES WHERE user_id=$USER_ID"
fi
Your code so far
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Challenge Information:
Build a Number Guessing Game - Build a Number Guessing Game