<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gianni Castellano</title>
    <description>The latest articles on DEV Community by Gianni Castellano (@gianni_cast).</description>
    <link>https://dev.to/gianni_cast</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1550743%2Fba3d393a-4235-4176-8f1a-c7cba6349e45.png</url>
      <title>DEV Community: Gianni Castellano</title>
      <link>https://dev.to/gianni_cast</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gianni_cast"/>
    <language>en</language>
    <item>
      <title>Redux the beginners guide!</title>
      <dc:creator>Gianni Castellano</dc:creator>
      <pubDate>Thu, 22 Aug 2024 21:46:28 +0000</pubDate>
      <link>https://dev.to/gianni_cast/redux-the-beginners-guide-dn9</link>
      <guid>https://dev.to/gianni_cast/redux-the-beginners-guide-dn9</guid>
      <description>&lt;p&gt;Hi everyone! I am here to give a small guide explaining on how to use Redux within to manage state within your react application. There are five key concepts to understand in order to incorporate redux in your react application:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Store&lt;/li&gt;
&lt;li&gt;Actions&lt;/li&gt;
&lt;li&gt;Reducers&lt;/li&gt;
&lt;li&gt;Dispatch &lt;/li&gt;
&lt;li&gt;Selectors&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Store&lt;/strong&gt;&lt;br&gt;
The purpose of store is to have a central hub where all the state within the app is managed. After creating a Store.jsx file, you need to first import configureStore at the top of the file. Then you need to import the files within your app that are modifying states from the slice files. Lastly, you need to combine the reducers from all the slice files into a single root reducer. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actions&lt;/strong&gt;&lt;br&gt;
Within your slice files it is necessary to use actions in order to send data from within your app to the Store file. They are generic Javascript objects that require a type property in order to perform the needed action. In the context of my latest project I used them in order to fetch, delete, create, and post data to my backend server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reducers&lt;/strong&gt;&lt;br&gt;
Reducers work hand in hand with actions. They are functions that take the current state and an action as arguments and return a new state without mutating the original data. They state how the app's state will change due to an action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dispatch&lt;/strong&gt;&lt;br&gt;
Just like reducers, dispatch, is a function that works with reducers to send actions to the store file. The dispatch function is used to trigger the reducer which in turn triggers action in order to update the state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Selectors&lt;/strong&gt;&lt;br&gt;
Selectors are used in order to grab the data from the state within files in order to update the data accordingly.&lt;/p&gt;

&lt;p&gt;Here is code snippets from my latest project in order to give a visual demonstration on the above information  &lt;/p&gt;

&lt;p&gt;Store.jsx:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from "@reduxjs/toolkit"
import clanReducer from './ClanSlice'
import eventReducer from './EventSlice'
import clanFormReducer from './ClanFormSlice'
import eventFormReducer from './EventFormSlice'

const store = configureStore({
    reducer: {
        clan: clanReducer,
        event: eventReducer,
        clanForm: clanFormReducer,
        eventForm: eventFormReducer,
    },
})

export default store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ClanSlice.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import axios from 'axios'

export const fetchClan = createAsyncThunk('clans/fetchClan', async (id) =&amp;gt; { 
  const response = await axios.get(`http://127.0.0.1:5555/clans/${id}`)
  return response.data
})

export const deleteClan = createAsyncThunk('clans/deleteClan', async (id) =&amp;gt; {
    await axios.delete(`http://127.0.0.1:5555/clans/${id}`)
    return id
})

export const fetchClans = createAsyncThunk('clans/fetchClans', async () =&amp;gt; { 
    const response = await axios.get('http://127.0.0.1:5555/clans')
    return response.data
  })

export const createClan = createAsyncThunk('clans/createClan', async (clanData, { dispatch }) =&amp;gt; {
      const response = await axios.post('http://127.0.0.1:5555/clans', clanData)
      dispatch(fetchClans())
      return response.data
    })

