<?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: Drew Womble</title>
    <description>The latest articles on DEV Community by Drew Womble (@drew_womble).</description>
    <link>https://dev.to/drew_womble</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%2F1073093%2F9c2d79c7-f1ac-48f9-b617-a5a516799d90.png</url>
      <title>DEV Community: Drew Womble</title>
      <link>https://dev.to/drew_womble</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/drew_womble"/>
    <language>en</language>
    <item>
      <title>Using Stripe to Implement Payment handling on a website</title>
      <dc:creator>Drew Womble</dc:creator>
      <pubDate>Fri, 21 Jul 2023 17:16:03 +0000</pubDate>
      <link>https://dev.to/drew_womble/using-stripe-to-implement-payment-handling-on-a-website-13m3</link>
      <guid>https://dev.to/drew_womble/using-stripe-to-implement-payment-handling-on-a-website-13m3</guid>
      <description>&lt;p&gt;Using Stripe to set up payment handling yourself can seem like a daunting task. While Stripes documentation is incredibly useful, it can be confusing at times. In this demonstration I'll show you how I set it up in one of my projects. From getting the payments to go through to stripe, and receiving the information from the payment to update your database.&lt;/p&gt;

&lt;p&gt;First you'll want to go to &lt;a href="https://stripe.com/" rel="noopener noreferrer"&gt;Stripe's&lt;/a&gt; website and setup an account. Then install Stripe with &lt;code&gt;pip install stripe&lt;/code&gt;. In this example I'm using Flask for the backend and React for the frontend. The website is an e-commerce page for buying and selling skateboards. Now create some models for the product to sell and the receipts then run &lt;code&gt;flask db init&lt;/code&gt;, &lt;code&gt;flask db migrate&lt;/code&gt;, and &lt;code&gt;flask db upgrade&lt;/code&gt; to create the database. My models look like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ygwc8gozjnaepnisc5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ygwc8gozjnaepnisc5f.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmq0hkv4n7qttyxoly6j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmq0hkv4n7qttyxoly6j.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Now that we have a database go back to your Stripe dashboard and click on developers. A tab for API keys will show click that and get your test key.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mv159tkh9ytixe1lj8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mv159tkh9ytixe1lj8p.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Since this project was being tracked by github I have my test key stored as an environment variable in a separate .env file. You can either do the same or replace &lt;code&gt;environ.get('STRIPE_API_TEST_KEY')&lt;/code&gt; with your key in quotes. Now there isn't a lot of stuff we have to do on the front end so figure out what button you want to use to redirect the user to the stripe checkout page. Then create a post request to whatever route you're using to create the checkout session like so: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvapbepuzkj3n5ssoxm05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvapbepuzkj3n5ssoxm05.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Mine is the one on line 23. Now that you have a way to redirect the user on the frontend, you can either create the products manually in the Stripe dashboard or use the following function to create them on Stripe and in your database. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg8j9fzcnqe8nelzcq8vr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg8j9fzcnqe8nelzcq8vr.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
You can disregard the get method (lines 147-149). What's happening here is whenever the user creates a new deck to sell lines 160-170 is actually creating the products in your Stripe dashboard as well as adding the product id's and price id's to our database because we will need them later.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6fev0es1phsbsxhe8ir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6fev0es1phsbsxhe8ir.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Here we defined a route for the session, and got the product from our database. Now that we have said product we can use the price id stripe provides to determined the amount the user will be charged. We also specified the &lt;code&gt;client_reference_id&lt;/code&gt; so when we get the information back we'll know which user made the purchase. You can add a lot here from custom fields to shipping addresses, but for the purposes of this example we won't be needing all the extra fields. Now you'll need to setup a webhook to retrieve the data from stripe after the transaction. I used &lt;a href="https://ngrok.com/" rel="noopener noreferrer"&gt;Ngrok&lt;/a&gt;, you can use any service you like. Stripe even offers its own CLI you can use, but I prefer ngrok. If you go with ngrok you'll need to create an account and download ngrok. Once you've done that unzip the file. Sometimes you'll run into issues if it isn't set as a windows environment variable. To set it as such copy the path for the ngrok application. Then in the windows search bar search edit environment variables. You should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsjujte1rrs31m6kcyi5c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsjujte1rrs31m6kcyi5c.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
From here click environment variables and then under system variables click on Path. Then click edit. Now click on New and paste in the path do the ngrok application. You can now close the window and open up your terminal. If you actually set the path to a variable enter that variable name otherwise paste the path back in and type http 5000. Or whatever port you plan on running your server on. You should have something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F68h9cq2m1l9i0colreob.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F68h9cq2m1l9i0colreob.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Copy the link after Forwarding and head back over to your Stripe dashboard. From the developers tab you should find a tab for webhooks. Once there click on add endpoint. Then paste the link from the terminal into endpoint URL and add &lt;code&gt;/webhook&lt;/code&gt; to the end. Now click on select events then checkout. Now toggle the event called &lt;code&gt;checkout.session.completed&lt;/code&gt;, and select add endpoint. Now click on the endpoint you just created; under signing secret click reveal and copy the key.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8w2vly8daht3dd02ovj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8w2vly8daht3dd02ovj1.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Same as before my key is stored as an environment variable, but you can just paste it in here as a string. Now we'll write the method to use the webhook. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm5sl5vypyr5f9jk4k7ko.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm5sl5vypyr5f9jk4k7ko.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
The only really important thing here is lines 67-75. This is where we retrieve the event object and add the relevant information to the receipts table. You can get a lot of information back from this depending on what you specify when you initially create the checkout session. Now you can test it out and see how it all works! Stripe provides you with a test card number you can use for the transactions so you don't unload your bank account trying to test an app. Feel free to leave any questions in the comments I'll be sure to get back to you, and checkout the repo used in the examples &lt;a href="https://github.com/drwomble/capstone-project" rel="noopener noreferrer"&gt;here&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>react</category>
      <category>python</category>
      <category>stripe</category>
    </item>
    <item>
      <title>A Comprehensive Guide to Cookies and Sessions in Web Development with Python</title>
      <dc:creator>Drew Womble</dc:creator>
      <pubDate>Fri, 30 Jun 2023 03:46:24 +0000</pubDate>
      <link>https://dev.to/drew_womble/a-comprehensive-guide-to-cookies-and-sessions-in-web-development-with-python-1oo0</link>
      <guid>https://dev.to/drew_womble/a-comprehensive-guide-to-cookies-and-sessions-in-web-development-with-python-1oo0</guid>
      <description>&lt;p&gt;Cookies and sessions are powerful tools that enable websites to store and manage user data. Understanding how cookies and sessions are used is essential to building dynamic and personalized web applications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvkpxrongogclebj6okxz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvkpxrongogclebj6okxz.jpg" alt="Picture of chocolate chip cookies"&gt;&lt;/a&gt;&lt;br&gt;
