I’ve been stuck for a few hours trying to make it work. I don’t think is on my end now because sometimes Test 13 fails even though I haven’t done anything.
In the forum there’s like 5 post of people running into similar issues with the tests in these project although nothing they did works or applies to me.
Here’s my code:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
NUMBER=$((RANDOM % 1000 + 1))
NUMBER_OF_GUESSES=0
GAMES_PLAYED=0
GUESS_LOOP() {
read GUESS
if [[ $GUESS =~ ^[0-9]+$ ]]
then
if [[ $GUESS = $NUMBER ]]
then
let NUMBER_OF_GUESSES++
echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $NUMBER. Nice job!"
#Update best game yet
BEST_GAME=$($PSQL "SELECT best_game FROM guesser WHERE username = '$USERNAME'")
if [[ $BEST_GAME -eq 0 || $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
then
UPDATE_BEST_GAME_RESULT=$($PSQL "UPDATE guesser SET best_game = '$NUMBER_OF_GUESSES' WHERE username = '$USERNAME'")
fi
else if [[ $GUESS > $NUMBER ]]
then
let NUMBER_OF_GUESSES++
echo "It's lower than that, guess again:"
GUESS_LOOP
else
let NUMBER_OF_GUESSES++
echo "It's higher than that, guess again:"
GUESS_LOOP
fi
fi
else
echo "That is not an integer, guess again:"
GUESS_LOOP
fi
}
echo "Enter your username:"
read USERNAME
GUESSER=$($PSQL "SELECT * FROM guesser WHERE username = '$USERNAME'")
if [[ -z $GUESSER ]]
then
echo "Welcome, $USERNAME! It looks like this is your first time here."
INSERT_USER_RESULT=$($PSQL "INSERT INTO guesser(username, games_played, best_game) VALUES('$USERNAME', 1, 0)")
else
echo "$GUESSER" | while IFS="|" read USERNAME GAMES_PLAYED BEST_GAME
do
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
done
fi
#Update number of games played
GAMES_PLAYED=$($PSQL "SELECT games_played FROM guesser WHERE username = '$USERNAME'")
let GAMES_PLAYED++
UPDATE_GAMES_PLAYED_RESULT=$($PSQL "UPDATE guesser SET games_played = '$GAMES_PLAYED' WHERE username = '$USERNAME'")
echo "Guess the secret number between 1 and 1000:"
GUESS_LOOP
Here’s the SQL 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: guesser; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.guesser (
username character varying(16) NOT NULL,
games_played integer,
best_game integer
);
ALTER TABLE public.guesser OWNER TO freecodecamp;
--
-- Data for Name: guesser; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
--
-- Name: guesser guesser_username_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.guesser
ADD CONSTRAINT guesser_username_key UNIQUE (username);
--
-- PostgreSQL database dump complete
--
I tried moving around where does the games_played variable gets updated if at the end of the game or after entering the username but it doesn’t seem to make a difference.
Hope someone can help me.