Hello,
I have been having issues with passing the tests of the final certification project for relational databases. I’ve spent a few hours trying to resolve the issues but I haven’t figured it out. There are 2 tests in particular that won’t seem to let my code pass:
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
And
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
This is my code:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c";
GUESS()
{
USER_ID=$($PSQL "SELECT user_id FROM users WHERE name = '$1';")
NUMBER=0;
RANDOM_NUMBER=$(( RANDOM % 1000 + 1 ));
NUMBER_TRIES=0;
echo "Guess the secret number between 1 and 1000:"
until [[ $NUMBER == $RANDOM_NUMBER ]]
do
read NUMBER;
if [[ ! $NUMBER =~ ^[0-9]+$ ]]
then
echo "That is not an integer, guess again:"
else
if [[ $NUMBER -gt $RANDOM_NUMBER ]]
then
echo "It's lower than that, guess again:"
else
echo "It's higher than that, guess again:"
fi
NUMBER_TRIES=$(( NUMBER_TRIES + 1 ))
fi
done
INSERT_GAME_RESULT=$($PSQL "INSERT INTO games (number_of_guesses, user_id) VALUES ($NUMBER_TRIES, $USER_ID);")
echo "\nYou guessed it in $NUMBER_TRIES tries. The secret number was $RANDOM_NUMBER. Nice job!\n"
}
echo "Enter your username:";
read USER_NAME;
FIND_USER=$($PSQL "SELECT name FROM users WHERE name = '$USER_NAME'");
if [[ -z $FIND_USER ]]
then
echo -e "\nWelcome, $USER_NAME! It looks like this is your first time here.\n";
INSERT_NEW_USER_RESULT=$($PSQL "INSERT INTO users (name) VALUES ('$USER_NAME');");
else
# GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games INNER JOIN users USING(user_id) WHERE name = '$USER_NAME' GROUP BY (user_id)");
# BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM games INNER JOIN users USING(user_id) WHERE name = '$USER_NAME' GROUP BY (user_id)");
USER_ID=$($PSQL "SELECT user_id FROM users WHERE name = '$USER_NAME'")
GAMES_PLAYED=$($PSQL "SELECT COUNT(game_id) FROM games WHERE user_id = $USER_ID GROUP BY user_id")
BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM games WHERE user_id = $USER_ID GROUP BY user_id")
USER_NAME_FROM_DB=$($PSQL "SELECT name FROM users WHERE user_id = $USER_ID")
echo -e "\nWelcome back, $USER_NAME_FROM_DB! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses.\n"
fi
GUESS $USER_NAME
I don’t know what I’m doing wrong, it probably has something to do with the echo like including the -e flag and new line characters or something like that but I just don’t know what it is.
I googled a solution for this project (the shell file) and i found this persons solution on github: https://github.com/SarangWadode/fcc-number-guess/blob/main/number_guess.sh#L5
I pasted their work and their solution also didn’t pass. So I guess it has something to do with the tests themselves or this code also doesn’t work.