Not those kind of cookies!!!&lt;br&gt;
ok&lt;br&gt;
....now that your eyes have rolled out of the back of your head.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;COOKIES:&lt;/strong&gt;&lt;br&gt;
Cookies are small pieces of data stored on a user's computer by the websites that they visit. These data snippets are then passed from the users browser to the web server whenever a request is made. This allows websites to remember users preferences, track their activity, and store session information.&lt;/p&gt;

&lt;p&gt;Cookies are primarily used for personalization, session management, tracking, and analytics. Personalization would be how cookies are used to remember a users preferences and deliver an experience more tailored to the user. Thing's like how a website will remember your username when you go to login or a preferred language. Session management involves maintaining a user's session information. This allows for user authentication throughout a user's visit to a website. Tracking and analytics can be used to collect data on a user's behavior. Resulting in websites optimizing their content for each user, and you seeing ads for the same things on every site you visit.&lt;/p&gt;

&lt;p&gt;In Python, the &lt;code&gt;http.cookies&lt;/code&gt; module provides functionalities for working with cookies. Setting a cookie with Python isn't difficult and can be achieved with a few lines of code when using the &lt;code&gt;http.cookies&lt;/code&gt; module:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fncrcritp56go46x7l3pi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fncrcritp56go46x7l3pi.png" alt="code snippet"&gt;&lt;/a&gt;&lt;br&gt;
Above we created a &lt;code&gt;SimpleCookie&lt;/code&gt; object to represent the cookie. We then set the value of the &lt;code&gt;username&lt;/code&gt; cookie to &lt;code&gt;'generic username'&lt;/code&gt;. We also specified certain attributes such as the expiration time and the path where the cookie is accessible. Finally, we output the cookie in the response headers.&lt;/p&gt;

