Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:
Describe your issue in detail here.
For this project, I have created only one table (users) in the database with fields user_id(serial, PK), user_name(varchar, not null) and other two fields.

The below bash script is not complete as I have just started it and will take more time to complete. I am not confused about the code so far because whenever I run the number_guess.sh script(with the code so far), the validation as coded is performed correctly. But every time the CodeRoad is run or initiated, two rows are inserted into the users table (that I have created) without my knowledge or action. I had to redo the code in a different way to see if the insert statement in the code /script is initiated incorrectly but that is not the case.

Can anyone explain to me why that is happening.
Your code so far

echo "Enter your username:"
  read USER_NAME
  #echo -e "\nThe username your enetered is $USER_NAME"

  USER_ID=$($PSQL "select user_id from users where user_name='$USER_NAME'")
  # if user id  not available
  if [[ -z $USER_ID ]]
  then
      echo "Welcome, $USER_NAME! It looks like this is your first time here."
      #add the new user name into the table and fetch the new user id
      INSERT_DETAILS=$($PSQL "insert into users(user_name,no_of_games,fewest_no_of_guess) values('$USER_NAME',0,0)")
      #echo "$INSERT_DETAILS"
      NEW_USER_ID=$($PSQL "select user_id from users where user_name='$USER_NAME'")
      GUESS_NUMBER "MAIN-CALLING" $NEW_USER_ID 0 0
  else
  # if user id is available
      USER_DETAILS=$($PSQL "select no_of_games,fewest_no_of_guess from users where user_id=$USER_ID")
      echo "$USER_DETAILS" | while read NO_OF_GAMES BAR FEWEST_GAMES
      do
        echo "Welcome back, $USER_NAME! You have played $NO_OF_GAMES games, and your best game took $FEWEST_GAMES guesses."
        GUESS_NUMBER "MAIN-CALLING" $USER_ID $NO_OF_GAMES $FEWEST_GAMES
      done
  fi

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Number Guessing Game - Build a Number Guessing Game

Link to the challenge:

 number_guess=> select * from users;
 user_id |     user_name      | no_of_games | fewest_no_of_guess 
---------+--------------------+-------------+--------------------
      86 | Tracy              |           0 |                  0
      87 | Amma               |           0 |                  0
      88 | user_1662859461734 |           0 |                  0
      89 | user_1662859461733 |           0 |                  0
      90 | user_1662861099315 |           0 |                  0
      91 | user_1662861099314 |           0 |                  0
(6 rows)

number_guess=> select * from users;
 user_id |     user_name      | no_of_games | fewest_no_of_guess 
---------+--------------------+-------------+--------------------
      86 | Tracy              |           0 |                  0
      87 | Amma               |           0 |                  0
      88 | user_1662859461734 |           0 |                  0
      89 | user_1662859461733 |           0 |                  0
      90 | user_1662861099315 |           0 |                  0
      91 | user_1662861099314 |           0 |                  0
(6 rows)

The above is the data in my users table. All the names starting with user… is not something that I have tested with. The user_id column is serial and it shows 91. I have not tested the table with so much data.

Please advise!

Each time you press “Run”, FreeCodeCamp’s CodeRoad tester runs through your code to test it by inputting different values (this is how it checks off the test requirements that you pass/fail)

1 Like