Number Guessing Game task validation issue

Three tasks fail despite my script producing the expected output :

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

The next line printed should be Guess the secret number between 1 and 1000: and input from the user should be read

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

example output:

codeally@44789582b4c9:~/project/number_guessing_game$ ./number_guess.sh 
Enter your username:
lol
Welcome back, lol! You have played 2 games, and your best game took 9 guesses.
Guess the secret number between 1 and 1000:
^C
codeally@44789582b4c9:~/project/number_guessing_game$ ./number_guess.sh 
Enter your username:
Molly
Welcome, Molly! It looks like this is your first time here.
Guess the secret number between 1 and 1000:
500
It's higher than that, guess again:
750
It's higher than that, guess again:
850
It's lower than that, guess again:
800
It's higher than that, guess again:
825
It's lower than that, guess again:
810
It's higher than that, guess again:
818
It's higher than that, guess again:
820
It's higher than that, guess again:
You guessed it in 8 tries. The secret number was 820. Nice job!

This is my full script:

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

CHECK_USERNAME(){
  QUERY_USERNAME=$($PSQL "SELECT name FROM users WHERE name = '$USERNAME' LIMIT 1" )
  if [[ -z "$QUERY_USERNAME" ]]; then
    echo "Welcome, $USERNAME! It looks like this is your first time here."
    INSERT_USERNAME=$($PSQL "INSERT INTO users(name) VALUES ('$USERNAME')")
  else
    echo "$($PSQL "SELECT COUNT(game_id), MIN(guesses) FROM games_info FULL JOIN users ON games_info.player = users.user_id WHERE name = '$USERNAME'")" | while IFS='|' read SUM GUESS
    do
    echo "Welcome back, $USERNAME! You have played $SUM games, and your best game took $GUESS guesses."
    done
  fi
}

MAIN_FUNCTION(){
NUMBER_TO_GUESS=$(( $RANDOM % 1000 +1))
  declare -i COUNT=0
  echo "Enter your username:"
  read USERNAME
  CHECK_USERNAME
  echo "Guess the secret number between 1 and 1000:"
  while [[ $NUMBER != $NUMBER_TO_GUESS ]]
  do
  read NUMBER
  if ! [[ $NUMBER =~ ^[0-9]+$ ]]; then
    echo "That is not an integer, guess again:"
    else
    if [[ $NUMBER -gt $NUMBER_TO_GUESS ]]; then
      echo "It's lower than that, guess again:"
      COUNT+=1
    else [[ $NUMBER -lt $NUMBER_TO_GUESS ]];
      echo "It's higher than that, guess again:"
      COUNT+=1
    fi
  fi
  done
    GET_PLAYER_ID=$($PSQL "SELECT user_id FROM users WHERE name = '$USERNAME'")
    INSERT_TRIES=$($PSQL "INSERT INTO games_info (guesses, player) VALUES($COUNT, $GET_PLAYER_ID)")
    echo "You guessed it in $COUNT tries. The secret number was $NUMBER_TO_GUESS. Nice job!"
    exit 0
}
MAIN_FUNCTION

This is the output log:

FAILED TEST LOG

✘ SUBTASKS 1.1 :8 Your script should print the correct welcome message for returning users

AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

assert(re.test(scriptOutput))

at Context.<anonymous> (test/1.1.test.js:98:5)

at runMicrotasks (<anonymous>)

at processTicksAndRejections (internal/process/task_queues.js:95:5)

✘ SUBTASKS 1.1 :10 Your script should print the correct initial message to prompt a user for a guess

Error: Command failed: ./number_guessing_game/number_guess.sh

at ChildProcess.exithandler (child_process.js:383:12)

at maybeClose (internal/child_process.js:1058:16)

at Socket.<anonymous> (internal/child_process.js:443:11)

at Pipe.<anonymous> (net.js:686:12)

✘ SUBTASKS 1.1 :13 Your script should print the correct message when a game is finished

AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

assert(re.test(scriptOutput))

at Context.<anonymous> (test/1.1.test.js:133:5)

at runMicrotasks (<anonymous>)

