Hi guys,
First time posting here, i’ve search everywhere and my frustration is peaking right now.
There are some tests failing randomly but at this point i can live with that, the problem is the app is working as intended and i could never get test 8 and 13 passed!
Does anyone has any idea how to fix this?
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
echo "Enter your username:"
read USERNAME
GET_USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
if [[ -z $GET_USER_ID ]]
then
INSERT_NEW_USER=$($PSQL "INSERT INTO users(username, games_played, best_game) VALUES('$USERNAME', 0, 0)")
if [[ $INSERT_NEW_USER == "INSERT 0 1" ]]
then
echo "Welcome, $USERNAME! It looks like this is your first time here."
fi
GET_USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
GET_GAMES_PLAYED=0
GET_BEST_GAME=0
else
GET_GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE user_id=$GET_USER_ID")
GET_BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE user_id=$GET_USER_ID")
echo -e "\nWelcome back, $USERNAME! You have played $GET_GAMES_PLAYED games, and your best game took $GET_BEST_GAME guesses."
fi
SECRET_NUMBER=$((1 + $RANDOM % 1000))
echo "Guess the secret number between 1 and 1000:"
echo "SECRET IS $SECRET_NUMBER"
GUESSES=0
GUESS_NUMBER=0
while [[ -z $GUESS_NUMBER ]] || [[ $GUESS_NUMBER -ne $SECRET_NUMBER ]]
do
read GUESS_NUMBER
if [[ $GUESS_NUMBER =~ ^[1-9][0-9]{0,2}$|1000$ ]];
then
if [[ $GUESS_NUMBER -gt $SECRET_NUMBER ]];
then
echo "It's lower than that, guess again:"
elif [[ $GUESS_NUMBER -lt $SECRET_NUMBER ]]
then
echo "It's higher than that, guess again:"
fi
GUESSES=$((GUESSES+1))
else
echo "That is not an integer, guess again:"
fi
#GUESSES=$((GUESSES+1))
done
GET_GAMES_PLAYED=$((GET_GAMES_PLAYED+1))
if [[ $GUESSES -lt $GET_BEST_GAME || $GET_BEST_GAME == 0 ]];
then
UPDATE_RESULTS=$($PSQL "UPDATE users SET best_game = $GUESSES, games_played = $GET_GAMES_PLAYED WHERE user_id = $GET_USER_ID")
else
UPDATE_RESULTS=$($PSQL "UPDATE users SET games_played = $GET_GAMES_PLAYED WHERE user_id = $GET_USER_ID")
fi
echo "You guessed it in $GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
Thank you so much!
Hi there and welcome to our community!
Could you check the output of the CodeRoad tests please?
When you have a terminal open, click on the OUTPUT tab and select CodeRoad Tests from the dropdown menu (on the right of the window).
Then hit Run on the tests and see what output you get.
This should give an indication of why any of the tests are failing.
Well, here it is…
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 :13 Your script should print the correct message when a game is finished
Error: write EPIPE
at afterWriteDispatched (internal/stream_base_commons.js:156:25)
at writeGeneric (internal/stream_base_commons.js:147:3)
at Socket._writeGeneric (net.js:798:11)
at Socket._write (net.js:810:8)
at writeOrBuffer (internal/streams/writable.js:358:12)
at Socket.Writable.write (internal/streams/writable.js:303:10)
at Socket.<anonymous> (test/1.1.test.js:17:21)
at addChunk (internal/streams/readable.js:293:12)
at readableAddChunk (internal/streams/readable.js:263:11)
at Socket.Readable.push (internal/streams/readable.js:206:10)
at Pipe.onStreamRead (internal/stream_base_commons.js:188:23)
✘ SUBTASKS 1.1 :13 Your script should print the correct message when a game is finished
Error: done() called multiple times in test <SUBTASKS 1.1 :13 Your script should print the correct message when a game is finished> of file /home/codeally/project/.freeCodeCamp/test/1.1.test.js; in addition, done() received error: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert(re.test(scriptOutput))
✘ SUBTASKS 1.1 :16 You should submit your project while on the "main" branch of your repository with a clean working tree
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert(/On branch main\s/.test(commandOutput) && /working tree clean/.test(commandOutput))
at Context.<anonymous> (test/1.1.test.js:164:5)
It all points out to the message being incorrectly printed but I find no errors in that.
It’s all so vague…
No worries, that gives me some information to work with. I’ll have a proper look at your code and see if I can figure out what’s going wrong.
1 Like
Do you have an sql dump file which I can use please, to save me setting up the database from scratch myself?
My head is so fried that i ended up posting the wrong db.
Here it is!
--
-- 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: users; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.users (
user_id integer NOT NULL,
username character varying(22) NOT NULL,
games_played integer,
best_game integer
);
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: 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: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
--
-- Name: users_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.users_user_id_seq', 1, false);
--
-- Name: users users_username_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_username_key UNIQUE (username);
--
-- PostgreSQL database dump complete
--
Thanks, I’ll get back to you with my findings…
Ok, I made one change to your number_guess.sh
file and it passes all the tests now.
I haven’t examined the tests file closely but it seems that it really matters where this line goes:
GUESSES=$((GUESSES+1))
Remove it from inside the if
statement and uncomment the other line ( just before the done
).
It’s possible that your tests still won’t pass because of other issues unrelated to your code but that’s a failing of the testing environment and there are workarounds. If you’re still not passing, show me the test output again and I’ll give you a fix for it.
I am so mad right now…
That was my first approach for GUESSES=$((GUESSES+1))
and I changed it’s location based on other forum threads trying to find a solution, maybe at that time there where some other tests failing, although i never saw those two passed.
Anyways:
Thank you so much for your help and your time igorgetmeabrain I was already gaining courage to start the project from scratch