Build a Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

The tests that involve printing lines (specifically 7, 10, 11, 12 and 13) change from fulfilled to not fulfilled randomly, and the 8th and 9th tests are not fulfilled despite the script printing exactly what seems to be asked for.

Other posts on this topic suggest counting invalid guesses, which I’ve done, but the issues remain. Does anybody have any insights?

Thanks in advance.

Screenshot of tests:

Your code so far

#! /bin/bash

PSQL="psql --username=freecodecamp --dbname=guessing_game -t --no-align -c"

echo "Enter your username:"
read GIVEN_USERNAME
GIVEN_USERNAME=$(echo $GIVEN_USERNAME | sed -E "s/ //g")

if [[ $GIVEN_USERNAME =~ ^([a-zA-Z]|[0-9]){1,22}$ ]]
then
  USERNAME=$($PSQL "SELECT username FROM usernames WHERE username = '$GIVEN_USERNAME'")
  if [[ -z $USERNAME ]]
  then
    INSERT_USERNAME_RESULT=$($PSQL "INSERT INTO usernames(username) VALUES('$GIVEN_USERNAME')")
    USER_ID=$($PSQL "SELECT user_id FROM usernames WHERE username = '$GIVEN_USERNAME'")
    echo -e "Welcome, $GIVEN_USERNAME! It looks like this is your first time here."
  else
    USER_ID=$($PSQL "SELECT user_id FROM usernames WHERE username = '$USERNAME'")
    TOTAL_GAMES=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id = $USER_ID")
    BEST_GAME=$($PSQL "SELECT MIN(guesses) FROM games WHERE user_id = $USER_ID")

    echo -e "Welcome back, $USERNAME! You have played $TOTAL_GAMES games, and your best game took $BEST_GAME guesses."
  fi
fi

GUESSING_GAME() {
  NUMBER=$((($RANDOM % 1000) + 1))

  echo "Guess the secret number between 1 and 1000:"
  echo $NUMBER
  read GUESS
  COUNT=1

  while (( $GUESS != $NUMBER ))
  do
    if [[ $GUESS =~ ^[0-9]+$ ]]
    then
      if [[ $GUESS -gt $NUMBER ]]
      then
        echo "It's lower than that, guess again:"
        read GUESS
        COUNT=$(($COUNT + 1))
      elif [[ $GUESS -lt $NUMBER ]]
      then
        echo "It's higher than that, guess again:"
        read GUESS
        COUNT=$(($COUNT + 1))
      fi
    else
      echo "That is not an integer, guess again:"
      read GUESS
      COUNT=$(($COUNT + 1))
    fi
  done

  INSERT_GAME_RESULT=$($PSQL "INSERT INTO games(user_id, guesses) VALUES($USER_ID, $COUNT)")
  echo -e "You guessed it in $COUNT tries. The secret number was $NUMBER. Nice job!"
}

GUESSING_GAME;

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

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-number-guessing-game/602da04c22201c65d2a019f4.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome back to the forum @JYF

You are not asked to valid the characters for the username. This will slow down the script and may cause the tests to time out.

Create table to store users, number of games played, and best game guesses.

You are not asked to store all game guesses, only the best ones.

Happy coding