<?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: Karter L.</title>
    <description>The latest articles on DEV Community by Karter L. (@kdliving14).</description>
    <link>https://dev.to/kdliving14</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%2F883705%2F7b402d43-6b79-48f9-9bfe-89635b9510a4.jpg</url>
      <title>DEV Community: Karter L.</title>
      <link>https://dev.to/kdliving14</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kdliving14"/>
    <language>en</language>
    <item>
      <title>Databases &amp; I: an Adventure</title>
      <dc:creator>Karter L.</dc:creator>
      <pubDate>Wed, 05 Oct 2022 14:37:00 +0000</pubDate>
      <link>https://dev.to/kdliving14/databases-i-an-adventure-33l3</link>
      <guid>https://dev.to/kdliving14/databases-i-an-adventure-33l3</guid>
      <description>&lt;p&gt;Drawing up database connections aren’t as difficult as you might think. Say for example, you are creating a choose your own adventure web application, where the user would be able to see their choices and be able to reset their progress if they hit a bad end.&lt;/p&gt;

&lt;p&gt;What would you need in your database? You know for sure that you will need a User table and a Story table, but what else?&lt;/p&gt;

&lt;p&gt;That all depends on what you want to present to the user.&lt;/p&gt;




&lt;h2&gt;
  
  
  User Table
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Minimum datapoints needed:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Id (auto generated)&lt;/li&gt;
&lt;li&gt;Username&lt;/li&gt;
&lt;li&gt;Password&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Absolutely needed to keep track of input per user, aka where the user left off in the story and the choices they've made in the story.&lt;/p&gt;




&lt;h2&gt;
  
  
  Story Table
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Minimum datapoints needed:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Id (auto generated)&lt;/li&gt;
&lt;li&gt;Title&lt;/li&gt;
&lt;li&gt;Description&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Think of this like the front and back cover of a book where it says the title and the brief blurb about the story. If we wanted this to be a regular story without the choose your own adventure aspect, we could put all of the story in here but...&lt;/p&gt;




&lt;h2&gt;
  
  
  Events Table
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Minimum datapoints needed:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Id (auto generated)&lt;/li&gt;
&lt;li&gt;Story_id (which story does the event belong to?)&lt;/li&gt;
&lt;li&gt;Event description (what you are displaying to the user)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The story table could hold all of the events but if you ever want to expand to more stories, you would end up getting all of the events mixed up between stories. &lt;/p&gt;

&lt;p&gt;Not to mention that in order to be able to have the user pick what they want to happen, you need to have events (chapters) separated so that when they choose an option it can change the story trajectory.&lt;/p&gt;




&lt;h2&gt;
  
  
  Choices Table
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Minimum datapoints needed:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Id (auto generated)&lt;/li&gt;
&lt;li&gt;Choice description (what you are displaying to the user)&lt;/li&gt;
&lt;li&gt;Event_id (what event it belongs to)&lt;/li&gt;
&lt;li&gt;Next Event Id (what event goes next if selected)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If we had the choices in the events table it would be extremely difficult to map out which choice leads to what event. Separating them out makes it easier to navigate plus then you can get a little creative on what you want each choice to include: a little picture to symbolize the choice, a sound effect to be played when it's selected?&lt;/p&gt;




&lt;p&gt;Ok so those are pretty basic, but how are we going to be able to track the user choices? Making the User have an array data point is not the easiest thing to do but what is the alternative? &lt;em&gt;More tables!&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  User Stories Table
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Minimum datapoints needed:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Id (auto generated)&lt;/li&gt;
&lt;li&gt;User_id&lt;/li&gt;
&lt;li&gt;Story_id &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This table will be our connection (join table) between the User and the Stories. Having this table will make it so that if we ever add more stories, our database will be easily expandable and the user can do more than one story at a time.&lt;/p&gt;




&lt;h2&gt;
  
  
  User Choices Table
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Minimum datapoints needed:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Id (auto generated)&lt;/li&gt;
&lt;li&gt;Userstory_id&lt;/li&gt;
&lt;li&gt;Event_id &lt;/li&gt;
&lt;li&gt;Choice_id&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally down to the meat and potatoes of what we actually wanted in the first place. This table will be our connection between Users and their choices. &lt;/p&gt;

&lt;p&gt;However, we don't want to just have a User data point and a Choice data point. &lt;/p&gt;

