So i wrote all the code needed and when i run my code it seems to be outputting the correct result. But tests 8 and 13 aren’t passing no matter what i do. I’ve seen other people struggling with the same issue.
Here’s my bash code
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess --no-align --tuples-only -c"
GUESS_COUNT=0
# generate random number
RANDOM_NUMBER=$[ $RANDOM % 1000 + 1 ]
# get user's username
echo "Enter your username:"
read USER
# check if username is in the database
USERNAME_EXISTS=$($PSQL "SELECT username FROM user_info WHERE username='$USER'")
# if not found
if [[ -z $USERNAME_EXISTS ]]
then
# insert username in database
INSERT_USER_INFO_RESULT=$($PSQL "INSERT INTO user_info(username, games_played) VALUES('$USER', 0)")
# get user info
GAMES_PLAYED=$($PSQL "SELECT games_played FROM user_info WHERE username='$USER'")
BEST_GAME=$($PSQL "SELECT best_game FROM user_info WHERE username='$USER'")
USERNAME=$($PSQL "SELECT username FROM user_info WHERE username='$USER'")
# greet user
echo "Welcome, $USERNAME! It looks like this is your first time here."
# if username is found
else
# get user info
GAMES_PLAYED=$($PSQL "SELECT games_played FROM user_info WHERE username='$USER'")
BEST_GAME=$($PSQL "SELECT best_game FROM user_info WHERE username='$USER'")
USERNAME=$($PSQL "SELECT username FROM user_info WHERE username='$USER'")
# greet user
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
GUESS () {
read USER_GUESS
# if the guess is not an integer
if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]
then
echo "That is not an integer, guess again:"
GUESS
# if the guess is higher than the secret number
elif [[ $USER_GUESS -gt $RANDOM_NUMBER ]]
then
let "GUESS_COUNT++"
echo "It's lower than that, guess again:"
GUESS
# if the guess is lower
elif [[ $USER_GUESS -lt $RANDOM_NUMBER ]]
then
let "GUESS_COUNT++"
echo "It's higher than that, guess again:"
GUESS
# if the guess is correct
elif [[ $USER_GUESS -eq $RANDOM_NUMBER ]]
then
let "GUESS_COUNT++"
echo "You guessed it in $GUESS_COUNT tries. The secret number was $RANDOM_NUMBER. Nice job!"
echo $GUESS_COUNT
fi
}
echo "Guess the secret number between 1 and 1000:"
GUESS
# UPDATE USER INFO
# update games_played
UPDATE_GAMES_PLAYED=$($PSQL "UPDATE user_info SET games_played=user_info.games_played+1 WHERE username='$USERNAME'")
# update best_game
if [[ -z $BEST_GAME || $BEST_GAME -gt $GUESS_COUNT ]]
then
UPDATE_BEST_GAME=$($PSQL "UPDATE user_info SET best_game=$GUESS_COUNT WHERE username='$USERNAME'")
fi
and here’s the database dump
--
-- PostgreSQL database dump
--
-- Dumped from database version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)
-- Dumped by pg_dump version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
DROP DATABASE number_guess;
--
-- Name: number_guess; Type: DATABASE; Schema: -; Owner: freecodecamp
--
CREATE DATABASE number_guess WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';
ALTER DATABASE number_guess OWNER TO freecodecamp;
\connect number_guess
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: user_info; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.user_info (
username character varying(30),
games_played integer,
best_game integer
);
ALTER TABLE public.user_info OWNER TO freecodecamp;
--
-- Data for Name: user_info; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
INSERT INTO public.user_info VALUES ('user_1667755946176', 2, 324);
INSERT INTO public.user_info VALUES ('user_1667755946177', 5, 123);
INSERT INTO public.user_info VALUES ('user_1667756067719', 2, 15);
INSERT INTO public.user_info VALUES ('user_1667756067720', 5, 191);
INSERT INTO public.user_info VALUES ('user_1667756658905', 2, 575);
INSERT INTO public.user_info VALUES ('user_1667756658906', 5, 296);
INSERT INTO public.user_info VALUES ('Ritvars', 4, 8);
INSERT INTO public.user_info VALUES ('user_1667756843121', 2, 372);
INSERT INTO public.user_info VALUES ('user_1667756843122', 5, 246);
--
-- PostgreSQL database dump complete
--
Also i’m not sure if i need to count it as a guess when the user inputs a non integer but regardless i to run the code with and without the guess counting but it still didn’t pass.