Tell us what’s happening:
Tried in some different ways to finish this challenge, but couldn’t. Only one test’s not passing, can someone give me a tip or smth? Saw some others that were having the same issue so wanted to have a lead of it, any thoughts?
Your code so far
#!/bin/bash
PSQL=“psql --username=freecodecamp --dbname=number_guess -t --no-align -c”
SECRETNUMBER=$(( RANDOM % 1000 + 1 ))
GLOBAL_NUMBER_GUESSES=0
echo -e “\nEnter your username:”
read INPUT_USERNAME
USERNAME=$($PSQL “SELECT username FROM users WHERE username = ‘$INPUT_USERNAME’”)
GUESS_NUMBER() {
NUMBER_GUESSES=0
echo -e “\nGuess the secret number between 1 and 1000:”
read USER_GUESS
(( NUMBER_GUESSES++ ))
while ! [[ $USER_GUESS =~ [1]+$ ]]
do
echo “That is not an integer, guess again:”
read USER_GUESS
done
while [[ $USER_GUESS -ne $SECRETNUMBER ]]
do
if [[ $USER_GUESS -gt $SECRETNUMBER ]]
then
echo -e “\nIt’s lower than that, guess again:”
else
echo -e “\nIt’s higher than that, guess again:”
fi
read USER_GUESS
(( NUMBER_GUESSES++ ))
done
echo -e “\nYou guessed it in $NUMBER_GUESSES tries. The secret number was $SECRETNUMBER. Nice job!”
GLOBAL_NUMBER_GUESSES=$NUMBER_GUESSES
exit
}
if [[ -z $USERNAME ]]
then
echo -e “\nWelcome, $INPUT_USERNAME! It looks like this is your first time here.”
GUESS_NUMBER
$PSQL “INSERT INTO users(username, games_played, best_game) VALUES(‘$INPUT_USERNAME’, 1, $GLOBAL_NUMBER_GUESSES)”
else
USERNAME_ID=$($PSQL “SELECT user_id FROM users WHERE username = ‘$USERNAME’”)
BEST_GAME=$($PSQL “SELECT best_game FROM users WHERE user_id = ‘$USERNAME_ID’”)
GAMES_PLAYED=$($PSQL “SELECT games_played FROM users WHERE user_id = ‘$USERNAME_ID’”)
echo -e “\nWelcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses.”
GUESS_NUMBER
(( GAMES_PLAYED++ ))
if [[ $GLOBAL_NUMBER_GUESSES -lt $BEST_GAME ]]
then
$PSQL “UPDATE users SET games_played = $GAMES_PLAYED, best_game = $GLOBAL_NUMBER_GUESSES WHERE user_id = ‘$USERNAME_ID’”
else
$PSQL “UPDATE users SET games_played = $GAMES_PLAYED, best_game = $BEST_GAME WHERE user_id = ‘$USERNAME_ID’”
fi
fi
Your browser information:
User Agent is: Chrome 114.0.5735.199
Challenge: Number Guessing Game - Build a Number Guessing Game
Link to the challenge:
0-9 ↩︎