Number Guessing Game - Build a Number Guessing Game - SUBTASKS 1.1 :11

Hello, I’m currently unable to pass the SUBTASK:

SUBTASKS 1.1 :11 Your script should print the correct messages if they do not guess the correct number:

I did everything as required but the task didn’t seem to accept my solution. However I have achieved the goal there.

image

Other than that only task, I have completed all other requirements so I’m really looking forward to earning this certification. Please help me to understand what I was doing wrong.

Thank you in advance!

My dump files

My browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Number Guessing Game - Build a Number Guessing Game

Link to the challenge:

So I did some experiments and identified the issue but I haven’t got a solution to fix it yet.

In my code block for checking the input’s condition, it was like this:

MAIN_MENU(){
read NUMB

if [[ $NUMB =~ $REGEX ]]
then
  if [[ $NUMB -gt $RANDOM_NUMB ]]
  then
  CURRENT_STAT=$(( $CURRENT_STAT + 1 ))
  echo -e "\nIt's lower than that, guess again:"
  MAIN_MENU 
  elif [[ $NUMB -lt $RANDOM_NUMB ]]
  then
  CURRENT_STAT=$(( $CURRENT_STAT + 1 ))
  echo -e "\nIt's higher than that, guess again:"
  MAIN_MENU 
  elif [[ $NUMB == $RANDOM_NUMB ]]
  then
  CURRENT_STAT=$(( $CURRENT_STAT + 1 ))
  echo -e "\nYou guessed it in $CURRENT_STAT tries. The secret number was $RANDOM_NUMB. Nice job!"
    if [[ -z $GAMES_PLAYED_COUNT ]]
    then
    GAMES_PLAYED_COUNT=1
    else
    GAMES_PLAYED_COUNT=$(( $GAMES_PLAYED_COUNT+1 ))
    fi
    if [[ $CURRENT_STAT -lt $BEST_GAME_STAT ]]
    then
    UPDATE_RESULT=$($PSQL "UPDATE games SET games_played=$GAMES_PLAYED_COUNT, best_game=$CURRENT_STAT WHERE user_id=$USER_ID")
    else
    UPDATE_RESULT=$($PSQL "UPDATE games SET games_played=$GAMES_PLAYED_COUNT, best_game=$BEST_GAME_STAT WHERE user_id=$USER_ID")
    fi
  fi
else
CURRENT_STAT=$(( $CURRENT_STAT + 1 ))
echo "That is not an integer, guess again:"
MAIN_MENU
fi
}

I refactored it into:

MAIN_MENU(){
read NUMB
while ! [[ $NUMB =~ $REGEX ]]
do  
CURRENT_STAT=$(( $CURRENT_STAT + 1 ))
echo "That is not an integer, guess again:"
read NUMB
done

if [[ $NUMB -gt $RANDOM_NUMB ]]
then
CURRENT_STAT=$(( $CURRENT_STAT + 1 ))
echo -e "\nIt's lower than that, guess again:"
MAIN_MENU 
elif [[ $NUMB -lt $RANDOM_NUMB ]]
then
CURRENT_STAT=$(( $CURRENT_STAT + 1 ))
echo -e "\nIt's higher than that, guess again:"
MAIN_MENU 
elif [[ $NUMB == $RANDOM_NUMB ]]
then
CURRENT_STAT=$(( $CURRENT_STAT + 1 ))
echo -e "\nYou guessed it in $CURRENT_STAT tries. The secret number was $RANDOM_NUMB. Nice job!"
  if [[ -z $GAMES_PLAYED_COUNT ]]
  then
  GAMES_PLAYED_COUNT=1
  else
  GAMES_PLAYED_COUNT=$(( $GAMES_PLAYED_COUNT+1 ))
  fi
  if [[ $CURRENT_STAT -lt $BEST_GAME_STAT ]]
  then
  UPDATE_RESULT=$($PSQL "UPDATE games SET games_played=$GAMES_PLAYED_COUNT, best_game=$CURRENT_STAT WHERE user_id=$USER_ID")
  else
  UPDATE_RESULT=$($PSQL "UPDATE games SET games_played=$GAMES_PLAYED_COUNT, best_game=$BEST_GAME_STAT WHERE user_id=$USER_ID")
  fi
fi

}

MAIN_MENU

It worked the same as the previous code block, but if I comment out the while block to check if the input is integer or not, it passed the subtask

while ! [[ $NUMB =~ $REGEX ]]
do  
CURRENT_STAT=$(( $CURRENT_STAT + 1 ))
echo "That is not an integer, guess again:"
read NUMB
done

I don’t understand why this code caused the problem, can anyone explain it for me? Thank you.