Tell us what’s happening:
I have tried several structures of code, if, while, until loops. At first I created functions to segregate the code but I ended up cleaning them up. On Terminal the program works perfectly but when it comes to the tests, most of them are failing. Could anyone help reviewing my code, please?
Error on tests: #7 until #13
FAILED TEST LOG
✘ SUBTASKS 1.1 :7 Your script should prompt for a username
RangeError [ERR_CHILD_PROCESS_STDIO_MAXBUFFER]: stdout maxBuffer length exceeded
at new NodeError (internal/errors.js:322:7)
at Socket.onChildStdout (child_process.js:443:14)
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 :8 Your script should print the correct welcome message for returning users
RangeError [ERR_CHILD_PROCESS_STDIO_MAXBUFFER]: stdout maxBuffer length exceeded
at new NodeError (internal/errors.js:322:7)
at Socket.onChildStdout (child_process.js:443:14)
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 :9 Your script should print the correct welcome message for new users
Error: Timeout of 7500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/codeally/project/.freeCodeCamp/test/1.1.test.js)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7)
✘ SUBTASKS 1.1 :10 Your script should print the correct initial message to prompt a user for a guess
RangeError [ERR_CHILD_PROCESS_STDIO_MAXBUFFER]: stdout maxBuffer length exceeded
at new NodeError (internal/errors.js:322:7)
at Socket.onChildStdout (child_process.js:443:14)
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 :11 Your script should print the correct messages if they do not guess the correct number
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 :12 Your script should print the correct message if users enter a guess other than a number
RangeError [ERR_CHILD_PROCESS_STDIO_MAXBUFFER]: stdout maxBuffer length exceeded
at new NodeError (internal/errors.js:322:7)
at Socket.onChildStdout (child_process.js:443:14)
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
RangeError [ERR_CHILD_PROCESS_STDIO_MAXBUFFER]: stdout maxBuffer length exceeded
at new NodeError (internal/errors.js:322:7)
at Socket.onChildStdout (child_process.js:443:14)
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 :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)
I’ve managed to adjust the values on the utils.js and the other file, but I’m still stuck with these errors in the test log:
FAILED TEST LOG
✘ SUBTASKS 1.1 :7 Your script should prompt for a username
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 :7 Your script should prompt for a username
Error: done() called multiple times in test <SUBTASKS 1.1 :7 Your script should prompt for a username> of file /home/codeally/project/.freeCodeCamp/test/1.1.test.js
at processTicksAndRejections (internal/process/task_queues.js:95:5)
✘ 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 processTicksAndRejections (internal/process/task_queues.js:95:5)
✘ SUBTASKS 1.1 :12 Your script should print the correct message if users enter a guess other than a number
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 :12 Your script should print the correct message if users enter a guess other than a number
Error: done() called multiple times in test <SUBTASKS 1.1 :12 Your script should print the correct message if users enter a guess other than a number> of file /home/codeally/project/.freeCodeCamp/test/1.1.test.js
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
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)
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
# prompt for username
echo "Enter your username:"
read USERNAME
# generate random game_name
# GAME_NAME=$(echo $RANDOM | md5sum | head -c 5; echo)
GAME_NAME="$USERNAME-$(date +'%Y-%m-%d-%H-%M-%S')"
# echo $GAME_NAME
# query username
VALIDATE_USER=$($PSQL "select user_id from users where name = '$USERNAME';")
# username validation
if [[ -z $VALIDATE_USER ]]; then
# first time user
echo "Welcome, $USERNAME! It looks like this is your first time here."
# insert new username
INSERT_INTO_USERS=$($PSQL "insert into users(name) values('$USERNAME');")
# get user_id based on username
GET_USER_ID=$($PSQL "select user_id from users where name = '$USERNAME';")
# insert game_name and user_id to register game
INSERT_GAME_NAME=$($PSQL "insert into games(name, user_id) values('$GAME_NAME', $GET_USER_ID);")
# query game_id
GET_GAME_ID=$($PSQL "select game_id from games where name = '$GAME_NAME';")
# update guess_count based on game_id
UPDATE_GUESS_COUNT_IN_GAMES=$($PSQL "update games set guess_count = 1000 where game_id = $GET_GAME_ID;")
else
# existing user
GAMES_PLAYED=$($PSQL "select count(game_id) from games where user_id = $VALIDATE_USER;")
BEST_GAME=$($PSQL "select min(guess_count) from games where user_id = $VALIDATE_USER;")
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
# get user_id based on username
GET_USER_ID=$($PSQL "select user_id from users where name = '$USERNAME';")
# insert game_name and user_id to register game
INSERT_GAME_NAME=$($PSQL "insert into games(name, user_id) values('$GAME_NAME', $GET_USER_ID);")
fi
# generate random number up to 1000
CORRECT_RANDOM_GUESS=$(( $RANDOM % 1000 + 1 ))
# echo "CORRECT_RANDOM_GUESS: $CORRECT_RANDOM_GUESS"
GUESS_COUNT=0
echo "Guess the secret number between 1 and 1000:"
# loop until number_guessed is equal to correct_random_guess
until [[ $NUMBER_GUESSED -eq $CORRECT_RANDOM_GUESS ]]; do
read NUMBER_GUESSED
(( GUESS_COUNT++ ))
# integer validation
if [[ ! $NUMBER_GUESSED =~ ^[0-9]+$ ]]; then
echo "That is not an integer, guess again:"
else
# if [[ ! ( $NUMBER_GUESSED -gt 0 && $NUMBER_GUESSED -le 1000 ) ]]; then
# echo "It's lower than that, guess again:"
# else
if [[ $NUMBER_GUESSED -gt $CORRECT_RANDOM_GUESS ]]; then
echo "It's lower than that, guess again:"
elif [[ $NUMBER_GUESSED -lt $CORRECT_RANDOM_GUESS ]]; then
echo "It's higher than that, guess again:"
fi
# fi
fi
done
# when guessed, increment guess_count
(( GUESS_COUNT++ ))
# query game_id
GET_GAME_ID=$($PSQL "select game_id from games where name = '$GAME_NAME';")
# update guess_count based on game_id
UPDATE_GUESS_COUNT_IN_GAMES=$($PSQL "update games set guess_count = $GUESS_COUNT where game_id = $GET_GAME_ID;")
# print result
echo "You guessed it in $GUESS_COUNT tries. The secret number was $CORRECT_RANDOM_GUESS. Nice job!"
# alter table users
# alter column name type varchar(23)
# using name::varchar(23);
# NEW COMMENT
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; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.games (
game_id integer NOT NULL,
name character varying NOT NULL,
guess_count integer,
user_id 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(23) 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
--
--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
--
-- Name: games_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.games_game_id_seq', 1155, true);
--
-- Name: users_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.users_user_id_seq', 473, 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
--
I’m having real issue trying to get your code to pass, despite it seemingly working exactly as intended.
I’m getting strange errors which I don’t understand at all:
FAILED TEST LOG
An error occurred running ./number_guessing_game/number_guess.sh
ERROR: syntax error at or near "1444"
LINE 2: 1444
^
✘ 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 processTicksAndRejections (internal/process/task_queues.js:95:5)
An error occurred running ./number_guessing_game/number_guess.sh
ERROR: syntax error at or near "1449"
LINE 2: 1449
^
✘ 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)
✘ 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)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
Does anyone know what those ‘syntax errors’ are about?
I had real problems getting this to pass. I don’t know if it’s because your script is slow to test because it adds a new record to the games table for every game? (The testing adds a bunch).
Your code works fine but for one tiny detail. You added an extra GUESS_COUNT increment:
# when guessed, increment guess_count
(( GUESS_COUNT++ ))
You don’t actually need this. If you run your code and check, you’ll find that it miscounts by one with that in. So I commented it out.
Anyhow, your code did eventually pass without any other changes, except those I made to the test files. I actually have no idea if it was simply blind luck that it eventually passed, from spamming the Run button, but at least you know it definitely can pass!
I ended up with crazy timeout values but it worked eventually.
I changed the following (with nano):