Testcase failed - periodic table challenge

Anyone can help me, to resolve this testcase. Output is getting what they expecting, but testcase is failing.

#!/bin/bash
echo Please provide an element as an argument.


PSQL="psql -X --username=freecodecamp --dbname=periodic_table --tuples-only -c"

SEND_MESSAGE(){
  INPUT=$1
  if [[ -z $INPUT ]]
  then
    return;
  fi
  # whenever we get from the terminal
  # number 
  # symbol
  # name
  if [[ $INPUT =~ ^[0-9]+$ ]]
  then
  # when we get atomic number as input
    ATOMIC_NUMBER=$($PSQL "SELECT atomic_number FROM elements WHERE atomic_number='$INPUT'")
    if [[ -z $ATOMIC_NUMBER ]]
    then
      echo "I could not find that element in the database."
    else
      SYMBOL=$($PSQL "SELECT symbol FROM elements WHERE atomic_number=$ATOMIC_NUMBER")
      NAME=$($PSQL "SELECT name FROM elements WHERE atomic_number=$ATOMIC_NUMBER")

      GET_MESSAGE $ATOMIC_NUMBER
    fi
  
  elif [[ $INPUT =~ ^[A-Z]$|^[A-Z][a-z]$ ]]
  then 
  # when we get symbol as input
    SYMBOL=$($PSQL "SELECT symbol FROM elements WHERE symbol = '$INPUT'")
    if [[ -z $SYMBOL ]]
    then 
      echo "I could not find that element in the database."
    else
      ATOMIC_NUMBER=$($PSQL "SELECT atomic_number FROM elements WHERE symbol LIKE '$INPUT';")
      NAME=$($PSQL "SELECT name FROM elements WHERE symbol='$INPUT';")

      
       GET_MESSAGE $ATOMIC_NUMBER
    fi
  
  elif [[ $INPUT =~ ^[A-Za-z][A-Za-z]+$ ]]
  then
  # when we get element as input
    NAME=$($PSQL "SELECT name FROM elements WHERE name='$INPUT';")
    if [[ -z $NAME ]]
    then
      echo "I could not find that element in the database."
    else
      ATOMIC_NUMBER=$($PSQL "SELECT atomic_number FROM elements WHERE name LIKE '$INPUT';")
      SYMBOL=$($PSQL "SELECT symbol FROM elements WHERE name = '$INPUT'")

      GET_MESSAGE $ATOMIC_NUMBER
    fi

  else
    echo I could not find  that element in the database.
  fi
}



GET_MESSAGE()
{
  GET_ELEMENT_INFO=$($PSQL "SELECT * FROM properties WHERE atomic_number = $1")
  echo "$GET_ELEMENT_INFO" | while IFS=" | " read ATOMIC_NUM ATOMIC_MASS MELTING_POINT BOILING_POINT TYPE_ID
  do
  TYPE=$($PSQL "SELECT type FROM types WHERE type_id = $TYPE_ID")
  SYMBOL=$(echo "$SYMBOL" | sed 's/ //')

  echo "The element with atomic number $ATOMIC_NUMBER is $NAME ($SYMBOL). It's a $TYPE, with a mass of $ATOMIC_MASS amu. $NAME has a melting point of $MELTING_POINT celsius and a boiling point of $BOILING_POINT celsius." | sed -E 's/ +/ /g'
  done
}


SEND_MESSAGE $1
Summary

This text will be hidden

I had the same problem on this one…The only way i found was to hardcode 10 ifs for every element in the database…if nothing else works for you try doing that.
That’s not the right way but it’s the last resort passing all the cases

what is hardcode 10?

Make an if for every element.
Eg… If [[ $1 == 1 || $1 == “Hydrogen” || $1 == “H”]]
then
echo …
fi
Do that for all the elements

Could you share dump of your db as well?

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 periodic_table;
--
-- Name: periodic_table; Type: DATABASE; Schema: -; Owner: postgres
--

CREATE DATABASE periodic_table WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';


ALTER DATABASE periodic_table OWNER TO postgres;

\connect periodic_table

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

CREATE TABLE public.elements (
    atomic_number integer NOT NULL,
    symbol character varying(2) NOT NULL,
    name character varying(40) NOT NULL
);


ALTER TABLE public.elements OWNER TO freecodecamp;