const clanSlice = createSlice({
    name: 'clan',
    initialState: {
        clans: [],
        clan: [],
        status: 'idle',
        error: null,
    },
    reducers: {
        resetClan: (state) =&amp;gt; {
            state.clan = []
            state.status = 'idle'
            state.error = null
        },
        resetStatus: (state) =&amp;gt; {
            state.status = 'loading'
        }
    },
    extraReducers: (builder) =&amp;gt; {
        builder
        .addCase(fetchClan.pending, (state) =&amp;gt; {
            state.status = 'loading'
        })
        .addCase(fetchClan.fulfilled, (state, action) =&amp;gt; {
            state.status = 'succeeded'
            state.clan = action.payload
        })
        .addCase(fetchClan.rejected, (state, action) =&amp;gt; {
            state.status = 'failed'
            state.error = action.error.message
        })
        .addCase(fetchClans.pending, (state) =&amp;gt; {
            state.status = 'loading'
        })
        .addCase(fetchClans.fulfilled, (state, action) =&amp;gt; {
            state.status = 'succeeded'
            state.clans = action.payload
        })
        .addCase(fetchClans.rejected, (state, action) =&amp;gt; {
            state.status = 'failed'
            state.error = action.error.message
        })
        .addCase(deleteClan.fulfilled, (state, action) =&amp;gt; {
            state.clans = state.clans.filter(clan =&amp;gt; clan.id !== action.payload)
        })
        .addCase(createClan.fulfilled, (state, action) =&amp;gt; {
            state.status = 'succeeded'
            state.clans.push(action.payload)
        })
    },
    })

export const { resetClan, resetStatus } = clanSlice.actions

export default clanSlice.reducer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ClanSlice.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect } from "react"
import { useDispatch, useSelector } from "react-redux"
import { fetchClans, resetStatus } from "./ClanSlice"
import { Link } from "react-router-dom"
import './Clans.css'

function Clans() {
    const dispatch = useDispatch()
    const clans = useSelector((state) =&amp;gt; state.clan.clans)
    const status = useSelector((state) =&amp;gt; state.clan.status)
    const error = useSelector((state) =&amp;gt; state.clan.error)

    useEffect(() =&amp;gt; {
        dispatch(fetchClans())
    }, [dispatch])

    useEffect(() =&amp;gt; {
        return () =&amp;gt; {
            dispatch(resetStatus())
        }
    }, [])

    let content

    if (status === 'loading') {
        content = &amp;lt;p&amp;gt;Loading...&amp;lt;/p&amp;gt;
    } else if (status === 'succeeded') {
        content = (
            &amp;lt;div className="clans-wrapper"&amp;gt;
                {clans.map((clan) =&amp;gt; (
                    &amp;lt;div key={clan.id} className="clan-box"&amp;gt;
                        &amp;lt;h3&amp;gt;
                            &amp;lt;Link to={`/clans/${clan.id}`}&amp;gt;{clan.name}&amp;lt;/Link&amp;gt;
                        &amp;lt;/h3&amp;gt;
                        &amp;lt;p&amp;gt;{clan.description}&amp;lt;/p&amp;gt;
                        &amp;lt;h4&amp;gt;Members:&amp;lt;/h4&amp;gt;
                        &amp;lt;div className="members-container"&amp;gt;
                            &amp;lt;ul&amp;gt;
                                {clan.members.map((member) =&amp;gt; (
                                    &amp;lt;li key={member.id}&amp;gt;
                                        &amp;lt;span className="username-text"&amp;gt;Username:&amp;lt;/span&amp;gt;{member.username} &amp;lt;span className="role-text"&amp;gt;Role:&amp;lt;/span&amp;gt; {member.role}
                                        &amp;lt;ul&amp;gt;
                                            {member.participations.map((participation) =&amp;gt; (
                                                &amp;lt;li key={participation.id}&amp;gt;
                                                    &amp;lt;span className="event-text"&amp;gt;Event:&amp;lt;/span&amp;gt; {participation.event.event}
                                                    &amp;lt;br /&amp;gt;
                                                    &amp;lt;span className="status-text"&amp;gt;Status:&amp;lt;/span&amp;gt; {participation.participation_status}
                                                &amp;lt;/li&amp;gt;
                                            ))}
                                        &amp;lt;/ul&amp;gt;
                                    &amp;lt;/li&amp;gt;
                                ))}
                            &amp;lt;/ul&amp;gt;
                        &amp;lt;/div&amp;gt;
                        &amp;lt;h4&amp;gt;Events:&amp;lt;/h4&amp;gt;
                        &amp;lt;div className="events-container"&amp;gt;
                            &amp;lt;ul&amp;gt;
                                {clan.events.map((event) =&amp;gt; (
                                    &amp;lt;li key={event.id}&amp;gt;
                                        &amp;lt;Link to={`/events/${event.id}`}&amp;gt;{event.event}&amp;lt;/Link&amp;gt; - {event.date} - {event.location} - {event.details}
                                    &amp;lt;/li&amp;gt;
                                ))}
                            &amp;lt;/ul&amp;gt;
                        &amp;lt;/div&amp;gt;
                    &amp;lt;/div&amp;gt;
                ))}
            &amp;lt;/div&amp;gt;
        )
    } else if (status === 'failed') {
        content = &amp;lt;p&amp;gt;{error}&amp;lt;/p&amp;gt;
    }

    return (
        &amp;lt;div className="container"&amp;gt;
            &amp;lt;h2&amp;gt;Welcome to the Clans Page&amp;lt;/h2&amp;gt;
            {content}
        &amp;lt;/div&amp;gt;
    )
}

