About last project Number Guessing Game, Relational Databases

hi! I think I already solve this project, but I am not passing the tests. The curios thing is that sometimes I passed the ones that other times I don’t pass. So, my code passes all the testm but not at once. I think it’s because there are random tests.
What can I do?
Here my code

#! /bin/bash

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

SECRET_NUMBER=$((($RANDOM % 1000) + 1 ))

echo  Enter your username:

read USERNAME

# get user_id if exists
USER_ID=$($PSQL "SELECT user_id FROM users WHERE name='$USERNAME' ")

if [[ -z $USER_ID ]]
then
  echo "Welcome, $USERNAME! It looks like this is your first time here."
  # insert new user into db
  INSERT_NEW_USER=$($PSQL "INSERT INTO users(name) values('$USERNAME')")
  USER_ID=$($PSQL "SELECT user_id FROM users WHERE name='$USERNAME' ")
else
  BEST_GAME=$($PSQL "SELECT numb_guesses FROM games WHERE user_id=$USER_ID ORDER BY numb_guesses LIMIT 1")
  GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id=$USER_ID") 
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

echo Guess the secret number between 1 and 1000:

NUMBER_OF_GUESSES=0
FOUND=0
EXPRESSION='^[0-9]+$'
while [[ $INPUT != $SECRET_NUMBER ]]
do
  NUMBER_OF_GUESSES=$(($NUMBER_OF_GUESSES+1))
  read INPUT
  if [[ $INPUT =~ $EXPRESSION ]]
  then
    if [[  $INPUT -eq $SECRET_NUMBER ]]
    then
      echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
      FOUND=1
    else
      if [[ $INPUT -gt $SECRET_NUMBER ]]
      then
        echo "It's lower than that, guess again:"
      else
        echo "It's higher than that, guess again:"
      fi
    fi
  else
    echo That is not an integer, guess again:
  fi
done

if [[ $FOUND ]]
then
  INSERT_GAME=$($PSQL "INSERT INTO games (user_id, numb_guesses) VALUES($USER_ID , $NUMBER_OF_GUESSES) ")
fi


And here my database file:

--
-- 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: games; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.games (
    game_id integer NOT NULL,
    user_id integer NOT NULL,
    numb_guesses integer NOT NULL
);


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 (
    user_id integer NOT NULL,
    name character varying(22) NOT NULL
);


ALTER TABLE public.users OWNER TO freecodecamp;

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

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


ALTER TABLE public.users_user_id_seq OWNER TO freecodecamp;

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

ALTER SEQUENCE public.users_user_id_seq OWNED BY public.users.user_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 user_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

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


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

INSERT INTO public.games VALUES (372, 44, 18);
INSERT INTO public.games VALUES (373, 44, 5);
INSERT INTO public.games VALUES (374, 44, 13);
INSERT INTO public.games VALUES (375, 45, 858);
INSERT INTO public.games VALUES (376, 46, 12);
INSERT INTO public.games VALUES (377, 45, 159);
INSERT INTO public.games VALUES (378, 45, 243);
INSERT INTO public.games VALUES (379, 45, 495);
INSERT INTO public.games VALUES (380, 44, 12);


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

INSERT INTO public.users VALUES (44, 'gabriele');
INSERT INTO public.users VALUES (45, 'user_1684690632882');
INSERT INTO public.users VALUES (46, 'user_1684690632881');
INSERT INTO public.users VALUES (47, 'nina');


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

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


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

SELECT pg_catalog.setval('public.users_user_id_seq', 47, 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 (user_id);


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

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(user_id);


--
-- PostgreSQL database dump complete
--


Note: now the name column is up to 30 characters, but originnaly it was of 22 char. It doesn’t change anything

Can you share a screenshot of the test output please?
Open up a terminal, click on the OUTPUT tab and select CodeRoad(tests), then hit the Run button on the tests.

Sure!
case 1:

case 2:


case 1 and case 2 are two differents executions!

It’s a timeout issue with the testing process.
You can manually alter the timeouts in the test files and that should resolve those errors.

In the terminal (from the main project folder):

cd .freeCodeCamp/test
cat utils.js
cat 1.1.test.js

Find this function in both files and note the timeout value for each (most likely 1000 or 10000, not 100000 as here):

Untitled

In your terminal enter the following two commands (replacing <current_timeout> with the noted values):

sed 's/<current_timeout>/100000/' -i /home/codeally/project/.freeCodeCamp/test/1.1.test.js
sed 's/<current_timeout>/100000/' -i /home/codeally/project/.freeCodeCamp/test/utils.js

It’s a simple search and replace command which will adjust the timeout to 100secs.
Then run the tests again and see what happens.

1 Like

Thank you! that fixed part of the problem, because now I only have one task to solve. Is this one: " The next line printed should be Guess the secret number between 1 and 1000: and input from the user should be read".


Maybe the problem is that i’m calling the read function inside the while cycle?