Number Guessing Game Failed in step 8

Hi all,

This is my first post in this forum. So, I had a problem finishing the number guessing game for test number 8. I’ve already read other posts about related issues but still cannot solve it.

Below is the output of CodeRoad (Test)

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 process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Below is my code for number_guess.sh

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

# generate actual number
ACTUAL_NUM=$(( RANDOM % 1000 + 1 ))
# set number guesses
NUMBER_OF_GUESSES=0

MAIN_FN(){
  # print asking username
  echo "Enter your username:"
  read USERNAME

  # get player id in db
  PLAYER_ID=$($PSQL "SELECT player_id FROM players WHERE username = '$USERNAME'");

  # check username in db
  if [[ -z $PLAYER_ID ]]
  then
    # print welcome msg for 1st time player
    echo "Welcome, $USERNAME! It looks like this is your first time here."
    # insert new data in db
    INSERT_PLAYER=$($PSQL "INSERT INTO players(username) VALUES('$USERNAME')")
    # get player id in db
    PLAYER_ID=$($PSQL "SELECT player_id FROM players WHERE username = '$USERNAME'");
  else
    USERNAME_DB=$($PSQL "SELECT username FROM players WHERE player_id = $PLAYER_ID" | sed 's/ //g' );
    GAMES_PLAYED=$($PSQL "SELECT games_played FROM players WHERE player_id = $PLAYER_ID" | sed 's/ //g' );
    BEST_GAME=$($PSQL "SELECT best_game FROM players WHERE player_id = $PLAYER_ID" | sed 's/ //g' );
    
    echo "Welcome back, $USERNAME_DB! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  fi

  # run main function
  GAME_FUNCTION
}

GAME_FUNCTION(){
  
  # get guess number from input
  if [[ $1 ]]
  then
    echo "$1"
  else
    echo "Guess the secret number between 1 and 1000:"
  fi
  read GUESS_NUM
  
  # check if input is not number
  if [[ ! $GUESS_NUM =~ ^[0-9]+$ ]]
  then
    GAME_FUNCTION "That is not an integer, guess again:"
  else    
    NUMBER_OF_GUESSES=$(( $NUMBER_OF_GUESSES + 1 ))

    # the input is number, check if the numbers is same
    if [[ $ACTUAL_NUM = $GUESS_NUM ]]
    then
      # print message
      echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $GUESS_NUM. Nice job!"

      # add best_game if it is not 0 and few than prev best_game
      PREV_BEST_GAME=$($PSQL "SELECT best_game FROM players WHERE player_id = $PLAYER_ID")

      if [[ $PREV_BEST_GAME -eq 0 || $NUMBER_OF_GUESSES -lt $PREV_BEST_GAME ]]
      then
        UPDATE_GAMES_PLAYED=$($PSQL "UPDATE players SET games_played = games_played + $NUMBER_OF_GUESSES WHERE player_id = $PLAYER_ID")
        UPDATE_BEST_GAME=$($PSQL "UPDATE players SET best_game = $NUMBER_OF_GUESSES WHERE player_id = $PLAYER_ID")
      fi
    else
      # check if lower or higher
      if [[ $GUESS_NUM -gt $ACTUAL_NUM ]]
      then
        GAME_FUNCTION "It's lower than that, guess again:"
      elif [[ $GUESS_NUM -lt $ACTUAL_NUM ]]
      then
        GAME_FUNCTION "It's higher than that, guess again:"
      fi
    fi
  fi
}

MAIN_FN

And, below is my SQL dump

--
-- PostgreSQL database dump
--