export default Clans
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>redux</category>
      <category>react</category>
    </item>
    <item>
      <title>Introduction to Routing in Flask</title>
      <dc:creator>Gianni Castellano</dc:creator>
      <pubDate>Fri, 02 Aug 2024 07:47:15 +0000</pubDate>
      <link>https://dev.to/gianni_cast/introduction-to-routing-in-flask-19bj</link>
      <guid>https://dev.to/gianni_cast/introduction-to-routing-in-flask-19bj</guid>
      <description>&lt;p&gt;Hello readers, I am going to give a brief introduction on an important concept in Flask, routing. It is used to direct incoming HTTP requests to the relevant parts of your application. Flask is a flexible and easy to use web framework designed to support the development of web applications. I will be using examples from my most recent Oldschool RuneScape Quest tracker application. There are four different methods used when creating your routes: GET, POST, PATCH/PUT, DELETE. The GET method will retrieve/view data from your API. POST will allow you to submit data via forms. PUT/PATCH allows you to update existing data. Lastly, DELETE, allows you to delete data.&lt;/p&gt;

&lt;p&gt;This is done using the decorator @app.route followed by "/(specific route)", methods=["method"].&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/players', methods=["GET"])
def players_route():
    players = []
    for player in Player.query.all():
        player_dict = {
            "level": player.level,
            "id": player.id,
            "name": player.name,
        }
        players.append(player_dict)

    response = make_response(
        players,
        200,
        {"Content-Type": "application/json"}
    )

    return response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see at the top of this code we have the decorator followed by the route and the method we want to use on it. For this example we want to GET/Read/View the data from my players table in my API. &lt;/p&gt;

&lt;p&gt;You might now be wondering what all the code under the decorator does. Well lets break it down! Our decorator tells flask to call the function "def players_route()" whenever a GET request is made the /players URL.&lt;br&gt;&lt;br&gt;
"players = []" is an empty list to store our player data. Next, we use a for loop to fetch all the players by using Player.query.all(). We then create a dictionary inside to loop in order to store our data in a JSON format. Lastly, we append the data using make_response to convert the player list to a JSON response. The 200 lets us know if the request was successful or not.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this short introduction on Flask routing! &lt;/p&gt;

</description>
      <category>flask</category>
      <category>sqlalchemy</category>
    </item>
    <item>
      <title>Classes in Python (Introduction)</title>
      <dc:creator>Gianni Castellano</dc:creator>
      <pubDate>Thu, 11 Jul 2024 21:41:40 +0000</pubDate>
      <link>https://dev.to/gianni_cast/classes-in-python-introduction-13cc</link>
      <guid>https://dev.to/gianni_cast/classes-in-python-introduction-13cc</guid>
      <description>&lt;p&gt;In Python, classes are the foundation of object-oriented programming. In simple terms, they are essentially a template for creating objects with similar attributes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating Classes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Class definition syntax is extremely straightforward. All you need is the keyword: class followed by the ClassName: (the class name is always in UpperCamelCase). I have provided an example below:&lt;br&gt;
&lt;code&gt;class Shop:&lt;/code&gt;&lt;br&gt;
Well done you have successfully created a class! Now we will take a deeper dive into how you can use them. I will be using a class to create and store different shops throughout this blog.   &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Classes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first step after creating your class is to use a constructer method know as the &lt;strong&gt;init&lt;/strong&gt; method to initialize instance attributes that will be used when instantiating objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Shop:
    def __init__(self, name, location, owner):
        self.name = name
        self.location = location
        self.owner = owner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now whenever we create or instantiate a new store/shop object within this class it will share these attributes we initialized! Now lets create some shops:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Shop:
    def __init__(self, name, location, owner):
        self.name = name
        self.location = location
        self.owner = owner

   #method for displaying our stores
   def display_store_info(self)
       return f"Shop: {self.name}, Location: {self.location}, Owner: {self.owner}"