&lt;p&gt;To retrieve cookies sent by the client we can access the &lt;code&gt;'HTTP_COOKIE'&lt;/code&gt; environment variable like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkcefalz5awe9bykjgagw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkcefalz5awe9bykjgagw.png" alt="Code snippet"&gt;&lt;/a&gt;&lt;br&gt;
Above we retrieved the raw cookie string from the &lt;code&gt;'HTTP_COOKIE'&lt;/code&gt; environment variable using &lt;code&gt;os.environ.get()&lt;/code&gt;. Then we parse the cookie string into a dictionary using the &lt;code&gt;SimpleCookie&lt;/code&gt; class. Lastly, we can access the value of a specific cookie, such as &lt;code&gt;'username'&lt;/code&gt; if it exists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SESSIONS:&lt;/strong&gt;&lt;br&gt;
Sessions provide us with the ability to store user-specific data on the server and maintain user state across multiple requests. Unlike cookies, which are stored on the client-side, session data is stored on the server and is associated with a unique session identifier.&lt;/p&gt;

&lt;p&gt;Sessions offer several advantages in web development. Such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Secure data storage: Session data is stored on the server, ensuring the security and confidentiality of user information.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;State Management: Sessions enable websites to maintain user state across multiple requests, such as remembering a user's shopping cart items or progress in a multi-step form.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Customization and Personalization: Session data allows websites to deliver customized experiences based on user preferences, setting, and history.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Python frameworks, such as Flask, provide built-in session management functionality. For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8xo0jiemqq8nyf2c7vfc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8xo0jiemqq8nyf2c7vfc.png" alt="Code snippet"&gt;&lt;/a&gt;&lt;br&gt;
Here we initialized a Flask application and set a secret key, &lt;code&gt;'not_so_secret_key'&lt;/code&gt;, which is required for session management. We then defined three routes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The index route checks if the &lt;code&gt;'username'&lt;/code&gt; key exists in the session. If it does, it retrieves the value and displays it, showing that the user is logged in. Contrarily, it displays a message indicating the user in not logged in.&lt;/li&gt;
&lt;li&gt;The login route sets the &lt;code&gt;'username'&lt;/code&gt; key in the session to &lt;code&gt;'generic username'&lt;/code&gt; on accessing the &lt;code&gt;'/login'&lt;/code&gt; endpoint.&lt;/li&gt;
&lt;li&gt;The logout route removes the &lt;code&gt;'username'&lt;/code&gt; key from the session with &lt;code&gt;session.pop()&lt;/code&gt;.
The &lt;code&gt;session&lt;/code&gt; object in Flask provides a dictionary-like interface to store and retrieve session data. The &lt;code&gt;secret_key&lt;/code&gt; is used to sign the session cookie, ensuring its integrity and security.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;BEST PRACTICES WHEN USING COOKIES AND SESSIONS:&lt;/strong&gt;&lt;br&gt;
Using cookies and sessions requires careful consideration of privacy and security. Here are some important guidelines to follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Session Expiration: Define session expiration policies to ensure session data is cleared after a specified period of inactivity or a set amount of time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Validate and Sanitize User Input: When working with cookies and sessions, ensure that user input is validated and sanitized properly to prevent security vulnerabilities such as cross-site request forgery (CSRF) and cross-site scripting (XSS) attacks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compliance with Privacy Regulations: Follow relevant privacy regulations, such as General Data Protection Regulation (GDPR), when handling user data through cookies and sessions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encryption and Integrity: Encrypt sensitive information stored in cookies to prevent unauthorized access, such as passwords. Even if your website doesn't contain any other sensitive information. A lot of people tend to use the same passwords for multiple websites. Also, sign cookies to ensure their integrity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Secure Communication: Ensure that cookies are transmitted over secure channels (HTTPS) to prevent interception and unauthorized access. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Limited Data Storage: Store only essential data in cookies, as they are transmitted with every request and have an impact on performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cookie Expiration: Set appropriate expiration times for cookies to balance convenience and security. Shorter expiration times can enhance security, while longer expiration times offer better user convenience.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cookies and sessions are indispensable tools in web development, enabling websites to remember user preferences, track user interactions, and maintain user state. By understanding the intricacies of cookies and sessions, as well as adhering to privacy and security best practices, developers can create dynamic, personalized web applications that provide noteworthy user experiences while safeguarding user data.&lt;/p&gt;