at processTicksAndRejections (internal/process/task_queues.js:95:5)```

Can you provide an SQL dump please, so I can test your code against your database?

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

CREATE TABLE public.games_info (
    game_id integer NOT NULL,
    player integer NOT NULL,
    guesses integer NOT NULL
);


ALTER TABLE public.games_info OWNER TO freecodecamp;

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

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


ALTER TABLE public.games_info_game_id_seq OWNER TO freecodecamp;

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

ALTER SEQUENCE public.games_info_game_id_seq OWNED BY public.games_info.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_info game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games_info ALTER COLUMN game_id SET DEFAULT nextval('public.games_info_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_info; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.games_info VALUES (1, 1, 6);
INSERT INTO public.games_info VALUES (2, 2, 281);
INSERT INTO public.games_info VALUES (3, 3, 367);
INSERT INTO public.games_info VALUES (4, 3, 33);
INSERT INTO public.games_info VALUES (5, 2, 186);
INSERT INTO public.games_info VALUES (6, 2, 458);
INSERT INTO public.games_info VALUES (7, 2, 571);
INSERT INTO public.games_info VALUES (8, 4, 8);
INSERT INTO public.games_info VALUES (9, 4, 954);
INSERT INTO public.games_info VALUES (10, 5, 748);
INSERT INTO public.games_info VALUES (11, 5, 507);
INSERT INTO public.games_info VALUES (12, 4, 67);
INSERT INTO public.games_info VALUES (13, 4, 765);
INSERT INTO public.games_info VALUES (14, 4, 445);
INSERT INTO public.games_info VALUES (15, 1, 9);
INSERT INTO public.games_info VALUES (16, 6, 696);
INSERT INTO public.games_info VALUES (17, 6, 554);
INSERT INTO public.games_info VALUES (18, 7, 258);
INSERT INTO public.games_info VALUES (19, 7, 732);
INSERT INTO public.games_info VALUES (20, 6, 226);
INSERT INTO public.games_info VALUES (21, 6, 880);
INSERT INTO public.games_info VALUES (22, 6, 56);
INSERT INTO public.games_info VALUES (23, 1, 2);
INSERT INTO public.games_info VALUES (24, 8, 144);
INSERT INTO public.games_info VALUES (25, 8, 82);
INSERT INTO public.games_info VALUES (26, 9, 461);
INSERT INTO public.games_info VALUES (27, 9, 355);
INSERT INTO public.games_info VALUES (28, 8, 933);
INSERT INTO public.games_info VALUES (29, 8, 81);
INSERT INTO public.games_info VALUES (30, 8, 563);
INSERT INTO public.games_info VALUES (31, 10, 869);
INSERT INTO public.games_info VALUES (32, 10, 685);
INSERT INTO public.games_info VALUES (33, 11, 283);
INSERT INTO public.games_info VALUES (34, 11, 898);
INSERT INTO public.games_info VALUES (35, 10, 913);
INSERT INTO public.games_info VALUES (36, 10, 674);
INSERT INTO public.games_info VALUES (37, 10, 79);
INSERT INTO public.games_info VALUES (38, 12, 9);


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

INSERT INTO public.users VALUES (1, 'BOb');
INSERT INTO public.users VALUES (2, 'user_1690450633087');
INSERT INTO public.users VALUES (3, 'user_1690450633086');
INSERT INTO public.users VALUES (4, 'user_1690450797642');
INSERT INTO public.users VALUES (5, 'user_1690450797641');
INSERT INTO public.users VALUES (6, 'user_1690451346824');
INSERT INTO public.users VALUES (7, 'user_1690451346823');
INSERT INTO public.users VALUES (8, 'user_1690452319924');
INSERT INTO public.users VALUES (9, 'user_1690452319923');
INSERT INTO public.users VALUES (10, 'user_1690452366687');
INSERT INTO public.users VALUES (11, 'user_1690452366685');
INSERT INTO public.users VALUES (12, 'lol');


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

SELECT pg_catalog.setval('public.games_info_game_id_seq', 38, true);


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

SELECT pg_catalog.setval('public.users_user_id_seq', 12, true);


--
-- Name: games_info games_info_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games_info
    ADD CONSTRAINT games_info_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_info games_info_player_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games_info
    ADD CONSTRAINT games_info_player_fkey FOREIGN KEY (player) REFERENCES public.users(user_id);


--
-- PostgreSQL database dump complete
--

Sorry for the slow reply - hadn’t spotted your response.
I got your code to pass, after making a couple of changes to number_guess.sh:

  1. When guessing the number, your if ... else code block prints “It’s higher than that, guess again:” even when you guess the correct number, as it’s an else condition. I refactored that code block to change all else ... if to elif.
  2. Instead of putting COUNT+=1 after each condition, move that up to the top of the while loop, immediately after do. This will still keep count correctly but is more efficient and I think the key to the tests passing.

I hope that helps!

So I did what you suggested (and it did work, thanks a lot !) but I had to run the validation script several times to pass all the tests because everytime one or two of the tasks failed randomly. I’ve seen this issue in other topics on this forum :

Thanks again for your help !