Hi,
I have completed this project, and my code output is as per the user stories’ requirements. Yet when I am running tests, my tasks 8 and 13 fail even though the output is as required.
Below is the screen shot of the tasks
My Code is as below:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess --tuples-only -c"
RANDOM_NUMBER=$(( RANDOM % 1000 + 1 ))
GUESS=0
# Ask for username
echo "Enter your username: "
read USERNAME
# Check if username exissts
USER_EXISTS=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME';")
# echo -e "\nUserName: $USERNAME"
# If username exists
if [[ -z $USER_EXISTS ]]
then
INSERT_USER_DATA=$($PSQL "INSERT INTO users (username) VALUES ('$USERNAME');")
echo "Welcome, $USERNAME! It looks like this is your first time here."
else
# Get u ser game data
USERDATA=$($PSQL "SELECT username, COUNT(*), MIN(number_of_guesses) FROM users LEFT JOIN games USING(user_id) WHERE username = '$USERNAME' GROUP BY username;")
echo "$USERDATA" | while read USER_DB_NAME BAR GAMES_PLAYED BAR BEST_GAME
do
echo "Welcome back, $USER_DB_NAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
done
fi
while true
# Start the game
do
echo -e "\nGuess the secret number between 1 and 1000:"
read USER_GUESS
# If guess is not an integer
if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]
then
echo "That is not an integer, guess again:"
continue
fi
(( GUESS++ ))
# if guess is greater than the random number
if [[ $USER_GUESS -gt $RANDOM_NUMBER ]]
then
echo "It's lower than that, guess again:"
# if guess is less thant the random number
elif [[ $USER_GUESS -lt $RANDOM_NUMBER ]]
then
echo "It's higher than that, guess again:"
else
# if the guess is correct
# Get user_id
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")
# Add game data to database
INSERT_GAME_RESULT=$($PSQL "INSERT INTO games (user_id, number_of_guesses) VALUES ($USER_ID, $GUESS);")
# Display game completion message
echo "You guessed it in $GUESS tries. The secret number was $RANDOM_NUMBER. Nice job!"
break
fi
done
Sample gameplay as below with required output
Enter your username:
John
Welcome back, John! You have played 1 games, and your best game took 9 guesses.
Guess the secret number between 1 and 1000:
500
It's higher than that, guess again:
Guess the secret number between 1 and 1000:
800
It's higher than that, guess again:
Guess the secret number between 1 and 1000:
900
It's lower than that, guess again:
Guess the secret number between 1 and 1000:
850
It's lower than that, guess again:
Guess the secret number between 1 and 1000:
830
It's lower than that, guess again:
Guess the secret number between 1 and 1000:
829
It's lower than that, guess again:
Guess the secret number between 1 and 1000:
810
It's lower than that, guess again:
Guess the secret number between 1 and 1000:
805
It's lower than that, guess again:
Guess the secret number between 1 and 1000:
802
It's higher than that, guess again:
Guess the secret number between 1 and 1000:
803
You guessed it in 10 tries. The secret number was 803. Nice job!