Tell us what’s happening:
Final submission is failing 2 tests yet my code works as expected. Please assist. I have pasted my code below:
#!/bin/bash
# PSQL Connection variable
PSQL="psql -X --username=freecodecamp --dbname=number_guess --no-align --tuples-only -c"
# Function to check if a string is an integer
is_integer() {
re='^[0-9]+$'
[[ "$1" =~ $re ]]
}
# Prompt for username
echo "Enter your username:"
read USERNAME
# Check if the username exists
USER_INFO=$($PSQL "SELECT username, games_played, best_game FROM users WHERE username='$USERNAME'")
if [[ -n "$USER_INFO" ]]
then
This file has been truncated. show original
Your code so far
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Challenge Information:
Number Guessing Game - Build a Number Guessing Game
Teller
April 20, 2025, 9:15pm
2
Welcome to the forum @michaelmainacodes
Which two tests are you failing?
I changed the category from freeCodeCamp Support to Backend Development to better reflect the nature of the post.
Happy coding
1 Like
Thank you for the welcome Teller.
The following 2 are failing:
8. 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
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
In either instance, it requests my next guess until I reach the correct #.
Please see below example from just now:
ILM
April 21, 2025, 12:48pm
6
are you counting all tryes? even the ones with invalid numbers?
Yes. In case something happens, I count it as a try, and the numeric columns are set to 0.
ILM
April 21, 2025, 1:31pm
8
here:
while true
do
read GUESS
if ! is_integer "$GUESS"
then
echo "That is not an integer, guess again:"
continue
fi
GUESSES=$((GUESSES + 1))
doesn’t the continue
bring to next iteration? meaning the increase of GUESSES
is not done in the case the guess is not an integer?
Sorry. My mistake there. You are right.
So then that iteration will run in the while loop until the secret number is gotten. Which is catered for in the next set of if statements.
Any idea what I might be missing to lead to both tests failing?
ILM
April 21, 2025, 1:48pm
10
you need to count all guesses, even those with invalid values, have you fixed that already?
Yes I have. Please see below:
#!/bin/bash
# PSQL Connection variable
PSQL="psql -X --username=freecodecamp --dbname=number_guess --no-align --tuples-only -c"
# Function to check if a string is an integer
is_integer() {
re='^[0-9]+$'
[[ "$1" =~ $re ]]
}
# Prompt for username
echo "Enter your username:"
read USERNAME
# Check if the username exists
USER_INFO=$($PSQL "SELECT username, games_played, best_game FROM users WHERE username='$USERNAME'")
if [[ -n "$USER_INFO" ]];
then
This file has been truncated. show original
ILM
April 21, 2025, 3:31pm
12
and the tests now still say the same thing?
Yes. Unfortunately.
I’ve even tried reorganizing the code as shown below:
#!/bin/bash
# PSQL Connection variable
PSQL="psql -X --username=freecodecamp --dbname=number_guess --no-align --tuples-only -c"
# Function to check if a string is an integer
is_integer() {
re='^[0-9]+$'
[[ "$1" =~ $re ]]
}
# Prompt for username
echo "Enter your username:"
read USERNAME
# Check if the username exists
USER_INFO=$($PSQL "SELECT username, games_played, best_game FROM users WHERE username='$USERNAME'")
if [[ -n "$USER_INFO" ]];
then
This file has been truncated. show original
Still not passing them.
Had to refactor my code and used a different approach. Was able to pass all tests after that.
1 Like