#creating shop instances
first_shop = Shop("FoodMart", "Main Street", "John Smith")
second_shop = Shop("ClothingStore", "Billybob Avenue", "Billy Bob")


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in our python shell if we type print(first_shop.display_store_info())  we will see this display:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Shop: FoodMart, Location: Main Street, Owner: John Smith&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We could also do the same for the second_shop! We created a method or function in our class called display_store_info that allowed us to inject the attributes defined in our init. Now we could make limitless shop objects that include the name, location, and owner as a reusable template. &lt;/p&gt;

&lt;p&gt;This is just the beginning when it comes to classes. The possibilities and reusability is incredible when it comes to using classes in Python. I would love to go into more detail in a future blog post but this is just a small little intro.  &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>classes</category>
    </item>
    <item>
      <title>A Guide to Better Understand Props in React.js!</title>
      <dc:creator>Gianni Castellano</dc:creator>
      <pubDate>Thu, 20 Jun 2024 21:21:31 +0000</pubDate>
      <link>https://dev.to/gianni_cast/a-guide-to-better-understand-props-in-reactjs-40of</link>
      <guid>https://dev.to/gianni_cast/a-guide-to-better-understand-props-in-reactjs-40of</guid>
      <description>&lt;p&gt;Props are essential in any React.js application as they simplify the flow of data! They allow you to pass data from parent to child, thus making your code more dynamic as well as reusable.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are they?
&lt;/h2&gt;

&lt;p&gt;As mentioned briefly in the intro, props, is how data is passed from one component to another. It is crucial to understand that they are uni-directional, meaning they could only be passed down from a parent component to a child component. They *&lt;em&gt;cannot *&lt;/em&gt; be passed from sibling to sibling. &lt;/p&gt;

&lt;p&gt;Here is a basic example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ParentComponent() {
  const data = "Hello from Parent!";
  return &amp;lt;ChildComponent greeting={data} /&amp;gt;;
}

function ChildComponent(props) {
  return &amp;lt;p&amp;gt;{props.greeting}&amp;lt;/p&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These functions are two different components in an React app. The parent component has data that the other component cant access without it being passed down. the props parameter allows the child component have access to that data. But what if you had another component that wanted to use that data? Simple! You pass it down to that component as well!. &lt;/p&gt;

&lt;p&gt;You might be wondering, "What else can I do with props?" and the answer to that is a lot. You can even pass down functions as props which allows child components to communicate with parent components by invoking the passed down functions. &lt;/p&gt;

&lt;p&gt;Here is an example from my personal project I did this week:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const [characters, setCharacters] = useState([]);

  useEffect(() =&amp;gt; {
    fetch(API)
      .then(resp =&amp;gt; resp.json())
      .then(data =&amp;gt; setCharacters(data))   
  }, [])

  function addCharacter(character) {
    fetch(API, {
      method: 'POST',
      headers: {
          'Content-Type': 'application/json', 
      },
      body: JSON.stringify(character)
  })
  .then(response =&amp;gt; response.json())
  .then(json =&amp;gt; setCharacters([...characters, json]))}

  return (
     &amp;lt;Router&amp;gt;
      &amp;lt;div className="container"&amp;gt;
        &amp;lt;Header /&amp;gt;
        &amp;lt;Routes&amp;gt;
          &amp;lt;Route path="/" element={&amp;lt;CharacterPage characters={characters} /&amp;gt;} /&amp;gt;
          &amp;lt;Route path="/create" element={&amp;lt;CreateCharacter addCharacter={addCharacter} /&amp;gt;} /&amp;gt;
          &amp;lt;Route path="/search" element={&amp;lt;Search /&amp;gt;} /&amp;gt;
          &amp;lt;Route path="/search/:searchTerm" element={&amp;lt;Results /&amp;gt;} /&amp;gt;
        &amp;lt;/Routes&amp;gt;
      &amp;lt;/div&amp;gt;
     &amp;lt;/Router&amp;gt;
  )
}