&lt;p&gt;For one thing, the choice belongs to an Event. Without that context, it could get confusing for the user to determine what they did in the story at whatever time. So we definitely need to add an Event connection.&lt;/p&gt;

&lt;p&gt;Secondly, if we just link the user choice directly to the user, it becomes incredibly difficult to not only delete the user choices when it comes time to reset the story but it also makes it difficult for the User to determine what Story the choices are from as you would have to go several layers deep to get that connection back (Choice &amp;gt; Event &amp;gt; Story).&lt;/p&gt;

&lt;p&gt;Connecting it to the User Stories table instead of the User table will allow us to not only easily reset the User's progress (using dependent destroy on the User Stories table) but it will also allow us to easily separate out the choices per Story on the front end.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yp52hI8v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzhqsf683xnjebq8osur.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yp52hI8v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzhqsf683xnjebq8osur.png" alt="Database Diagram" width="880" height="697"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All of these tables allow the project to expand with time; whether that be with more users, stories, or even expanded mechanics (inventory system maybe?). I'm definitely excited to see where this could go. The diagram above shows a little more than what I previously described, but that's the beauty of this project, there is so many ways to expand!&lt;/p&gt;

&lt;p&gt;If you found this interesting, checkout the project on &lt;a href="https://github.com/kdliving14/adventure"&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;( &lt;a href="https://unsplash.com/photos/3OiYMgDKJ6k"&gt;Header image from here&lt;/a&gt; )&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>database</category>
      <category>programming</category>
    </item>
    <item>
      <title>Ruby Active Record Cheat Sheet</title>
      <dc:creator>Karter L.</dc:creator>
      <pubDate>Wed, 14 Sep 2022 04:08:07 +0000</pubDate>
      <link>https://dev.to/kdliving14/ruby-active-record-cheat-sheet-35la</link>
      <guid>https://dev.to/kdliving14/ruby-active-record-cheat-sheet-35la</guid>
      <description>&lt;p&gt;Hello and welcome back to: Cheat Sheets with Karter. &lt;/p&gt;

&lt;p&gt;In today's cheat sheet we will be briefly looking at the beginning steps of ruby's active record functionality.&lt;/p&gt;

&lt;p&gt;Copy and paste this into a code editor and see all the pretty colors!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--------------------SET UP--------------------

# 1. Run in console to install gems in Gemfile bundle install

# 2. do migrations (see RAKE CONSOLE COMMANDS &amp;amp; MIGRATION FILES)
    # Verify schema file is correct before continuing

# 3. set up Class files if not set up (see CLASSES and everything below it)
    # Set up the has_many &amp;amp; belongs_to relationships first

# 4. set up seeds.rb file if not set up for running tests (see SEED FILE)
    # Run in console to seed data
    bundle exec rake db:seed

# 5. Run console (should have pry in Rakefile task) (see RUNNING TESTS)
    bundle exec rake console
    # can put binding.pry in code anywhere but reset console first
    # to get out of pry =&amp;gt; control + D (on mac)

    #reset console/seeds if needed 
    rake db:reset

    # clear console of previous tests (!does not reset console!)
    clear

    # Returns all the methods you can use
    User.methods 
    # OR
    Ruby.methods

-----------RAKE CONSOLE COMMANDS-------------

#          where v      v what do      name of file v
bundle exec rake db:create_migration NAME=create_artists

# write in generated file then can migrate using command below
bundle exec rake db:migrate

# check status of migration
bundle exec rake db:migrate:status

# if mistake, take back mistakes
bundle exec rake db:rollback

---------------MIGRATION FILES---------------

# after doing =&amp;gt; bundle exec rake db:create_migration NAME=______

class CreateArtists &amp;lt; ActiveRecord::Migration[6.1]   
  def change

    # create a table called artists with columns (t)
    create_table :artists do |t|

      # v data type 
      t.string :name
      #           ^name of data (column name)

      # v lots of different data types
      t.string :birthdate
      t.integer :age
      t.float :price
      t.datetime :release_date
      t.text :description
      t.boolean :cool 

      t.timestamps
      # ^ creates a created_at column and a updated_at column

      # id column auto generated for every table
      # DO NOT put symbols as part of a data name, 0/10 do not recommend
    end
  end
end

# in def change can also do these if not making table:

#            v table   v new column    v data type
add_column :artists, :favorite_food, :string

#                v table   v column    v new data type
change_column :artists, :birthdate, :datetime

---------------RUNNING TESTS------------------

