Build a Number Guessing Game - Test problem

Tell us what’s happening:
Describe your issue in detail here.
I have some issues when I run the test, sometimes some tests passes and some times it doesnt, however I can complete the project, I have tried several times and several variations of the code but It wont let me, the code runs in the terminal as it is supposed to. This is the test that is having the biggest issue: " If that username has been used before, it should print Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses. , with <username> 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"
Your code so far


PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
SECRET_NUMBER=$(( $RANDOM % 1000 + 1 ))
# echo "$SECRET_NUMBER"

ASK_USERNAME(){
  echo -e "\nEnter your username:"
  read USERNAME

  USERNAME_CHARACTERS=$(echo $USERNAME | wc -c)
  if [[ $USERNAME_CHARACTERS -gt 22 ]]
  then
    ASK_USERNAME
  fi
}

ASK_USERNAME
RETURNING_USER=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME'")
# RETURNING_USER_HAS_ONE=$($PSQL "SELECT COUNT(*) FROM users INNER JOIN games USING(user_id) WHERE username = '$USERNAME'")
if [[ -z $RETURNING_USER ]]
then
  INSERTED_USER=$($PSQL "INSERT INTO users (username) VALUES ('$USERNAME')")
  echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."

else
  GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games INNER JOIN users USING(user_id) WHERE username = '$USERNAME'")
  BEST_GAME=$($PSQL "SELECT MIN(guesses) FROM games INNER JOIN users USING(user_id) WHERE username = '$USERNAME'")
  GAMES=$(if [[ $GAMES_PLAYED -eq 1 ]]; then echo "game"; else echo "games"; fi)
  GUESSES=$(if [[ $BEST_GAME -eq 1 ]]; then echo "guess"; else echo "guesses"; fi)
  echo -e "\nWelcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

# Grab user_id
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME'")

TRIES=1
GUESS=0

GUESSING_MACHINE(){

  read GUESS


  while [[ $GUESS =~ ^[+-]?[0-9]+$ && ! $GUESS -eq $SECRET_NUMBER ]]
  do

    TRIES=$(expr $TRIES + 1)

    if [[ $GUESS -gt $SECRET_NUMBER ]]
    then

      echo -e "\nIt's lower than that, guess again:"
      read GUESS

    elif [[ $GUESS -lt $SECRET_NUMBER ]]
    then

      echo -e "\nIt's higher than that, guess again:"
      read GUESS

    fi
  done

  if [[ ! $GUESS =~ ^[0-9]+$ ]]
  then
      echo -e "\nThat is not an integer, guess again:"
      TRIES=$(expr $TRIES + 1)
      GUESSING_MACHINE
  fi

}

echo -e "\nGuess the secret number between 1 and 1000:"
GUESSING_MACHINE

# insert data from game
INSERTED_GAME=$($PSQL "INSERT INTO games (user_id, guesses) VALUES ($USER_ID, $TRIES)")
PLURAL_TRIES=$(if [[ $TRIES -eq 1 ]]; then echo "try"; else echo "tries"; fi)
echo -e "\nYou guessed it in $TRIES tries. The secret number was $SECRET_NUMBER. Nice job!"
´´´

**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36</code>

**Challenge:** Basic Node and Express - Chain Middleware to Create a Time Server

**Link to the challenge:**
https://www.freecodecamp.org/learn/relational-database/build-a-number-guessing-game-project/build-a-number-guessing-game
1 Like