</description>
      <category>python</category>
      <category>security</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Creating a simple quiz game using python</title>
      <dc:creator>Drew Womble</dc:creator>
      <pubDate>Fri, 09 Jun 2023 09:43:43 +0000</pubDate>
      <link>https://dev.to/drew_womble/creating-a-simple-quiz-game-using-python-2hbj</link>
      <guid>https://dev.to/drew_womble/creating-a-simple-quiz-game-using-python-2hbj</guid>
      <description>&lt;p&gt;This will be a simple tutorial on how to create a quiz game with Structured Query Language aka SQL and python. If you're unfamiliar with SQL check it out &lt;a href="https://www.w3schools.com/sql/"&gt;here&lt;/a&gt;. If you want to follow along you can use &lt;a href="https://github.com/drwomble/quiz-game-template"&gt;this&lt;/a&gt; repository I created as a template. First off we will want to get started with the &lt;code&gt;Quiz&lt;/code&gt; class:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vNUM9G1F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzi6ifq6cnu5xp9pouu7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vNUM9G1F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzi6ifq6cnu5xp9pouu7.png" alt="Quiz class" width="692" height="644"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll want to go ahead and import sqlite3 and set some variables so that we can connect to our &lt;code&gt;quiz.db&lt;/code&gt; file. Create the &lt;code&gt;__init__&lt;/code&gt; method, getters, and setters for the question and answer attributes. Then we'll want to build a class method for our &lt;code&gt;create_table&lt;/code&gt;, &lt;code&gt;drop_table&lt;/code&gt;, &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;get_all&lt;/code&gt;, and &lt;code&gt;find_by_question&lt;/code&gt;. We'll also create our &lt;code&gt;save&lt;/code&gt; method:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1i8xy1FF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42s24cu7ppqldsjuqntm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1i8xy1FF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42s24cu7ppqldsjuqntm.png" alt="SQL methods" width="558" height="913"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TtMBUp2u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6yco4hxgp0a5qkadu0q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TtMBUp2u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6yco4hxgp0a5qkadu0q.png" alt="find_by_question method" width="511" height="226"&gt;&lt;/a&gt;&lt;br&gt;