# if testing an instance's association 
# EX: show all pets that belong to shelter
Shelter.first.pets
# =&amp;gt; Class that owns thing . first instance . Class that belongs to it (lowercase and plural)

# if testing the Class method
# EX: return the oldest pet
Pet.oldest
# =&amp;gt; Class . method in class

-----------------SEED FILE--------------------
# make a file called seeds.rb in db folder

# if you want to use id from one class in another, save to variable
t1 = Class1.create(key:"value")

# ids are auto generated, so no need to make them
Class2.create(attached_id: t1.id, key1:"value1", key2:2, key3:false)

# highly recommend doing:
puts "Done!"

# so that you know it didn't freeze on you.

------------------CLASSES------------------

# Must inherit Active Record in order to get all the cool shit
class Dog &amp;lt; ActiveRecord::Base 
    belongs_to :pet 
    # Dog is part of Pet

    has_many :appointments
    # Dog has many appointments

    has_many :doctors, through: :appointments
    # Dog has many doctors through appointments

    @@dog_count = 0
    # class variable

    def initialize
        # upon initialization
        super
        # do *initialize* from inherited files first
        # can take an arg EX: super(name)

        @@dog_count += 1
        # add 1 to class variable
    end

    def get_adopted(owner_name)
        self.owner = owner_name
        # self refers to the instance of Dog that is being called
    end
end

# objects in Dog are accessed by dot notation
fido = Dog.new("Fido")
# fido = new instance of Dog, passing in "Fido" as name

fido.get_adopted("Sophie")
# sets "Sophie" as owner_name

------------------METHODS------------------

#                               v default value for name
def greet_with_default (name="programmer")
    puts "Hello, #{name}!"
end
# def -&amp;gt; identifies it as a method
    # parentheses are optional if not passing data
    # last line of a method is the return value unless you put return
# end -&amp;gt; end of method (always needed)

------------------------------------------

# keyword args make it less likely to break
def happy_birthday(person:, current_age:)
  puts "Happy Birthday, #{person}"
  current_age += 1
  puts "You are now #{current_age} years old"
end

# call the method with the keywords and values
happy_birthday(current_age: 31, person: "Carmelo Anthony")

------------------------------------------

def self.method 
    self.order
end
# ^self refers to class in both because it's in method name

def method
    self
end
# ^self refers to an instance because it's not in method name

=================ACTIVE RECORD METHODS======================
# more info:
# https://guides.rubyonrails.org/active_record_querying.html
# https://rubydoc.info/stdlib/core/Enumerable:group_by
# https://apidock.com/ruby/Enumerable/group_by

# vv inherit ActiveRecord to get the following vv #

--------attr_accessors are built into Active Record--------

# Class =&amp;gt; substitute name of class you want to use
# Class_Instance =&amp;gt; substitute instance of a class 

# Can combine things listed below!!

Class.new(___:___)
# creates new instance of class using variables listed in (__)
    # ex: Class.new(name: 'Jon')
# not saved in db until run .save
# can save to var to change instance variables separately

Class_Instance.save
# save changes of an instance to the database

Class.create(___:___)
# create a new entry of Class in database
# .new + .save put together
    # ____ =&amp;gt; variables, ex: Class.create(name: 'Jon')

Class.update(____:____)
# updates all instance vars to new data
# also works on instance basis: self.update(__:__) or Class.first.update(__:__)

Class_Instance.update(_____:_____)
# updates instance to new data

Class.all
# returns all the records from the table as instances of Class

Class.column_names
# retrieves a list of all the columns in the table

Class.find(_)
# retrieve a Class object from database by id (_)

Class.find_by(____:____)
# find by any attribute
    # ____ =&amp;gt; variables, ex: Class.find_by(name: 'Jon')
    # returns nil if not found

Class.pluck(____)
# returns array of all _____ of the instances of Class
    # EX: Class.pluck(:name) =&amp;gt; returns a list of all the instance names in Class

Class.where(___:___)
# find but returns list of all that match query (___:___)

Class.find_or_create_by(___:___)
# Creates instance of class if it does not exist
    # ____ =&amp;gt; variables, ex: Class.find_or_create_by(name: 'Jon')

Class.order(___)
# orders the instances for Class by ____
    # EX: Class.order(:name) =&amp;gt; orders instances by name

Class.first
# gets first instance created, can do second, third, etc

Class.last
# gets last instance created

Class_Instance.destroy 
# deletes instance

Class.destroy
# deletes last instance created (returns deleted instances)