function CreateCharacter ({ addCharacter }) {
    const [characterInfo, setCharacterInfo] = useState(initialCharcterInfo)

    function handleSubmit(e) {
        e.preventDefault()

        addCharacter(characterInfo)
        setCharacterInfo(initialCharcterInfo)}

    function handleChange(e) {
        const key = e.target.name
        const newCharacterInfo = {
            ...characterInfo,
            [key]: e.target.value
        }
        setCharacterInfo(newCharacterInfo)}

    return (
        &amp;lt;div className="form-container"&amp;gt;
            &amp;lt;form onSubmit={handleSubmit}&amp;gt;
                &amp;lt;input type="text" name="fullName" placeholder="Enter Full Name" value={characterInfo.fullName} onChange={handleChange}/&amp;gt;
                &amp;lt;input type="text" name="title" placeholder="Enter Title" value={characterInfo.title} onChange={handleChange}/&amp;gt;
                &amp;lt;select name="family" value={characterInfo.family} onChange={handleChange}&amp;gt;
                    &amp;lt;option value="House Stark"&amp;gt;House Stark&amp;lt;/option&amp;gt;
                    &amp;lt;option value="House Lannister"&amp;gt;House Lannister&amp;lt;/option&amp;gt;
                    &amp;lt;option value="House Baratheon"&amp;gt;House Baratheon&amp;lt;/option&amp;gt;
                    &amp;lt;option value="House Greyjoy"&amp;gt;House Greyjoy&amp;lt;/option&amp;gt;
                    &amp;lt;option value="House Tyrell"&amp;gt;House Tyrell&amp;lt;/option&amp;gt;
                    &amp;lt;option value="House Bolton"&amp;gt;House Bolton&amp;lt;/option&amp;gt;
                    &amp;lt;option value="Free Folk"&amp;gt;Free Folk&amp;lt;/option&amp;gt;
                    &amp;lt;option value="House Targaryen"&amp;gt;House Targaryen&amp;lt;/option&amp;gt;
                    &amp;lt;option value="House Mormont"&amp;gt;House Mormont&amp;lt;/option&amp;gt;
                    &amp;lt;option value="misc"&amp;gt;misc&amp;lt;/option&amp;gt;
                &amp;lt;/select&amp;gt;
                &amp;lt;input type="text" name="imageUrl" placeholder="Enter Image URL" value={characterInfo.imageUrl} onChange={handleChange}/&amp;gt;
                &amp;lt;input type="text" name="bio" placeholder="Enter Bio" value={characterInfo.bio} onChange={handleChange}/&amp;gt;
                &amp;lt;button type="submit" className="character-submit"&amp;gt;Create Character&amp;lt;/button&amp;gt;
            &amp;lt;/form&amp;gt;
        &amp;lt;/div&amp;gt;
    )}
export default CreateCharacter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see the function addCharacter that was used in order to post data to my db.json file was passed down from my App.jsx (parent) component to my CreateCharacter.jsx (child) via some {} curly brackets. And I wanted to use this again elsewhere in another child component I can! That's the beauty of Props in React.js.&lt;/p&gt;

&lt;p&gt;Hoped you enjoyed my beginner guide for Props in React.js!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Keydown Event Listener and Focus</title>
      <dc:creator>Gianni Castellano</dc:creator>
      <pubDate>Thu, 30 May 2024 20:24:17 +0000</pubDate>
      <link>https://dev.to/gianni_cast/keydown-event-listener-and-focus-bg3</link>
      <guid>https://dev.to/gianni_cast/keydown-event-listener-and-focus-bg3</guid>
      <description>&lt;p&gt;Hello readers! Welcome to my first blog where I will be discussing the issues and resolution I had with the "Keydown" event listener in JavaScript. When trying to implement my last event listener I struggled for an hour trying to understand why it was not working although the logic was all there. Here is the original HTML, JavaScript, and CSS for my code:&lt;br&gt;
&lt;strong&gt;HTML&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src="https://pbs.twimg.com/profile_images/1364220282790109188/uT0fQV56_400x400.jpg" alt="blue party hat" id="easterEgg" tabindex="0"&amp;gt;
    &amp;lt;div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;JavaScript&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const easterEggImage = document.getElementById('easterEgg') 

easterEggImage.addEventListener("keydown", handleKeyDown);

function handleKeyDown(event) {
  if (event.key.toLowerCase() === "h") {
    easterEggImage.style.display = "block";
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;CSS&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#easterEgg {
    display: none;
    position: fixed;
    left: 0;
    bottom: 0px;
    margin-left: 5%;
    max-width: 90%;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The idea of this was to make an image appear whenever the user pressed the "h" key. Unfortunately it was not working and I could not understand why. After doing research I became more familiar with the concepts of "Event Delegation" and "direct event attachment". When I was using the descendant element &lt;code&gt;easterEggImage&lt;/code&gt; I was only giving instruction for to apply the event to that element only. This is not what I intended so in order to fix this I attached the &lt;code&gt;addEventListener&lt;/code&gt; to the document instead. Here is the working code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener("keydown", handleKeyDown);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now my keydown event will trigger anywhere in the document! So after my hour long struggle I was able to get my image to appear with this function.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
