Relational Database (BETA) : Number Guessing Game / Script / Test misinterpretation / test failed

I might be getting the no of tries <best_game> data wrong or I might be counting it wrong like:

I do not count it until the guess is a number

(Should I do it ???)

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

When the secret number is guessed, your script should print `You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job!

I cannot pass these tests in Code Road.

But running the script doing it fine, I’m really confused



CODE:

#!/bin/bash

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

COMPARE(){

 # 2 will be the STDIN and 1 will be the secret

  if [ $1 -gt $2 ]
  then
    echo "It's higher than that, guess again:"
  else
    echo "It's lower than that, guess again:"
  fi

}

UPDATE_DB(){
# GAME_PLAYED OLD $2
GP=$(($2+1))
LG=$4
# if lowest guess / best score is 0 tie not played 
if [[ $3 -ne 0 ]]
then
  if [[ $4 -ge $3 ]]
  then
    LG=$3
  fi
fi

($PSQL "update user_info set  game_played = $GP ,lowest_guess = $LG where username = '$USERNAME';") >> /dev/null 
}

GUESS_GAME(){

# TBG
SECRET=$((1 + $RANDOM % 1000))

#echo "THE SECRET TESTING: $SECRET"

echo "Guess the secret number between 1 and 1000:"

TRY=1
###############################
read VAL

####################
while [[ ! $VAL =~ ^[0-9]+$ ]]
do
 echo "That is not an integer, guess again:"
 read VAL
done
####################
if [[ $SECRET -ne $VAL ]]
then

    COMPARE $SECRET $VAL
    ###############################
    while [ $VAL -ne $SECRET ]
    do
    ((TRY=TRY+1))
    ###############################
    read VAL

    ####################
    while [[ ! $VAL =~ ^[0-9]+$ ]]
    do
    echo "That is not an integer, guess again:"
    read VAL
    done
    ####################

    COMPARE $SECRET $VAL
    ###############################
    done
fi
UPDATE_DB $1 $2 $3 $TRY
echo   "You guessed it in $TRY tries. The secret number was $SECRET. Nice job!"
}



MAIN(){

echo "Enter your username:"
# ask for username 
read USERNAME 

# get data for this particular username
QUR=$($PSQL "select * from user_info where username='$USERNAME'")


#if users data is null then
if [[ -z $QUR ]]
then

# Enter user data with other values as 0 as default
  ($PSQL "insert into user_info values('$USERNAME');") >> /dev/null 
  echo "Welcome, $USERNAME! It looks like this is your first time here."
  GUESS_GAME $USERNAME 0 0

else
  
  IFS="|"; read USER_NAME GAME_PLAYED LOWEST_GUESS <<< $QUR
  echo  "Welcome back, $USER_NAME! You have played $GAME_PLAYED games, and your best game took $LOWEST_GUESS guesses."  
  GUESS_GAME $USER_NAME $GAME_PLAYED $LOWEST_GUESS

fi

}


MAIN

Thanks for help !!!

Not entirely sure what is happening. I’ve looked at your code and failing test. Test seem to be failing, because script prints out different number of tries for the best game, than what is expected. For example, when script returned:

Welcome back, user_1655984073767! You have played 1 games, and your best game took 256 guesses.

Test was expecting 259 guesses to be indicated.

Notice after the successful guess, there’s one more line print out that number is lower/higher. Maybe this is somehow impacting the result.

Can you link me where you saw what test are expecting ? Thanks

Kind of, but the expected values depends on the specific game that was played. I changed the test (learn-number-guessing-game/1.1.test.js at v1.0.3 · freeCodeCamp/learn-number-guessing-game · GitHub) to print out relevant bits when test was run manually from console.