Class.delete_all
#deletes all instances (returns number of instances deleted)

Class.destroy_all
# deletes all instances (returns deleted instances)

Class.sum(___)
# adds up all of the keys that match

Class.all.length
# returns num of instances in Class

Class_Instance.group_by {|p| p._____}
# EX: (1..6).group_by { |i| i%3 }   
#=&amp;gt; {0=&amp;gt;[3, 6], 1=&amp;gt;[1, 4], 2=&amp;gt;[2, 5]}

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

&lt;/div&gt;



&lt;p&gt;Please let me know if I'm missing anything :)&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>programming</category>
      <category>activerecord</category>
    </item>
    <item>
      <title>A Cheat Sheet for Ruby</title>
      <dc:creator>Karter L.</dc:creator>
      <pubDate>Tue, 23 Aug 2022 01:33:05 +0000</pubDate>
      <link>https://dev.to/kdliving14/a-cheat-sheet-for-ruby-1ic8</link>
      <guid>https://dev.to/kdliving14/a-cheat-sheet-for-ruby-1ic8</guid>
      <description>&lt;p&gt;A lot of this can be cut out by Active Record and/or Rails but it's always good to know the bare bones. &lt;/p&gt;

&lt;p&gt;Below are brief references and explanations to some basic parts of Ruby code. I recommend copying this and pasting it into a code editor so it becomes color coded (and easier to read).&lt;/p&gt;




&lt;h2&gt;
  
  
  VARIABLE DECLARATIONS
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;attr_reader :age
# makes "age" something you can only display, not edit (getter method)

attr_writer :name
# makes "name" something you can change, not display (setter method)

attr_accessor :owner
# makes "owner" able to display &amp;amp; edit (getter &amp;amp; setter)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  METHODS
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#                               v default value
def greet_with_default (name="programmer")
    puts "Hello, #{name}!"
end

# def -&amp;gt; identifies it as a method
    # parentheses are optional if not passing data
    # last line of a method is the return value unless you put return
# end -&amp;gt; end of method (always needed)

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

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# using keyword arguments make it less likely to break (____:)

def happy_birthday(person:, current_age:)
  puts "Happy Birthday, #{person}"
  current_age += 1
  puts "You are now #{current_age} years old"
end

# call the method with the keywords and values
happy_birthday(current_age: 31, person: "Carmelo Anthony")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def self.method 
    self.order
end
# ^self refers to class in both because it's in method name

def method
    self
end
# ^self refers to an instance because it's not in method name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# initialize without knowing what arguments you have
def initialize (args = nil)
    # upon initialization (start) accept (hash), default = nil

    if args
    #  ^ if there is a hash do stuff

            # v go through each key in hash
        args.each_key do |key, value|
                         # ^look at key &amp;amp; value

            self.class.attr_accessor(key)
            # ^ dynamically set methods automatically for all keys

            self.send("#{key}=", args[key])
            # ^send attribute to the self's class
        end
    end
end

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

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private
# any methods under "private" cannot be called by explicit receiver (Dog.privatemethod)
# can be called by other methods
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CLASSES
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Dog &amp;lt; Pet
# Dog is inheriting all the stuff from Pet class (separate file)

    extend FancyDance::ClassMethods
    # get module class methods

    include FancyDance::InstanceMethods
    # get module instance methods

    @@dog_count = 0
    # class variable

    def initialize
        # upon initialization
        super
        # do *initialize* from inherited file (Pet) first
        # can take an argument EX: super(name)

        @@dog_count += 1
        # add 1 to class variable
    end

    def get_adopted(owner_name)
        self.owner = owner_name
        # self refers to the instance of Dog that is being called
    end
end

# objects in Dog are accessed by dot notation
fido = Dog.new("Fido")
# fido = new instance of Dog, passing in "Fido" as name

fido.get_adopted("Sophie")
# sets "Sophie" as owner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  MODULES
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module FancyDance
    # can nest modules
    module InstanceMethods
        def twirl
            "I'm twirling!"
        end

        def initialize
            self.class.all &amp;lt;&amp;lt; self
            # put current instance in matching class array
            # EX: @@dog_list &amp;lt;&amp;lt; self
        end
    end

    module ClassMethods
        def metadata
            "Dogs love to dance."
        end
    end
end

fido.twirl
# instance output "I'm twirling!"

Dog.metadata
# class output "Dogs love to dance."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  HASHES
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# only use bracket notation
my_hash = { key1: "value1", key2: "value2" }
my_hash[:key2]
# =&amp;gt; "value2"