Now we can work on the &lt;code&gt;cli.py&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZUdUiX7x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ncanxpc77ab1fwcrx5d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZUdUiX7x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ncanxpc77ab1fwcrx5d.png" alt="cli.py file" width="336" height="458"&gt;&lt;/a&gt;&lt;br&gt;
Here we created a &lt;code&gt;main&lt;/code&gt; method to allow us to loop through our menu and when a user input's and option it will take us to the corresponding helper functions that we are about to create. Note the &lt;code&gt;.strip()&lt;/code&gt; at the end of line 13. That will allow us to remove and spaces the user might have accidentally entered giving us our desired response. The import at the top is preemptively imported so we can test the cli as we go. If you wish to test the cli at any point you can run the &lt;code&gt;seed.py&lt;/code&gt; file to fill the database with dummy data. Then run the &lt;code&gt;cli.py&lt;/code&gt; file to actually see what's happening with the cli. Now we just have to build our helper functions and we're done! We'll start with the &lt;code&gt;welcome()&lt;/code&gt;and &lt;code&gt;menu()&lt;/code&gt; methods:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BcuCcLci--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpeog2n0i6mxbrxf4oe7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BcuCcLci--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpeog2n0i6mxbrxf4oe7.png" alt="welcome and menu" width="348" height="265"&gt;&lt;/a&gt;&lt;br&gt;
We'll go ahead and import random and sqlite3, and we'll create the sqlite variables that we created in the &lt;code&gt;Quiz&lt;/code&gt; class, we will need them later. These functions are pretty simple allowing the user to actually see what they should input to get to their desired destination. Now we'll want to work on the &lt;code&gt;ask_question()&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N4DxjZMP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3gun3aw7lp76e3gc77k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N4DxjZMP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3gun3aw7lp76e3gc77k.png" alt="ask_question method" width="724" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we created another helper function for our helper function &lt;code&gt;randomize_questions&lt;/code&gt;. This function selects the questions from our database and gives us a single random question with its corresponding answer. The &lt;code&gt;ask_question()&lt;/code&gt; function takes the question and answer given to us by &lt;code&gt;randomize_questions()&lt;/code&gt; and compares the answer to the user's input. If correct the terminal will say you got the answer right and if not it'll tell you you're wrong and give you the correct answer. After answering the question right or wrong you'll get sent to another helper function for our helper functions to determine if the user wants to keep answering questions or if they want to go back to the menu:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3L_plllR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mrss7bg08gjwgjl1mds.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3L_plllR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mrss7bg08gjwgjl1mds.png" alt="handle_next" width="440" height="169"&gt;&lt;/a&gt;&lt;br&gt;
If the user chooses yes they'll be spit back to &lt;code&gt;ask_question()&lt;/code&gt; if they choose no they'll be redirected to the menu. Now we will want to create our function to handle creating new questions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hIfyZ3Mw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xmcldu380ey1tjbwkcu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hIfyZ3Mw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xmcldu380ey1tjbwkcu.png" alt="create_new()" width="629" height="174"&gt;&lt;/a&gt;&lt;br&gt;
Here we give the user option to input the question and answer they want to add to the database and store them as variables. Then we can use the &lt;code&gt;create&lt;/code&gt; method from the &lt;code&gt;Quiz&lt;/code&gt; class to actually add the users inputs to the database. Lastly we can add validation by using the &lt;code&gt;find_by_question&lt;/code&gt; method to check that the users inputs were successfully added to the database. Now we just need to add functions to handle the user exiting the cli and for invalid user inputs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nx9eMVYw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hncl8wshyxhz05ahiy4l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nx9eMVYw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hncl8wshyxhz05ahiy4l.png" alt="exit_cli and invalid inputs" width="463" height="174"&gt;&lt;/a&gt;&lt;br&gt;
The &lt;code&gt;exit_cli()&lt;/code&gt; function allows the user to exit the cli in a user friendly way. Although ctrl + d works too. The &lt;code&gt;invalid_inputs&lt;/code&gt; function simply prints a message to the user telling them their input was invalid and returning them to the menu. Note you don't have to call &lt;code&gt;menu()&lt;/code&gt; in &lt;code&gt;invalid_inputs()&lt;/code&gt; because &lt;code&gt;menu()&lt;/code&gt; comes before our if statement in the while loop in &lt;code&gt;cli.py&lt;/code&gt;. This means if we aren't somewhere in the loop say &lt;code&gt;ask_question()&lt;/code&gt; the user will automatically get taken back to &lt;code&gt;menu()&lt;/code&gt;. Now your done! The cli should look a little something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MsDl0A8i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdlqsg9bjiarr16qwuj7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MsDl0A8i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdlqsg9bjiarr16qwuj7.gif" alt="cli gif" width="690" height="145"&gt;&lt;/a&gt;&lt;br&gt;
Looks great right! Well not really we didn't do any styling, but it works like a charm! If you want to checkout the completed project you can find the repo &lt;a href="https://github.com/drwomble/quiz-game"&gt;here&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Create a Flashcard Application Using React</title>
      <dc:creator>Drew Womble</dc:creator>
      <pubDate>Fri, 19 May 2023 05:57:20 +0000</pubDate>
      <link>https://dev.to/drew_womble/create-a-flashcard-application-using-react-53ij</link>
      <guid>https://dev.to/drew_womble/create-a-flashcard-application-using-react-53ij</guid>
      <description>&lt;p&gt;Start by opening your terminal moving into whatever folder you want the application to be in and using &lt;code&gt;npx create-react-app flash-card-app&lt;/code&gt; flash-card-app being whatever you wish to name the project. Then I like to delete all of the unnecessary flack such as the test files and the logo &lt;code&gt;.png's&lt;/code&gt; in the public folder. After that make sure to run &lt;code&gt;npm install&lt;/code&gt; to install the node modules. Then create your &lt;code&gt;.json&lt;/code&gt; file to store all of the questions and answers.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75djvi8bi5jd1f36oxmp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75djvi8bi5jd1f36oxmp.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Once you make your &lt;code&gt;.json&lt;/code&gt; file go into the &lt;code&gt;package.json&lt;/code&gt; and inside the scripts object add this line:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbl7ij4onneet62e1wqzy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbl7ij4onneet62e1wqzy.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
