Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

I’m able to pass each scenario when testing the code myself, but tests are timing out when testing via CodeRode ‘run’ button.

Your code so far

GitHib Repository

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 Edg/131.0.0.0

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

FAILED TEST LOG

✘ SUBTASKS 1.1 :7 Your script should prompt for a username

Error: Timeout of 7500ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (/workspace/project/.freeCodeCamp/test/1.1.test.js)

at listOnTimeout (node:internal/timers:573:17)

at process.processTimers (node:internal/timers:514:7)

✘ SUBTASKS 1.1 :8 Your script should print the correct welcome message for returning users

Error: Timeout of 7500ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (/workspace/project/.freeCodeCamp/test/1.1.test.js)

at listOnTimeout (node:internal/timers:573:17)

at process.processTimers (node:internal/timers:514:7)

✘ SUBTASKS 1.1 :10 Your script should print the correct initial message to prompt a user for a guess

Error: Timeout of 7500ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (/workspace/project/.freeCodeCamp/test/1.1.test.js)

at listOnTimeout (node:internal/timers:573:17)

at process.processTimers (node:internal/timers:514:7)

✘ SUBTASKS 1.1 :12 Your script should print the correct message if users enter a guess other than a number

Error: Timeout of 7500ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (/workspace/project/.freeCodeCamp/test/1.1.test.js)

at listOnTimeout (node:internal/timers:573:17)

at process.processTimers (node:internal/timers:514:7)

Here are the errors I’ve been getting in the Output.

✘ SUBTASKS 1.1 :13 Your script should print the correct message when a game is finished

Error: Timeout of 7500ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (/workspace/project/.freeCodeCamp/test/1.1.test.js)

at listOnTimeout (node:internal/timers:573:17)

at process.processTimers (node:internal/timers:514:7)

✘ SUBTASKS 1.1 :16 You should submit your project while on the “main” branch of your repository with a clean working tree

AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

assert(/On branch main\s/.test(commandOutput) && /working tree clean/.test(commandOutput))

at Context. (test/1.1.test.js:164:5)

at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Hi @mandolin8806

Please post the output from running the script and the contents of the table using SELECT * FROM table_name

Happy coding

I was able to get past the TIMEOUTs I was seeing a few days ago by adding “exit” to the end of my GUESS function, but I’m still not passing after reviewing the formatting on the return statements several times.

username | played_games | best_game
--------------------±-------------±----------
user_1733142673758 | 2 | 170
user_1733142673759 | 5 | 103

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. (test/1.1.test.js:98:5)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
✘ 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. (test/1.1.test.js:133:5)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Hi @mandolin8806

Please post you script.

Did create any user accounts, then play the game?
Try creating a user called Gandalf, play a few games, saving the table contents after each game. Make sure one of the other games beats your best game. Then post the table contents.

Happy coding

#!/bin/bash

PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

echo -e "\n~~~~~ Number Game ~~~~~\n"

#generate random number between 1 and 1k.
RANDOM_NUMBER=$(( ( RANDOM % 1000 )  + 1 ))

USERNAME (){
  echo -e "\nEnter your username:\n"
  read USERNAME
  USERNAME_CHARACTERS=${#USERNAME}
  if 
    [[ $USERNAME_CHARACTERS -lt 23 ]]
    then
    USERNAME_QUERY
    else
    echo -e "\nThat username is not valid, please enter a username less than twenty-two characters."
    USERNAME
  fi
}


USERNAME_QUERY () {
  ##query for username 
  RETURN_USER=$($PSQL "SELECT username FROM number_guess WHERE username = '$USERNAME'")
    if [[ -z $RETURN_USER ]]
      then
      ADD_USER=$($PSQL "INSERT INTO number_guess(username) VALUES ('$USERNAME')")
      echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
      GUESS
      TRIES=0
      NUMBER_GAMES=0
      else
      ##user found
      NUMBER_GAMES=$($PSQL "SELECT played_games FROM number_guess WHERE username = '$USERNAME'")
      BEST_GAME=$($PSQL "SELECT best_game FROM number_guess WHERE username = '$USERNAME'")
      echo -e "\nWelcome back, $RETURN_USER! You have played $NUMBER_GAMES games, and your best game took $BEST_GAME guesses." 
      TRIES=0
      GUESS
    fi
}



GUESS (){
  echo -e "\nGuess the secret number between 1 and 1000:\n"
  read INPUT
  INPUT=$INPUT

    #not an integer
    if [[ ! $INPUT =~ ^[0-9]+$ ]]
      then
        echo -e "That is not an integer, guess again:\n"
        GUESS
      elif [[ $INPUT -lt $RANDOM_NUMBER ]]
      then
        echo -e "It's higher than that, guess again:\n"
        TRIES=$((TRIES+1))
        GUESS
      elif [[ $INPUT -gt $RANDOM_NUMBER ]]
      then
        echo -e "It's lower than that, guess again:\n"
        TRIES=$((TRIES+1))
        GUESS
      else
        TRIES=$((TRIES+1))
        NUMBER_GAMES=$((NUMBER_GAMES+1))
        echo -e "\nYou guessed it in $TRIES tries. The secret number was $RANDOM_NUMBER. Nice job!"
        if [[ -z $BEST_GAME || $BEST_GAME > $TRIES ]] 
          then
            # update best game and total games
            UPDATE_USER_RESULT="$($PSQL "UPDATE number_guess SET played_games = $NUMBER_GAMES, best_game = $TRIES WHERE username = '$USERNAME'")"
          else 
            # otherwise only update total games
            UPDATE_USER_RESULT="$($PSQL "UPDATE number_guess SET played_games = $NUMBER_GAMES WHERE username = '$USERNAME'")"
         fi
        fi
    exit 0
}
USERNAME

that’s not the table content, you will need to collaborate with the people trying to help you to get help

Here are the tables each round:

username | played_games | best_game
--------------------±-------------±----------
user_1733257543834 | 2 | 594
user_1733257543835 | 5 | 186
gandalf | 1 | 8

username | played_games | best_game
--------------------±-------------±----------
user_1733257543834 | 2 | 594
user_1733257543835 | 5 | 186
gandalf | 2 | 8

username | played_games | best_game
--------------------±-------------±----------
user_1733257543834 | 2 | 594
user_1733257543835 | 5 | 186
500 | 1 | 11
gandalf | 3 | 8

  username      | played_games | best_game 

--------------------±-------------±----------
user_1733257543834 | 2 | 594
user_1733257543835 | 5 | 186
500 | 1 | 11
gandalf | 4 | 1

Play one game with username Bilbo, this time give an invalid input for one of the tries. You will need to write down each input, including the invalid one.

Then check the table, does the number of tries equal the number of tries you wrote down.

Happy coding

Appears to be correct.

Guesses:

  1. 500
  2. Not A Number
  3. 750
  4. 600
  5. 650
  6. 675
  7. 680
  8. 690
  9. 695
  10. 700
  11. 725
  12. 740
  13. 73
  14. 0
  15. 730
  16. 735
  17. 738
  18. 7337
  19. 737

username | played_games | best_game
--------------------±-------------±----------
user_1733412646626 | 2 | 797
user_1733412646627 | 5 | 105
user_1733412668933 | 2 | 366
user_1733412668934 | 5 | 440
Bilbo | 1 | 19

When the input does not start with a number, the TRIES variable does not increment in the script.

If you want, test again with a new user, using another invalid input, maybe a word.