# Equivalent to:
{ :key1 =&amp;gt; "value1", :key2 =&amp;gt; "value2" }

my_hash.keys
# returns the keys in my_hash

my_hash.values
# returns the values in my_hash

my_hash.empty?
# eval if hash is empty (true/false value)

my_hash.delete(:key1)
# deletes (key1), returns edited hash

my_hash.merge(other_hash)
# returns edited my_hash with other_hash appended
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ARRAYS
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list.detect{|a| a.name == name}
# returns the first element that matches (array only)
# If no block, then it returns enumerator (name).

list.include? "blah"
# does list include "blah"? true/false value

[1, 2, 3].sum
# =&amp;gt; 6 (adds values together)

[1, 1, 2, 3, 3, 5, 5].uniq
# =&amp;gt; [1, 2, 3, 5] (returns only unique values)

[1, 3, 400, 7].length
# =&amp;gt; 4 (returns number of items in array)

[5, 100, 234, 7, 2].sort
# =&amp;gt; [2, 5, 7, 100, 234] (sorts smallest to largest)

[1, 2, 3].reverse
# =&amp;gt; [3, 2, 1] (put in reverse order)

list.count 
# counts amount of items in list

list.first
# returns first thing in array

list.last
# returns last thing in array

list.push("M&amp;amp;Ms")
list &amp;lt;&amp;lt; "M&amp;amp;Ms"
# Both put M&amp;amp;Ms at end of array, returns edited array

list.unshift("Cake")
# puts Cake at beginning of array, returns edited array

list.pop
# removes last item, returns removed item

list.shift
# removes first item, returns removed item

list1.concat(list2)
# combine arrays into original array (list1), returns edited array

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  STRINGS
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var.gsub(' ', '-')
# substitute space for dash (string only)

first_name, last_name = full_name.split
# default of split is by space (string only)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  MAP / FILTER / FIND / SORT
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def spicy_foods 
  [
    { name: 'Green Curry', cuisine: 'Thai', heat_level: 9 },
    { name: 'Buffalo Wings', cuisine: 'American', heat_level: 3 },
    { name: 'Mapo Tofu', cuisine: 'Sichuan', heat_level: 6 }
  ]
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#MAP the foods in the list by name, returns array of names
spicy_foods.map{ |food| food[:name] }

# Equivalent to:
spicy_foods.map do |food|
    food[:name]
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#FILTER foods by heat level &amp;gt; 5, returns array of objects
spicy_foods.filter{ |food| food[:heat_level] &amp;gt; 5 }

# Equivalent to:
spicy_foods.filter do |food|
    food[:heat_level] &amp;gt; 5 
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#FIND food that = what you are looking for, returns 1 object
spicy_foods.find{ |food| food[:cuisine] == cuisine }

# Equivalent to:
spicy_foods.find do |food|
    food[:cuisine] == cuisine
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#SORT by given key (ASCII value), returns array of objects (no change array)

# (&amp;lt;=&amp;gt;) combined comparison operator, returns:
    # 0 if the first operand equals the second,
    # -1 if the first operand is less than the second, and
    # 1 if the first operand is greater than the second.

# sort method, need 2 objects passed to it
spicy_foods.sort do |food1, food2|
  food1[:name] &amp;lt;=&amp;gt; food2[:name]
end

# sort_by only needs 1 element passed to it
spicy_foods.sort_by {|food| food[:heat_level]}

# Equivalent to:
spicy_foods.sort_by do |food|
    food[:heat_level]
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  LOOPS
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num = 10
until num == 0
    puts "#{num}"
    num -= 1
end
#until the num = 0, count down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num = 0
while num &amp;lt;= 100
    puts "#{num}"
    num += 1
end
#while num is &amp;lt;= 100, count up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10.times do |i|
  puts "i is: #{i}"
end
# Equivalent to:
10.times { |i| puts "i is: #{i}" }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# v range
(1..20).each do |b|
  puts "#{b}"
end
# ^ puts value of b while iterating through range of 1-20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  PRINT TO CONSOLE
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;puts "Hello World!"
# puts -&amp;gt; console.log() with line break @ end
    # makes the data into a string

print "Hello"
# print -&amp;gt; console.log() no line breaks


p [1, 2, 3]
# p -&amp;gt; output data in nicer format by calling .inspect
# equivalent to calling v
    # puts [1, 2, 3].inspect


