Three tasks fail despite my script producing the expected output :
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
The next line printed should be Guess the secret number between 1 and 1000: and input from the user should be read
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
example output:
codeally@44789582b4c9:~/project/number_guessing_game$ ./number_guess.sh
Enter your username:
lol
Welcome back, lol! You have played 2 games, and your best game took 9 guesses.
Guess the secret number between 1 and 1000:
^C
codeally@44789582b4c9:~/project/number_guessing_game$ ./number_guess.sh
Enter your username:
Molly
Welcome, Molly! It looks like this is your first time here.
Guess the secret number between 1 and 1000:
500
It's higher than that, guess again:
750
It's higher than that, guess again:
850
It's lower than that, guess again:
800
It's higher than that, guess again:
825
It's lower than that, guess again:
810
It's higher than that, guess again:
818
It's higher than that, guess again:
820
It's higher than that, guess again:
You guessed it in 8 tries. The secret number was 820. Nice job!
This is my full script:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
CHECK_USERNAME(){
QUERY_USERNAME=$($PSQL "SELECT name FROM users WHERE name = '$USERNAME' LIMIT 1" )
if [[ -z "$QUERY_USERNAME" ]]; then
echo "Welcome, $USERNAME! It looks like this is your first time here."
INSERT_USERNAME=$($PSQL "INSERT INTO users(name) VALUES ('$USERNAME')")
else
echo "$($PSQL "SELECT COUNT(game_id), MIN(guesses) FROM games_info FULL JOIN users ON games_info.player = users.user_id WHERE name = '$USERNAME'")" | while IFS='|' read SUM GUESS
do
echo "Welcome back, $USERNAME! You have played $SUM games, and your best game took $GUESS guesses."
done
fi
}
MAIN_FUNCTION(){
NUMBER_TO_GUESS=$(( $RANDOM % 1000 +1))
declare -i COUNT=0
echo "Enter your username:"
read USERNAME
CHECK_USERNAME
echo "Guess the secret number between 1 and 1000:"
while [[ $NUMBER != $NUMBER_TO_GUESS ]]
do
read NUMBER
if ! [[ $NUMBER =~ ^[0-9]+$ ]]; then
echo "That is not an integer, guess again:"
else
if [[ $NUMBER -gt $NUMBER_TO_GUESS ]]; then
echo "It's lower than that, guess again:"
COUNT+=1
else [[ $NUMBER -lt $NUMBER_TO_GUESS ]];
echo "It's higher than that, guess again:"
COUNT+=1
fi
fi
done
GET_PLAYER_ID=$($PSQL "SELECT user_id FROM users WHERE name = '$USERNAME'")
INSERT_TRIES=$($PSQL "INSERT INTO games_info (guesses, player) VALUES($COUNT, $GET_PLAYER_ID)")
echo "You guessed it in $COUNT tries. The secret number was $NUMBER_TO_GUESS. Nice job!"
exit 0
}
MAIN_FUNCTION
This is the output log:
FAILED TEST LOG
✘ SUBTASKS 1.1 :8 Your script should print the correct welcome message for returning users
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert(re.test(scriptOutput))
at Context.<anonymous> (test/1.1.test.js:98:5)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
✘ SUBTASKS 1.1 :10 Your script should print the correct initial message to prompt a user for a guess
Error: Command failed: ./number_guessing_game/number_guess.sh
at ChildProcess.exithandler (child_process.js:383:12)
at maybeClose (internal/child_process.js:1058:16)
at Socket.<anonymous> (internal/child_process.js:443:11)
at Pipe.<anonymous> (net.js:686:12)
✘ SUBTASKS 1.1 :13 Your script should print the correct message when a game is finished
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert(re.test(scriptOutput))
at Context.<anonymous> (test/1.1.test.js:133:5)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)```