--
-- Name: properties; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.properties (
    atomic_number integer NOT NULL,
    atomic_mass real NOT NULL,
    melting_point_celsius numeric NOT NULL,
    boiling_point_celsius numeric NOT NULL,
    type_id integer NOT NULL
);


ALTER TABLE public.properties OWNER TO freecodecamp;

--
-- Name: types; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.types (
    type_id integer NOT NULL,
    type character varying NOT NULL
);


ALTER TABLE public.types OWNER TO freecodecamp;

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

INSERT INTO public.elements VALUES (1, 'H', 'Hydrogen');
INSERT INTO public.elements VALUES (2, 'He', 'Helium');
INSERT INTO public.elements VALUES (3, 'Li', 'Lithium');
INSERT INTO public.elements VALUES (4, 'Be', 'Beryllium');
INSERT INTO public.elements VALUES (5, 'B', 'Boron');
INSERT INTO public.elements VALUES (6, 'C', 'Carbon');
INSERT INTO public.elements VALUES (7, 'N', 'Nitrogen');
INSERT INTO public.elements VALUES (8, 'O', 'Oxygen');
INSERT INTO public.elements VALUES (9, 'F', 'Fluorine');
INSERT INTO public.elements VALUES (10, 'Ne', 'Neon');


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

INSERT INTO public.properties VALUES (3, 6.94, 180.54, 1342, 1);
INSERT INTO public.properties VALUES (4, 9.0122, 1287, 2470, 1);
INSERT INTO public.properties VALUES (1, 1.008, -259.1, -252.9, 2);
INSERT INTO public.properties VALUES (2, 4.0026, -272.2, -269, 2);
INSERT INTO public.properties VALUES (6, 12.011, 3550, 4027, 2);
INSERT INTO public.properties VALUES (7, 14.007, -210.1, -195.8, 2);
INSERT INTO public.properties VALUES (8, 15.999, -218, -183, 2);
INSERT INTO public.properties VALUES (5, 10.81, 2075, 4000, 3);
INSERT INTO public.properties VALUES (9, 18.998, -220, -188.1, 2);
INSERT INTO public.properties VALUES (10, 20.18, -248.6, -246.1, 2);


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

INSERT INTO public.types VALUES (1, 'metal');
INSERT INTO public.types VALUES (2, 'nonmetal');
INSERT INTO public.types VALUES (3, 'metalloid');


--
-- Name: elements elements_atomic_number_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.elements
    ADD CONSTRAINT elements_atomic_number_key UNIQUE (atomic_number);


--
-- Name: elements elements_name_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.elements
    ADD CONSTRAINT elements_name_key UNIQUE (name);


--
-- Name: elements elements_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.elements
    ADD CONSTRAINT elements_pkey PRIMARY KEY (atomic_number);


--
-- Name: elements elements_symbol_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.elements
    ADD CONSTRAINT elements_symbol_key UNIQUE (symbol);


--
-- Name: properties properties_atomic_number_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.properties
    ADD CONSTRAINT properties_atomic_number_key UNIQUE (atomic_number);


--
-- Name: properties properties_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.properties
    ADD CONSTRAINT properties_pkey PRIMARY KEY (atomic_number);


--
-- Name: types types_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.types
    ADD CONSTRAINT types_pkey PRIMARY KEY (type_id);


--
-- Name: properties properties_atomic_number_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.properties
    ADD CONSTRAINT properties_atomic_number_fkey FOREIGN KEY (atomic_number) REFERENCES public.elements(atomic_number);


--
-- Name: properties properties_type_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.properties
    ADD CONSTRAINT properties_type_id_fkey FOREIGN KEY (type_id) REFERENCES public.types(type_id);


--
-- PostgreSQL database dump complete
--


I don’t have the dump…I made a rebuild.sh to rebuild the database everytime i enter the course cause i had some problems if i exited the course…I’m not home right now,i’ll send it as soon as i get back

The Please provide an element as an argument. should be printed only if no argument is provided.

1 Like

use case statements

  case $INPUT in
    [0-9]|[0-9][0-9]) SEARCH_WITH_ATOMIC_NUMBER ;;
    [A-Z]|[A-Z][a-z]) SEARCH_WITH_SYMBOL ;;
    *) SEARCH_WITH_NAME ;;
  esac
1 Like