pp [{ id: 1, hello: "World" }, { id: 2, hello: "Ruby" }, { id: 3, hello: "Moon" }, { id: 4, hello: "Learner" }]
# pp -&amp;gt; pretty printing for complex data (nested arrays/hashes)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Hope this was a good reference!&lt;/p&gt;

&lt;p&gt;Please let me know if you think I'm missing anything significant in the comments down below.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Common React Errors and how to Troubleshoot them</title>
      <dc:creator>Karter L.</dc:creator>
      <pubDate>Wed, 03 Aug 2022 00:03:00 +0000</pubDate>
      <link>https://dev.to/kdliving14/common-react-errors-and-how-to-troubleshoot-them-57f5</link>
      <guid>https://dev.to/kdliving14/common-react-errors-and-how-to-troubleshoot-them-57f5</guid>
      <description>&lt;p&gt;I'm just going to put it out there: &lt;em&gt;react is the biggest baby there ever was&lt;/em&gt;. If you forget a , or put in an extra ; the world will literally end. While sometimes it can be intuitive and tell you what is wrong, oftentimes the error messages are less than descriptive (if they show at at all).&lt;/p&gt;

&lt;p&gt;Below is a short list of errors I've received and how I troubleshoot them.&lt;/p&gt;

&lt;p&gt;BUT FIRST! Your saving grace should always be open: the &lt;em&gt;&lt;strong&gt;console&lt;/strong&gt;&lt;/em&gt;! That is where 99% of your error messages are going to be if they don't take up the page.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yeqnv1-E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54d6q0vum5qjvsyxdhjr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yeqnv1-E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54d6q0vum5qjvsyxdhjr.png" alt="Image description" width="880" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Missing , " &amp;gt; ] } )&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This one seems scary on the upset, but the solution is fairly easy to spot. The red arrows will point approximately where you need to look.&lt;/p&gt;

&lt;p&gt;In this case it is pointing to line 11, but it syntactically looks fine, what's the deal? Basically, the computer doesn't know what exactly is wrong, just that it all went down hill at that point. A good rule of thumb here is to look at indicated line and then at the line above because sometimes computers don't know what's wrong, just that something is wrong.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u4TRRpA---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/687ien7l4974ijmqpucg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u4TRRpA---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/687ien7l4974ijmqpucg.png" alt="Image description" width="880" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Missing Definition or Typo&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This one seems pretty easy. I forgot to define &lt;code&gt;songs&lt;/code&gt; in my component. However, when I look at my code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U-mFNNtp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0twkrmu813xi8n7v47gf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U-mFNNtp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0twkrmu813xi8n7v47gf.png" alt="Image description" width="880" height="771"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was trying to map &lt;code&gt;songs&lt;/code&gt; that are passed into my component. What I passed into my component (&lt;code&gt;song&lt;/code&gt;) should match what I'm trying to map (&lt;code&gt;songs&lt;/code&gt;). So this particular error could also refer to typos. &lt;/p&gt;

&lt;p&gt;To help determine which is which: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if it is an item you are passing in, make sure it matches. &lt;/li&gt;
&lt;li&gt;if it originates in the component/function you are working with, check to see if you put &lt;code&gt;const&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, or &lt;code&gt;var&lt;/code&gt; to define it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If that doesn't work: console log it! You'll be able to trace it from there.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9Mz1GIuP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfb963t3ew04frimd3r8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9Mz1GIuP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfb963t3ew04frimd3r8.png" alt="Image description" width="880" height="489"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;.map or .filter isn't a function&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Considering &lt;code&gt;.map()&lt;/code&gt; and &lt;code&gt;.filter()&lt;/code&gt; are built into react/javascript, you would think that it would be hard to mess them up but that is not the case.&lt;/p&gt;

&lt;p&gt;The key to troubleshooting this is in their definitions: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;The &lt;code&gt;map&lt;/code&gt; method is used to traverse and display a list of similar &lt;strong&gt;objects&lt;/strong&gt; of a component.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The &lt;code&gt;filter&lt;/code&gt; method is used to loop through an &lt;strong&gt;array&lt;/strong&gt; while including or excluding elements inside that &lt;strong&gt;array&lt;/strong&gt; based on a condition.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.map()&lt;/code&gt; and &lt;code&gt;.filter()&lt;/code&gt; are unfortunately very specialized and do not know how to handle other types of data. &lt;code&gt;.map()&lt;/code&gt; can only handle objects and &lt;code&gt;.filter()&lt;/code&gt; can only handle arrays.&lt;/p&gt;

