#!/bin/bash
PSQL="psql -U freecodecamp -d number_guess -t --no-align -c"
echo -e "\nEnter your username:"
read USERNAME
USER_EXIST=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
if [[ -z $USER_EXIST ]]; then
INSERT=$($PSQL "INSERT INTO users(username) VALUES('$USERNAME')")
if [[ $INSERT == "INSERT 0 1" ]]; then
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
fi
else
USER_ID=$USER_EXIST
GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id=$USER_ID")
BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM games WHERE user_id=$USER_ID")
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
COUNT_GUESSES=0
echo -e "\nGuess the secret number between 1 and 1000:\n"
while true; do
read -p "Your guess: " USER_GUESS
if [[ ! "$USER_GUESS" =~ ^[0-9]+$ ]]; then
echo -e "\nThat is not an integer, guess again:"
continue
fi
((COUNT_GUESSES++))
if [[ $USER_GUESS -eq $SECRET_NUMBER ]]; then
echo -e "\nYou guessed it in $COUNT_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
INSERT=($PSQL "INSERT INTO games(secret_number, number_of_guesses, user_id) VALUES($SECRET_NUMBER, $COUNT_GUESSES, $USER_ID)")
if [[ $INSERT == "INSERT 0 1" ]]; then
return ""
fi
break
elif [[ $USER_GUESS -lt $SECRET_NUMBER ]]; then
echo -e "\nIt's higher than that, guess again:"
else
echo -e "\nIt's lower than that, guess again:"
fi
done
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Challenge Information:
Number Guessing Game - Build a Number Guessing Game
Hello, thank you for your time. I’ve added count to the invalid guesses too but I’m still not passing, though my code counts all with the invalid guesses.
MY CURRENT CODE:
#!/bin/bash
PSQL="psql -U freecodecamp -d number_guess -t --no-align -c"
# user enter their name
echo "Enter your username:"
read USERNAME
# check if user already exist in DB
USER_EXIST=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
# if not exist
if [[ -z $USER_EXIST ]]; then
# insert new user
$PSQL "INSERT INTO users(username) VALUES('$USERNAME')"
# get user id
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
echo "Welcome, $USERNAME! It looks like this is your first time here."
else
USER_ID=$(echo $USER_EXIST | xargs)
# Fetch the stored username and trim whitespace
DB_USERNAME=$($PSQL "SELECT username FROM users WHERE user_id=$USER_ID")
DB_USERNAME=$(echo $DB_USERNAME | xargs)
# count how many times user played game and trim white space
GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id=$USER_ID")
GAMES_PLAYED=$(echo $GAMES_PLAYED | xargs)
# get minimum number of guesses
BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM games WHERE user_id=$USER_ID")
BEST_GAME=$(echo $BEST_GAME | xargs)
# if user hasn't played game yet
# set minimum guesses to 0
if [[ -z $BEST_GAME ]]; then
BEST_GAME=0
fi
echo "Welcome back, $DB_USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
# generate a random number from 1-1000
SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
# initial number of guesses set to 0
COUNT_GUESSES=0
echo "Guess the secret number between 1 and 1000:"
while true; do
read -p "Your guess: " USER_GUESS
# if user guess is not a number
if [[ ! "$USER_GUESS" =~ ^[0-9]+$ ]]; then
echo "That is not an integer, guess again:"
# increment number of guesses by 1
((COUNT_GUESSES++))
continue
fi
((COUNT_GUESSES++))
# if user get the correct number
if [[ $USER_GUESS -eq $SECRET_NUMBER ]]; then
echo "You guessed it in $COUNT_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
# insert the random number, number of guesses, and the user id to DB
$PSQL "INSERT INTO games(secret_number, number_of_guesses, user_id) VALUES($SECRET_NUMBER, $COUNT_GUESSES, $USER_ID)"
break
elif [[ $USER_GUESS -lt $SECRET_NUMBER ]]; then
echo "It's higher than that, guess again:"
else
echo "It's lower than that, guess again:"
fi
done
i would try re-running the test a few times because when I worked on this, I would get inconsistent failures (they would jump around). I ended up re-running until it passed.
(also make sure you are not printing out any strings that are not explicitly requested)
Edit: also remove any extra newlines or punctuation if you have them
its possible the test is finicky (because I had to, for example, run my code multiple times to pass).
In my version, I had only one table. And I only inserted once per game and only selected once per game. I mention it because sometimes the test is waiting on a specific response and if your response is being delayed because of the way you wrote the code, it may time-out the test and cause it to fail.
You can go to your container and click on the OUTPUT tab
Then, once there, you will see a drop down menu on the right side of the menu bar. You can click that to find the test output and/or test log and see if that helps you determine the issue.