Num guess game: CodeRoad tasks(8, 11, 13 and 16) aren’t registering as completed.
Your code so far
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
INPUT_NAME () {
echo "Enter your username:"
read NAME
n=${#NAME}
#USER=$($PSQL "SELECT COUNT(*) FROM users WHERE username='$NAME'")
#If username prompted is within parameters
if [[ $n -gt 22 ]] || [[ $n -le 0 ]]
then
echo "The username must be less than or equal to 22 characters and greater than 0."
INPUT_NAME
else
USER_NAME=$($PSQL "SELECT username FROM users WHERE username='$NAME'")
#check if username is in database
if [[ ! -z $USER_NAME ]]
then
#if username is in database than return the amount of games played and best game results
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USER_NAME'")
GAMES_PLAYED=$($PSQL "SELECT game_frequency FROM users WHERE user_id = $USER_ID")
BEST_GAME=$($PSQL "SELECT MIN(best_guess) FROM games WHERE user_id = $USER_ID")
BEST_GAME=${BEST_GAME:-N/A}
if [[ -z $BEST_GAME ]]
then
BEST_GAME="N/A"
fi
echo "Welcome back, $USER_NAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
#If the username has not been used before
USER_NAME=$NAME
echo -e "\nWelcome, $USER_NAME! It looks like this is your first time here."
fi
#script to generate a random number between 1 and 1000
CORRECT_ANSWER=$(( $RANDOM % 1000 + 1 ))
GUESS_COUNT=0
INPUT_GUESS $USER_NAME $CORRECT_ANSWER $GUESS_COUNT
fi
}
INPUT_GUESS () {
USER_NAME=$1
CORRECT_ANSWER=$2
GUESS_COUNT=$3
echo "Guess the secret number between 1 and 1000:"
read USER_GUESS
if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]
then
#if input anything else than an integer
echo "That is not an integer, guess again:"
INPUT_GUESS $USER_NAME $CORRECT_ANSWER $GUESS_COUNT
else
GUESS_COUNT=$(( $GUESS_COUNT + 1 ))
if [[ $USER_GUESS -lt $CORRECT_ANSWER ]]
then
echo "It's higher than that, guess again."
INPUT_GUESS $USER_NAME $CORRECT_ANSWER $GUESS_COUNT
elif [[ $USER_GUESS -gt $CORRECT_ANSWER ]]
then
echo "It's lower than that, guess again."
INPUT_GUESS $USER_NAME $CORRECT_ANSWER $GUESS_COUNT
else
echo "You guessed it in $GUESS_COUNT tries. The secret number was $USER_GUESS. Nice job!"
SAVE_USER $USER_NAME $GUESS_COUNT
fi
fi
}
#function to store user information in database
SAVE_USER () {
#initialise variables to be used in function
USER_NAME=$1
GUESS_COUNT=$2
#check for name in database
CHECK_NAME=$($PSQL "SELECT username FROM users WHERE username='$USER_NAME'")
#if it is not database, store it
if [[ -z $CHECK_NAME ]]
then
INSERT_NEW_USER=$($PSQL "INSERT INTO users(username, game_frequency) VALUES ('$USER_NAME', 1)")
else
#else return and store the amount of games played
GET_GAMES_PLAYED=$(( $($PSQL "SELECT game_frequency FROM users WHERE username='$USER_NAME'") + 1))
UPDATE_EXIST_USER=$($PSQL "UPDATE users SET game_frequency=$GET_GAMES_PLAYED WHERE username='$USER_NAME'")
fi
SAVE_GAME $USER_NAME $GUESS_COUNT
}
SAVE_GAME() {
USER_NAME=$1
NUMBER_OF_GUESSES=$2
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USER_NAME'")
if [[ ! -z $USER_ID ]]
then
INSERT_GAME=$($PSQL "INSERT INTO games(user_id, best_guess) VALUES ($USER_ID, $NUMBER_OF_GUESSES)")
else
echo "Error: User ID not found for username $USER_NAME."
fi
}
INPUT_NAME

Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Number Guessing Game - Build a Number Guessing Game
Good day Teller and the forum, hope this message finds you well. The failing tasks are 8. (The welcome message, you have been here before and returns previous games stats.), if someone is willling to provide description of task 11, 13 and 16 respectively, I will be eternally grateful. Unfortunately I will only get back to you next month, I have run out of git pod credits. Lol just needed the last program to run.
Create a number_guessing_game folder in the project folder for your program
Create number_guess.sh in your number_guessing_game folder and give it executable permissions
Your script should have a shebang at the top of the file that uses #!/bin/bash
Turn the number_guessing_game folder into a git repository
Your git repository should have at least five commits
Your script should randomly generate a number that users have to guess
When you run your script, you should prompt the user for a username with Enter your username:, and take a username as input. Your database should allow usernames that are 22 characters
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
If the username has not been used before, you should print Welcome, <username>! It looks like this is your first time here.
The next line printed should be Guess the secret number between 1 and 1000: and input from the user should be read
Until they guess the secret number, it should print It’s lower than that, guess again: if the previous input was higher than the secret number, and It’s higher than that, guess again: if the previous input was lower than the secret number. Asking for input each time until they input the secret number.
If anything other than an integer is input as a guess, it should print That is not an integer, guess again:
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! and finish running
The message for the first commit should be Initial commit
The rest of the commit messages should start with fix:, feat:, refactor:, chore:, or test:
You should finish your project while on the main branch, your working tree should be clean, and you should not have any uncommitted changes
What is the purpose of this variable?
If the user already exists in the database, then an integer should also exist for the best game.
Not sure why you are passing variables after an incorrect guess. Your script is also displaying the "Guess the secret number …" message, after each guess. I don’t think that is mentioned in the instructions.
That variable doesn’t look like the right one to use.
Make sure your tree is clean, and all changes are committed.
The n variable I put there when started the writing, it’s a habit I have when I declare an int variable, it is unnecessary.
I know that if a user already exists in the database, then an integer should also exist for best game, I did that because I had two different tables games and users, but there was inconsistencies calling the foreign column user_id and matching it to game_id.
I was passing variables after each function to all main function know which variables should be returned. I thought it was just good practice to prompt the user to guess the secret number until they guess the correct number.
The variable should be $CORRECT_ANSWER or $USER_GUESS because at the point in the if statement, they are equal.
I save and run tree after each change.
But I think I have to start again because, my gitpod workspace was deleted after 14 days, I don’t see my numbers.sh file in my github repositories. I must have not saved it. The dump file seems to be inaccessible. If I redo it I want to try and make it from one table, I had plenty issues getting the separate tables to function together using JOIN. I don’t see it in the code I posted here.