&lt;p&gt;Easy way to check what you are passing to the methods is to console log the variable (in this case it would be &lt;code&gt;songs&lt;/code&gt;). You need to make sure that you are passing an object to &lt;code&gt;.map()&lt;/code&gt; and an array to &lt;code&gt;.filter()&lt;/code&gt; otherwise you will continue to get that error. &lt;/p&gt;




&lt;p&gt;Most errors when you are first starting out are just syntax errors or typos but there are some more advanced errors you could get. &lt;/p&gt;

&lt;p&gt;One surefire way to solve errors is by commenting out things one by one and console logging each input and output.&lt;/p&gt;

&lt;p&gt;I found &lt;a href="https://blog.logrocket.com/8-common-react-error-messages-how-address-them/"&gt;this post&lt;/a&gt; which goes into some more error messages if you want to learn more.&lt;/p&gt;

&lt;p&gt;Happy coding! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Make Interactable Forms (with HTML &amp; Javascript)</title>
      <dc:creator>Karter L.</dc:creator>
      <pubDate>Tue, 12 Jul 2022 03:33:00 +0000</pubDate>
      <link>https://dev.to/kdliving14/how-to-make-interactable-forms-with-html-javascript-393l</link>
      <guid>https://dev.to/kdliving14/how-to-make-interactable-forms-with-html-javascript-393l</guid>
      <description>&lt;p&gt;Ok so you have your website looking pretty with headers, paragraphs of information, images, etc. but now you want something back: engagement and interactivity. But how? &lt;/p&gt;

&lt;p&gt;Your first step is in HTML.&lt;/p&gt;

&lt;p&gt;Not all forms are alike and depending on what you want to do, you may want to limit the user interaction, but there are some basics that are helpful to get started.&lt;/p&gt;




&lt;p&gt;Form Tag: The form tag is like a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; that is specifically for things you want your user to interact with. There are, however, some cool differences between regular &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; that we'll get more into when we go into the Javascript portion.&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;form id="example_form"&amp;gt;&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within the opening &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; and closing &lt;code&gt;&amp;lt;/form&amp;gt;&lt;/code&gt; tags there are the following options:&lt;/p&gt;




&lt;p&gt;Text Box: Where the user is able to type in something to be submitted. When the form is submitted, this option will return what was typed in by the user as a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Textbox: &amp;lt;input id="name" type="text"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q9J8KU3x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnku5h867cy97f28t6mx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q9J8KU3x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnku5h867cy97f28t6mx.png" alt="Textbox example" width="446" height="76"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Big Textbox: This is just a bigger version of the text box listed above. Like above, this option will return the typed data from the user as a string. Difference here is that you can specify how big the box is with columns and rows.&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;textarea cols="20" name="textbox" rows="5"&amp;gt;Textbox&amp;lt;/textarea&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vnXw5rPW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8ia22uewcwofceee0u3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vnXw5rPW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8ia22uewcwofceee0u3.png" alt="Big Textbox example" width="381" height="186"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Number Box: Where the user is able to either type or use the arrows on the side of the box to adjust the &lt;code&gt;value&lt;/code&gt; (which can have a default as shown in the below example) the amount of &lt;code&gt;steps&lt;/code&gt; indicated. Having &lt;code&gt;type = "number"&lt;/code&gt; makes it so that you can increment and decrement using arrow keys, otherwise this would be a normal text box. This option will return the &lt;code&gt;value&lt;/code&gt; when the form is submitted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Number: &amp;lt;input id= "number_box" max="99" min="1" step="1" type="number" value="18"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uK_8DJzX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5gq17d5bhd9dp5b5ecd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uK_8DJzX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5gq17d5bhd9dp5b5ecd.png" alt="Number box example" width="233" height="76"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Drop Down Selection: Where the user is able to select from a number of predefined options through a drop down list. The text in between the &lt;code&gt;&amp;lt;option&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;\option&amp;gt;&lt;/code&gt; tags is what the user sees. However, when they select Option 1, they will be returning the &lt;code&gt;value&lt;/code&gt; of Option 1 ("opt1"). The &lt;code&gt;selected&lt;/code&gt; tag indicates that the value it is attached to is the default selection (what the user will see when they first come across the form).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Drop Down Selection &amp;lt;select id="drop_down_window"&amp;gt;
&amp;lt;option selected="selected" value="opt1"&amp;gt;Option 1&amp;lt;/option&amp;gt;
&amp;lt;option value="opt2"&amp;gt;Option 2&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DWzWpceS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oba6egexk5goutlt03h6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DWzWpceS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oba6egexk5goutlt03h6.png" alt="Drop Down example" width="466" height="69"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Bubbles/Checkboxes: Where the user can select from predefined options through selecting the bubble/box associated with the option they would like to indicate. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you would like checkboxes, make &lt;code&gt;type = "checkbox"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If you would like bubbles, make &lt;code&gt;type = "radio"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;checked&lt;/code&gt; tag is used to indicate that it should be automatically checked before the user selects it.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;value&lt;/code&gt; is what the application will actually return when the form is submitted.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;name&lt;/code&gt; tag (if named the same between options) helps keep your &lt;code&gt;radio&lt;/code&gt; buttons connected so that when you click one it unclicks the other
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input checked="checked" name= "bubbles" type="radio" value="opt3"/&amp;gt; Option 3 
&amp;lt;input name = "bubbles" type="radio" value="opt4"/&amp;gt; Option 4
&amp;lt;input type="checkbox"/&amp;gt; Checkbox
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZT8Fp_Fn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbbnigcojsg4dxjq6ljf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZT8Fp_Fn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbbnigcojsg4dxjq6ljf.png" alt="Bubbles and Checkbox example" width="554" height="74"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Submit Button: Finally, how to actually submit the information! This code makes a button. The &lt;code&gt;value&lt;/code&gt; tag is also the label of the button (aka visible to the user). Buttons can trigger a &lt;code&gt;submit&lt;/code&gt; event (more details on that later).&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;input type="submit" value="Submit"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C2iGC8cs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cgvp8b1qsy04pliax4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C2iGC8cs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cgvp8b1qsy04pliax4s.png" alt="Submit Button example" width="147" height="73"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;You've got all that, but it doesn't do anything, why? We haven't told our application to listen for it. We will be doing that with step 2: Javascript.&lt;/p&gt;

