Build a Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

I am struggling to get the outputs to pass for user feed back on 2 tasks One question I have that is not clear, Is the total guesses for a round to include non integer guesses? What about out of range guesses?

Also is there a way to view the test runner output?

There is not space here to put my full outputs but the lines that are not passing are these.

Welcome back, DEN! You have played 1 games, and your best game took 4 guesses. 
You guessed it in 4 tries. The secret number was 862. Nice job!

Your code so far

#! /bin/bash

# create link to db
PSQL="psql --username=freecodecamp --dbname=postgres -t --no-align -c"

# set up game perms
TARGET=$(($RANDOM % 1000 + 1))
COUNTER=0
CORRECT=false

# get name from user
echo "Enter your username:"
read USERNAME
# query db for existing name
USER=$($PSQL "SELECT username FROM gamers WHERE username = '${USERNAME}'")

# add name if not found
if [[ -z $USER ]]
  then
    echo "Welcome, $USERNAME! It looks like this is your first time here."
    $PSQL "INSERT INTO gamers(username,score,played) VALUES('$USERNAME',1000,0);"
    SCORE=1000
# query db for info if found
  else
    PLAYED=$($PSQL "SELECT played FROM gamers WHERE username = '$USER'")
    SCORE=$($PSQL "SELECT score FROM gamers WHERE username = '$USER'")
    # return responce
    echo "Welcome back, $USER! You have played $PLAYED games, and your best game took $SCORE guesses."
fi

# enter guess loop
#echo $TARGET
# echo $CORRECT
while [[ "$CORRECT" == "false" ]]; do
  # get guess
  echo "Guess the secret number between 1 and 1000:"
  read GUESS
 
  # check if int
  if ! [[ "$GUESS" =~ ^[0-9]+$ ]]
  # if not short circuit return
    then
      echo "That is not an integer, guess again:" 
    continue
  fi
# increment guess count (not sure if this is to include non int guesses)
 (( COUNTER++ ))

  # return if correct
  if [[ "$GUESS" -eq "$TARGET" ]] 
  then
    echo "You guessed it in $COUNTER tries. The secret number was $TARGET. Nice job!"
	CORRECT=true

  # return if guess is high
  elif [[ "$GUESS" -gt "$TARGET" ]]
  then
    echo "It's lower than that, guess again:"

  # return if guess is low
  else
    echo "It's higher than that, guess again:"
  fi

# exit loop
done


# Query lowest guess count in db and increment down if applicable
if [[ "$SCORE" -gt "$COUNTER" ]]
then
  $PSQL "UPDATE gamers SET score=$COUNTER WHERE username='$USERNAME';"
fi


(( PLAYED++ ))
$PSQL "UPDATE gamers SET played=$PLAYED WHERE username='$USERNAME';"

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/154.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

Hi @Dennis-Petre

This is for a number guessing game and there is no mention of edge cases for invalid input, so no need to account for letters, symbols, or floats.

If it is a number, even if it out of range, count it as a guess.

Happy coding

Thanks for that explaination.

Is there a way to view the test runner output?

When I test the game, the outputs look correct to me, but the test still fail so I am at a loss to know what is not correct.

Hi @Dennis-Petre

For the failing test, play a round then post a screen grab of the output and the instructions.

Happy coding

your code is generating unexpected output, like INSERT 0 1, UPDATE 1, this happening during a guess round is messing with the tests

Thanks for pointing me in the correct direction.