-- Dumped from database version 12.17 (Ubuntu 12.17-1.pgdg22.04+1)
-- Dumped by pg_dump version 12.17 (Ubuntu 12.17-1.pgdg22.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: players; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.players (
    player_id integer NOT NULL,
    username character varying(25) NOT NULL,
    games_played integer DEFAULT 0,
    best_game integer DEFAULT 0
);


ALTER TABLE public.players OWNER TO freecodecamp;

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

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


ALTER TABLE public.players_player_id_seq OWNER TO freecodecamp;

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

ALTER SEQUENCE public.players_player_id_seq OWNED BY public.players.player_id;


--
-- Name: players player_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.players ALTER COLUMN player_id SET DEFAULT nextval('public.players_player_id_seq'::regclass);


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

INSERT INTO public.players VALUES (63, 'John', 2, 2);
INSERT INTO public.players VALUES (113, 'user_1715252128054', 1426, 266);
INSERT INTO public.players VALUES (64, 'jOHN', 19, 5);
INSERT INTO public.players VALUES (99, 'user_1715251850621', 120, 120);
INSERT INTO public.players VALUES (65, 'user_1715249638162', 10, 10);
INSERT INTO public.players VALUES (66, 'user_1715249638161', 46, 46);
INSERT INTO public.players VALUES (67, 'user_1715249668839', 0, 0);
INSERT INTO public.players VALUES (68, 'user_1715249668838', 0, 0);
INSERT INTO public.players VALUES (70, 'user_1715249676659', 846, 846);
INSERT INTO public.players VALUES (69, 'user_1715249676660', 315, 137);
INSERT INTO public.players VALUES (88, 'user_1715250958553', 1135, 318);
INSERT INTO public.players VALUES (71, 'user_1715249782364', 65, 65);
INSERT INTO public.players VALUES (100, 'user_1715251850620', 263, 80);
INSERT INTO public.players VALUES (72, 'user_1715249782363', 325, 325);
INSERT INTO public.players VALUES (87, 'user_1715250958554', 3013, 149);
INSERT INTO public.players VALUES (74, 'user_1715250030443', 392, 392);
INSERT INTO public.players VALUES (101, 'user_1715251911143', 205, 205);
INSERT INTO public.players VALUES (73, 'user_1715250030444', 1149, 159);
INSERT INTO public.players VALUES (102, 'user_1715251911142', 582, 270);
INSERT INTO public.players VALUES (76, 'user_1715250199729', 715, 332);
INSERT INTO public.players VALUES (75, 'user_1715250199730', 456, 36);
INSERT INTO public.players VALUES (90, 'user_1715250983905', 1359, 454);
INSERT INTO public.players VALUES (116, 'user_1715252506546', 281, 281);
INSERT INTO public.players VALUES (89, 'user_1715250983906', 1689, 275);
INSERT INTO public.players VALUES (78, 'user_1715250290076', 838, 42);
INSERT INTO public.players VALUES (77, 'user_1715250290077', 1403, 282);
INSERT INTO public.players VALUES (79, 'user_1715250685232', 19, 19);
INSERT INTO public.players VALUES (115, 'user_1715252506547', 1560, 109);
INSERT INTO public.players VALUES (104, 'user_1715251922119', 331, 331);
INSERT INTO public.players VALUES (80, 'user_1715250685231', 274, 121);
INSERT INTO public.players VALUES (92, 'user_1715251458405', 0, 0);
INSERT INTO public.players VALUES (103, 'user_1715251922120', 2017, 195);
INSERT INTO public.players VALUES (91, 'user_1715251458406', 0, 0);
INSERT INTO public.players VALUES (82, 'user_1715250707851', 0, 0);
INSERT INTO public.players VALUES (81, 'user_1715250707852', 0, 0);
INSERT INTO public.players VALUES (118, 'user_1715252772466', 687, 687);
INSERT INTO public.players VALUES (94, 'user_1715251586773', 480, 480);
INSERT INTO public.players VALUES (106, 'user_1715251927551', 234, 234);
INSERT INTO public.players VALUES (105, 'user_1715251927552', 830, 135);
INSERT INTO public.players VALUES (93, 'user_1715251586774', 2182, 210);
INSERT INTO public.players VALUES (84, 'user_1715250730960', 502, 198);
INSERT INTO public.players VALUES (83, 'user_1715250730961', 1997, 185);
INSERT INTO public.players VALUES (117, 'user_1715252772467', 1803, 341);
INSERT INTO public.players VALUES (86, 'user_1715250747973', 207, 207);
INSERT INTO public.players VALUES (85, 'user_1715250747974', 751, 197);
INSERT INTO public.players VALUES (96, 'user_1715251721088', 10, 10);
INSERT INTO public.players VALUES (95, 'user_1715251721089', 456, 149);
INSERT INTO public.players VALUES (97, 'user_1715251729044', 159, 159);
INSERT INTO public.players VALUES (119, 'user_1715252779997', 470, 188);
INSERT INTO public.players VALUES (98, 'user_1715251729043', 831, 359);
INSERT INTO public.players VALUES (108, 'user_1715251931729', 1157, 184);
INSERT INTO public.players VALUES (120, 'user_1715252779996', 226, 226);
INSERT INTO public.players VALUES (107, 'user_1715251931730', 1089, 45);
INSERT INTO public.players VALUES (109, 'user_1715252061142', 26, 26);
INSERT INTO public.players VALUES (110, 'user_1715252061141', 1371, 466);
INSERT INTO public.players VALUES (122, 'user_1715252885154', 508, 171);
INSERT INTO public.players VALUES (121, 'user_1715252885155', 223, 43);
INSERT INTO public.players VALUES (112, 'user_1715252089119', 1512, 671);
INSERT INTO public.players VALUES (111, 'user_1715252089120', 1568, 236);
INSERT INTO public.players VALUES (114, 'user_1715252128053', 157, 30);


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

SELECT pg_catalog.setval('public.players_player_id_seq', 122, true);


--
-- Name: players players_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.players
    ADD CONSTRAINT players_pkey PRIMARY KEY (player_id);


--
-- PostgreSQL database dump complete
--


1 Like

I noticed that in your “Welcome Back” message you are using $USERNAME_DB.
Shouldn’t you be using $USERNAME ?
Just an observation as i was looking at your code.

Hi @wbailey, thank you for your response. I’ve used the $USERNAME variable before and the test result has failed. But, then I tried to use the username value from db, hence I created a new $USERNAME_DB variable.

According to the step sentences, I thought the username value must be gotten from db. But, yeah, as per right now it still cannot past the test even though I used that $USERNAME_DB variable.

Hi all, finally I solved the issue by updating the code logic regarding the update action when the player succeeds in guessing a number (updating the games played and best game). I follow code logic according to this code as the references.

Here is my github for the final code https://github.com/krisdianwidya/freecodecamp-number-guessing-game