&lt;p&gt;I am going to be assuming you have used an &lt;code&gt;eventListener&lt;/code&gt; before but if not, please see &lt;a href="https://www.makeuseof.com/javascript-event-listeners-how-to-use/"&gt;this&lt;/a&gt; on the basics of how to use one. &lt;/p&gt;

&lt;p&gt;Let's jump in: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First you need to select your form from your HTML document using &lt;code&gt;document.querySelector(____)&lt;/code&gt; or &lt;code&gt;document.getElementById(____)&lt;/code&gt; (I don't recommend getting it by class names just in case you want to have more than one form.) The blank should be the identifier for the form (id, class, name, tag, etc)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;(Here's where we get to the cool part!) Add your &lt;code&gt;Event Listener&lt;/code&gt; with the event type of &lt;code&gt;"submit"&lt;/code&gt; and either call a callback function to deal with the data or make a function inside like the example below:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.querySelector("#form").addEventListener("submit", e =&amp;gt; 
{
e.preventDefault();
const userInput = document.querySelector("#name").value;
document.querySelector("#form").reset()
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.preventDefault()&lt;/code&gt; is piece of code that prevents the page from automatically reloading when you submit a form. This is helpful so that you can actually capture the information given on the form instead of it being wiped away with the refresh.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Because we added an event listener to the &lt;code&gt;form&lt;/code&gt; and listened for the &lt;code&gt;submit&lt;/code&gt;, all the user input inside the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; tags has been submitted! All we need to do is access it by finding the id tag (in this case &lt;code&gt;"#name&lt;/code&gt;) of the input in question and using &lt;code&gt;.value&lt;/code&gt; which will return the value of the targeted input. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After you've received the values, I recommend wiping the form with &lt;code&gt;.reset()&lt;/code&gt; if you are not going to redirect the page or hide the form after completion so that there is no confusion as to whether or not your user has submitted the form.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, you can do all sorts of things with user submitted information but my last brain cell has just died so now it is up to you to do with this information what you will...&lt;/p&gt;

&lt;p&gt;Good luck!&lt;/p&gt;

&lt;p&gt;***********************&lt;strong&gt;&lt;em&gt;RESOURCE&lt;/em&gt;&lt;/strong&gt;***********************&lt;br&gt;
&lt;a href="https://htmlcheatsheet.com/"&gt;HTML CHEAT SHEET&lt;/a&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