This will allow you to create a RESTful API to both fetch data and post data. Make sure to run the command &lt;code&gt;npm run server&lt;/code&gt; to actually start the server. Also run &lt;code&gt;npm start&lt;/code&gt; so we can watch our progress in the browser. Next we will start building out our &lt;code&gt;App&lt;/code&gt; component which will act as our main parent.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6vgh5inj9z2wr461rigx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6vgh5inj9z2wr461rigx.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
We will create the &lt;code&gt;CardList&lt;/code&gt; component and the &lt;code&gt;Form&lt;/code&gt; component soon for now they're just placeholders. It wouldn't hurt to go ahead and import them though. Now we will actually have to fetch our data from the json server we created. Import &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt; to help us with this.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8x4iwmfrwfrqgj9z4gvv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8x4iwmfrwfrqgj9z4gvv.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Use the &lt;code&gt;setCards&lt;/code&gt; function in the &lt;code&gt;GET&lt;/code&gt; request to set cards equal to the data we get back. Then we can pass &lt;code&gt;cards&lt;/code&gt; as a prop to our &lt;code&gt;CardList&lt;/code&gt; component. Create the &lt;code&gt;CardList&lt;/code&gt; component and export it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pxvht35tc7g5u1iqxut.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pxvht35tc7g5u1iqxut.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Here we created the &lt;code&gt;CardList&lt;/code&gt; component. Created our &lt;code&gt;displayCards&lt;/code&gt; variable which maps through the cards that were passed down and sends each one to the &lt;code&gt;Card&lt;/code&gt; component that we will create in just a moment. Don't forget to add the &lt;code&gt;key={card.id}&lt;/code&gt; in the &lt;code&gt;Card&lt;/code&gt; component. React needs the key component in list items to track what gets added, removed, or reordered. Now we will create the &lt;code&gt;Card&lt;/code&gt; component with card now being passed as a prop.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3r15l93dv4x6vgdbnagm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3r15l93dv4x6vgdbnagm.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Now we can use &lt;code&gt;useState&lt;/code&gt; to implement a flip functionality to our cards.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fctr99pzdr63j9au79sxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fctr99pzdr63j9au79sxx.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
The only thing happening here is when you click on the card the state of &lt;code&gt;flipCard&lt;/code&gt; will get set to true. Then the ternary allows us to see the answer instead of the question. You should now be able to toggle between the answers and questions by clicking on the individual cards. This is great and all, but what if you want to add new flashcards without going into the json and manually creating new objects. That's why we will do just that by creating a &lt;code&gt;Form&lt;/code&gt; component. Create a normal component that returns a form with 3 inputs; one for the question, answer, and submit. Like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5al0nh0s2bdp4wpvc40.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5al0nh0s2bdp4wpvc40.png" alt="Image description"&gt;&lt;/a&gt; &lt;br&gt;
Now we will use state to set what the user types into the form and pass it to our &lt;code&gt;handleSubmit&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkz7lrozt2ppfq0wwbxo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpkz7lrozt2ppfq0wwbxo.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Here we are taking what the user types for question and answer, and setting our state variables to those values. We then pass that information into our &lt;code&gt;handleSubmit&lt;/code&gt;, create a new object with it and pass it to the &lt;code&gt;handleForm&lt;/code&gt; function. Which must be created in the main &lt;code&gt;App&lt;/code&gt; component in order to set the state of the cards to whatever the user submitted. Don't forget to add &lt;code&gt;e.preventDefault()&lt;/code&gt;! The handle form function should look like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foozmr6azlwsuuxohjm2t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foozmr6azlwsuuxohjm2t.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
This function takes the new card object and adds it onto the existing array of card objects. The only issue with this is when we refresh the page all the cards we added are now gone! To prevent this from happening we can add a &lt;code&gt;POST&lt;/code&gt; request into our &lt;code&gt;handleSubmit&lt;/code&gt; function.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj3phesczlaqsqaex37px.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj3phesczlaqsqaex37px.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Here we have our &lt;code&gt;POST&lt;/code&gt; request which takes our &lt;code&gt;newCardObject&lt;/code&gt; and post's it to our RESTful API resulting in an updated json with our information. Note we don't have to add an id to the new object. The server will do that for us. Also it's nice to reset the state of question and answer at the end of the &lt;code&gt;handleSubmit&lt;/code&gt; function. That way the form clears itself and we don't have to backspace everything from the inputs when we want to add something new. Lastly, we can add some simple styling:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wq6105043tkle3vd5g5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wq6105043tkle3vd5g5.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
And you should have fully functional flashcard application that looks something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftd0drbawlea3bt9lyccu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftd0drbawlea3bt9lyccu.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Of course if this is all too much work, or you just don't feel like making it yourself. I've published this project to github. You can find it &lt;a href="https://github.com/drwomble/flash-card-app" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Using `.map()` in JavaScript</title>
      <dc:creator>Drew Womble</dc:creator>
      <pubDate>Fri, 28 Apr 2023 15:44:23 +0000</pubDate>
      <link>https://dev.to/drew_womble/using-map-in-javascript-499</link>
      <guid>https://dev.to/drew_womble/using-map-in-javascript-499</guid>
      <description>&lt;p&gt;In JavaScript, the &lt;code&gt;.map()&lt;/code&gt; function is a higher-order function that is used to transform an array by applying a function to each element of the array. The &lt;code&gt;.map()&lt;/code&gt; function creates a new array with the results of calling the provided function on every element in the original array. It does not modify the original array. &lt;/p&gt;

