Number guessing game works properly, but do not validate my code

2023-06-28T23:00:00Z
Hi!

I finished my shell script 2 days ago, and I do not figure out why CodeRoad is not totally validated.

  1. number_guess.sh
#!/bin/bash

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

GENERATED_NUMBER=$(shuf -i 1-1000 -n 1)

INSERT_NEW_GAME(){
	INSERT_GAME=$($PSQL "insert into games (id) values ($1)")
	GAME_ID=$($PSQL "select max(game_id) from games")
}

# $1 is the secret number and $2 is COUNT variable
GUESS_THE_SECRET_NUMBER(){
	echo -e "\nGuess the secret number between 1 and 1000:\n"
  read NUMBER_GUESSED

	if [[  $NUMBER_GUESSED =~ ^[0-9]+$  &&  $NUMBER_GUESSED -lt 1001 ]]    
	then
		if (( $1 == $NUMBER_GUESSED ))
		then
			let COUNT++
			FINAL $COUNT $GAME_ID $1
		else
			PROCESS $1 $NUMBER_GUESSED
		fi
	else
		echo "That is not an integer, guess again:"
		GUESS_THE_SECRET_NUMBER $GENERATED_NUMBER
	fi
}

PROCESS(){
	let COUNT++

	if ((  $1 < $2  ))
	then
		echo -e "It's lower than that, guess again:"
		GUESS_THE_SECRET_NUMBER $1 $COUNT
	else
    echo -e "It's higher than that, guess again:"
		GUESS_THE_SECRET_NUMBER $1 $COUNT
	fi
	let COUNT++
	FINAL $COUNT $GAME_ID $1
}

FINAL(){
	UPDATE_GAMES=$($PSQL "update games set guesses = $1 where game_id = $2")
	echo -e "\nYou guessed it in $1 tries. The secret number was $3. Nice job!"
	exit
}

echo -e "\nEnter your username:"
read USERNAME

ID_RESULT=$($PSQL "select id from users where username = '$USERNAME'")

if [[ -z $ID_RESULT ]]
then
	echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
	INSERT_USER=$($PSQL "insert into users(username) values('$USERNAME')")
	ID=$($PSQL "select id from users where username = '$USERNAME'")
	INSERT_NEW_GAME $ID
else
	GAME_RESULT=$($PSQL "select count(game_id),  min(guesses) from games where id = $ID_RESULT group by id")
	echo "$GAME_RESULT" | while IFS="|" read GAMES_PLAYED BEST_GAME
	do 
		echo -e "\nWelcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
	done
	INSERT_NEW_GAME $ID_RESULT
fi

GUESS_THE_SECRET_NUMBER $GENERATED_NUMBER 0

  1. number_guess.sql
--
-- PostgreSQL database dump
--

-- Dumped from database version 12.15 (Ubuntu 12.15-0ubuntu0.20.04.1)
-- Dumped by pg_dump version 12.15 (Ubuntu 12.15-0ubuntu0.20.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: games; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.games (
    game_id integer NOT NULL,
    id integer NOT NULL,
    guesses integer
);


ALTER TABLE public.games OWNER TO freecodecamp;

--
-- Name: games_game_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.games_game_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.games_game_id_seq OWNER TO freecodecamp;

--
-- Name: games_game_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.games_game_id_seq OWNED BY public.games.game_id;


--
-- Name: users; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.users (
    id integer NOT NULL,
    username character varying(22) NOT NULL
);


ALTER TABLE public.users OWNER TO freecodecamp;

--
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.users_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.users_id_seq OWNER TO freecodecamp;

--
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;


--
-- Name: games game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games ALTER COLUMN game_id SET DEFAULT nextval('public.games_game_id_seq'::regclass);


--
-- Name: users id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);


--
-- Data for Name: games; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.games VALUES (1, 1, 12);
INSERT INTO public.games VALUES (2, 1, 9);
INSERT INTO public.games VALUES (3, 1, 25);
INSERT INTO public.games VALUES (4, 2, 15);
INSERT INTO public.games VALUES (5, 3, 7);
INSERT INTO public.games VALUES (6, 2, 3);
INSERT INTO public.games VALUES (7, 2, 5);
INSERT INTO public.games VALUES (8, 2, 10);
INSERT INTO public.games VALUES (9, 3, 11);
INSERT INTO public.games VALUES (10, 3, 19);


--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.users VALUES (1, 'Leticia');
INSERT INTO public.users VALUES (2, 'Mari');
INSERT INTO public.users VALUES (3, 'MARI');


--
-- Name: games_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.games_game_id_seq', 10, true);


--
-- Name: users_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.users_id_seq', 3, true);


--
-- Name: games games_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_pkey PRIMARY KEY (game_id);


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (id);


--
-- Name: games games_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_id_fkey FOREIGN KEY (id) REFERENCES public.users(id);


--
-- PostgreSQL database dump complete
--


Is there any way to debug CodeRoad and see why it is not validating?

And these are the itens not validated yet, although they are working properly:

  • Your script should randomly generate a number that users have to guess;

  • 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;

  • If the username has not been used before, you should print `Welcome, ! It looks like this is your first time here.

  • 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

Thanks in advance for any help!