&lt;p&gt;The basic syntax for the &lt;code&gt;.map()&lt;/code&gt; function is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.map(function(currentValue, index, arr))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;function()&lt;/code&gt; is required and will be the function that is run on each array element. &lt;code&gt;currentValue&lt;/code&gt; is also required and represents the value of the current element. The &lt;code&gt;index&lt;/code&gt; is optional and represents the index of the current element. &lt;code&gt;arr&lt;/code&gt; is also optional and is the array of the current element. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;.map()&lt;/code&gt; is an incredibly useful tool in JavaScript. You could use other methods such as &lt;code&gt;for...of&lt;/code&gt; loops; however, &lt;code&gt;.map()&lt;/code&gt; increases the readability of your code. &lt;/p&gt;

&lt;p&gt;Here is an example of the differences between using a &lt;code&gt;for...of&lt;/code&gt; loop and using the &lt;code&gt;.map()&lt;/code&gt; function. Let's say we have the following array of menu items at a barbeque restaurant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const menuItems = [
     {
          dish: 'Pulled Pork',
          price: 12.99
     },
     {
          dish: 'BBQ Chicken',
          price: 11.99
     },
     {
          dish: 'Brisket',
          price: 13.99
     }
 ]

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

&lt;/div&gt;



&lt;p&gt;It's May 16th! International barbeque day! The owner calls and says that everything should be 50% off! We could use a &lt;code&gt;for...of&lt;/code&gt; loop to iterate through the array and change the prices. Like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function loopAndUpdate(array, updater){
  const updatedArray = []
  for(let item of array){
      updatedArray.push(updater(item))
  }
  return updatedArray
}
function halfOff(item){
  item.price = item.price/2
  return item
}
console.log(loopAndUpdate(menuItems, halfOff))
//=&amp;gt; [
  { dish: 'Pulled Pork', price: 6.495 },
  { dish: 'BBQ Chicken', price: 5.995 },
  { dish: 'Brisket', price: 6.995 }
]
console.log(menuItems)
//=&amp;gt; [
  { dish: 'Pulled Pork', price: 6.495 },
  { dish: 'BBQ Chicken', price: 5.995 },
  { dish: 'Brisket', price: 6.995 }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this example we wrote a function that allows us to loop through our array and return the updated array. Then wrote another function to change the price to half off. Notice the original array was changed.  If we wanted to prevent that we could use the &lt;code&gt;Object.assign()&lt;/code&gt; function like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function halfOff(item){
  let itemCopy = Object.assign({}, item)
  itemCopy.price = itemCopy.price/2
  return itemCopy
}
console.log(loopAndUpdate(menuItems, halfOff))
//=&amp;gt;[
  { dish: 'Pulled Pork', price: 6.495 },
  { dish: 'BBQ Chicken', price: 5.995 },
  { dish: 'Brisket', price: 6.995 }
]
console.log(menuItems)
//=&amp;gt;[
  { dish: 'Pulled Pork', price: 12.99 },
  { dish: 'BBQ Chicken', price: 11.99 },
  { dish: 'Brisket', price: 13.99 }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example the original object remains unchanged because we made a copy of it and passed that into our &lt;code&gt;loopAndUpdate&lt;/code&gt; function. This works, but things are starting to get messy and overcomplicated. Especially when we have &lt;code&gt;.map()&lt;/code&gt; at our disposal! Let's assume we are dealing with the same object as before &lt;code&gt;menuItems&lt;/code&gt;. Here's an example using &lt;code&gt;.map()&lt;/code&gt; to do the same thing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const halfOff = menuItems.map(item =&amp;gt;{
  item.price = item.price/2
  return item
})
console.log(halfOff)
//=&amp;gt;[
  { dish: 'Pulled Pork', price: 6.495 },
  { dish: 'BBQ Chicken', price: 5.995 },
  { dish: 'Brisket', price: 6.995 }
]
console.log(menuItems)
//=&amp;gt;[
  { dish: 'Pulled Pork', price: 6.495 },
  { dish: 'BBQ Chicken', price: 5.995 },
  { dish: 'Brisket', price: 6.995 }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That looks a lot better, but I know what you're thinking. "I thought &lt;code&gt;.map()&lt;/code&gt; didn't change the original array!" While this is true JavaScript is not storing the actual object at each index of &lt;code&gt;menuItems&lt;/code&gt;. It's storing a pointer that references the location in memory at where the object is stored. &lt;code&gt;.map()&lt;/code&gt; is creating a new array, but it's filled with pointers to the same objects. To fix this we can use &lt;code&gt;Object.assign()&lt;/code&gt; in the same way we did before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const halfOff = menuItems.map(item =&amp;gt;{
  let itemCopy = Object.assign({}, item)
  itemCopy.price = itemCopy.price/2
  return itemCopy
})
console.log(halfOff)
//=&amp;gt;[
  { dish: 'Pulled Pork', price: 6.495 },
  { dish: 'BBQ Chicken', price: 5.995 },
  { dish: 'Brisket', price: 6.995 }
]
console.log(menuItems)
//=&amp;gt;[
  { dish: 'Pulled Pork', price: 12.99 },
  { dish: 'BBQ Chicken', price: 11.99 },
  { dish: 'Brisket', price: 13.99 }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fixed! Now &lt;code&gt;.map()&lt;/code&gt; really shines when we already have a helper function to take 50% off the price:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function halfOff(item){
  let itemCopy = Object.assign({}, item)
  itemCopy.price = itemCopy.price/2
  return itemCopy
}
const halfPrice = menuItems.map(halfOff)
console.log(halfPrice)
//=&amp;gt;[
  { dish: 'Pulled Pork', price: 6.495 },
  { dish: 'BBQ Chicken', price: 5.995 },
  { dish: 'Brisket', price: 6.995 }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All condensed into one line! &lt;code&gt;.map()&lt;/code&gt; really is a useful function. Hopefully now you understand why it is useful, and understand how to use it. Not only does it make your code easier to read, but it makes things easier on you too!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
