<?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>Forem: Joshua Shane Doud Jr.</title>
    <description>The latest articles on Forem by Joshua Shane Doud Jr. (@jdoud1993).</description>
    <link>https://forem.com/jdoud1993</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%2F941224%2Fda8440c8-b52a-4770-a928-fa132dc16be8.jpg</url>
      <title>Forem: Joshua Shane Doud Jr.</title>
      <link>https://forem.com/jdoud1993</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jdoud1993"/>
    <language>en</language>
    <item>
      <title>Leveraging Active Storage in Ruby on Rails and React</title>
      <dc:creator>Joshua Shane Doud Jr.</dc:creator>
      <pubDate>Tue, 19 Dec 2023 00:12:51 +0000</pubDate>
      <link>https://forem.com/jdoud1993/leveraging-active-storage-in-ruby-on-rails-and-react-2hok</link>
      <guid>https://forem.com/jdoud1993/leveraging-active-storage-in-ruby-on-rails-and-react-2hok</guid>
      <description>&lt;p&gt;Introduction:&lt;/p&gt;

&lt;p&gt;In the dynamic landscape of web development, seamlessly handling file uploads and storage is a crucial aspect of building modern applications. Ruby on Rails, a robust back-end framework, simplifies this process with its Active Storage feature. When paired with the powerful front-end library React, developers can create a smooth and efficient experience for users.&lt;/p&gt;

&lt;p&gt;In this comprehensive guide, we'll explore the integration of Active Storage in Ruby on Rails with a React front end. We'll cover everything from setting up Active Storage to handling file uploads and rendering images in a React application.&lt;/p&gt;

&lt;p&gt;Getting Started:&lt;/p&gt;

&lt;p&gt;Setup Ruby on Rails with Active Storage:&lt;br&gt;
Start by creating a new Rails application or integrating Active Storage into an existing one. Ensure you have the necessary gems installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'rails', '~&amp;gt; 6.0'
gem 'image_processing'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run bundle install to install these gems. Next, set up Active Storage by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails active_storage:install
rails db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure Models:&lt;br&gt;
Incorporate Active Storage into your models by using the has_one_attached or has_many_attached macro. For example, in a Post model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Post &amp;lt; ApplicationRecord
  has_one_attached :image
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Handling File Uploads:&lt;/p&gt;

&lt;p&gt;Create a Form in React:&lt;br&gt;
When integrating with React, create a form that allows users to upload files. Utilize the fetch API or a library like Axios to send the file to your Rails backend. Here's a simple example using React and Axios:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import axios from 'axios';

const FileUploadForm = () =&amp;gt; {
  const [file, setFile] = useState(null);

  const handleFileChange = (event) =&amp;gt; {
    setFile(event.target.files[0]);
  };

  const handleUpload = async () =&amp;gt; {
    const formData = new FormData();
    formData.append('image', file);

    try {
      await axios.post('/api/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      });
      console.log('File uploaded successfully!');
    } catch (error) {
      console.error('Error uploading file:', error);
    }
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;input type="file" onChange={handleFileChange} /&amp;gt;
      &amp;lt;button onClick={handleUpload}&amp;gt;Upload&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};


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

&lt;/div&gt;



&lt;p&gt;Handle File Upload in Rails:&lt;br&gt;
In your Rails controller, handle the file upload by attaching it to the corresponding model. For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApiController &amp;lt; ApplicationController
  def upload
    post = Post.create
    post.image.attach(params[:image])
    render json: { status: 'success' }
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rendering Images in React:&lt;/p&gt;

&lt;p&gt;Retrieve Image URLs:&lt;br&gt;
After a successful upload, you'll want to retrieve the URL of the uploaded image from the Rails backend. Modify the Rails controller to include the image URL in the response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApiController &amp;lt; ApplicationController
  def upload
    post = Post.create
    post.image.attach(params[:image])
    render json: { status: 'success', image_url: url_for(post.image) }
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Display Images in React:&lt;br&gt;
Update your React component to display the uploaded image using the received URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import axios from 'axios';

const FileUploadForm = () =&amp;gt; {
  const [file, setFile] = useState(null);
  const [imageUrl, setImageUrl] = useState(null);

  const handleFileChange = (event) =&amp;gt; {
    setFile(event.target.files[0]);
  };

  const handleUpload = async () =&amp;gt; {
    const formData = new FormData();
    formData.append('image', file);

    try {
      const response = await axios.post('/api/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      });
      setImageUrl(response.data.image_url);
      console.log('File uploaded successfully!');
    } catch (error) {
      console.error('Error uploading file:', error);
    }
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;input type="file" onChange={handleFileChange} /&amp;gt;
      &amp;lt;button onClick={handleUpload}&amp;gt;Upload&amp;lt;/button&amp;gt;
      {imageUrl &amp;amp;&amp;amp; &amp;lt;img src={imageUrl} alt="Uploaded" /&amp;gt;}
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;Conclusion:&lt;/p&gt;

&lt;p&gt;In this guide, we've covered the integration of Active Storage in Ruby on Rails with a React front end. From setting up Active Storage to handling file uploads and rendering images in a React application, you now have a solid foundation for managing file storage in your web projects.&lt;/p&gt;

&lt;p&gt;By combining the strengths of Ruby on Rails and React, you can create a seamless user experience with efficient file management. As you continue to explore and refine your skills in these technologies, you'll be well-equipped to build robust and user-friendly applications. Happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Using Rails serializers to render data through a has_many relationship.</title>
      <dc:creator>Joshua Shane Doud Jr.</dc:creator>
      <pubDate>Tue, 04 Jul 2023 18:12:05 +0000</pubDate>
      <link>https://forem.com/jdoud1993/using-rails-serializers-to-render-data-through-a-hasmany-relationship-24gn</link>
      <guid>https://forem.com/jdoud1993/using-rails-serializers-to-render-data-through-a-hasmany-relationship-24gn</guid>
      <description>&lt;p&gt;Have you ever had the need to not only fetch a single models data but the data included in that models relationships with other models? &lt;/p&gt;

&lt;p&gt;Using Rails as a backend API server we can hone in on the data we want to render and send to our front end application. Say we have the following relationships:&lt;br&gt;
&lt;strong&gt;USER:&lt;/strong&gt;&lt;/p&gt;

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

  has_many :comments
    has_many :posted_pets, :class_name =&amp;gt; "Pet", :foreign_key =&amp;gt; "user_id"
    has_many :pets, through: :comments



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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;PET:&lt;/strong&gt;&lt;/p&gt;

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

  belongs_to :user
    has_many :comments, dependent: :delete_all
    has_many :users, through: :comments


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;COMMENTS:&lt;/strong&gt;&lt;/p&gt;

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

    belongs_to :user
    belongs_to :pet


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

&lt;/div&gt;

&lt;p&gt;These relationships establish that a user has many posted pets using an alias and has many comments that belong to a pet. &lt;/p&gt;

&lt;p&gt;Our need at the front end side of the application is that we want to display a pet with their list of comments and an individual comment will display the comments users username like below:&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%2Fj9vn3krgj0alwaaihz4e.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%2Fj9vn3krgj0alwaaihz4e.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our first task is to render the pet model data, but we want to establish what we want to render to the front end of the application. To do so we use a serializer. To create a serializer using Rails we need to include the following in our terminal &lt;code&gt;rails g serializer pet&lt;/code&gt;. You will notice that the name of the serializer "pet" is singular. This follows the proper naming convention to ensure this serializer is used as the default serializer for the "Pet" model. Lets take a look at the now generated serializer for "pet".&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%2Frdbpdnlo06h7j1nep7q5.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%2Frdbpdnlo06h7j1nep7q5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The serializer allows us to control the attributes that will be rendered to the front end. Say we have a found address attribute that we do not want to display in the front end of the application. We can exclude this attribute in the serializer to keep the data from being rendered. &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%2Fr71azolvryfsp4rwh7ka.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%2Fr71azolvryfsp4rwh7ka.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now this serializer is being used when we fetch our Pet model data. Here is a glimpse of the controller rendering the data to the front end application. &lt;/p&gt;

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

   def show
        pet = Pet.find_by(id: params[:id])
        if pet
            render json: pet,
        else
            render json: {error: "Pet not found"}, status: :not_found 
        end 
    end


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

&lt;/div&gt;

&lt;p&gt;We however are not done. Looking back at our front end image we can see that we require the comments data that is associated with the pet. We already established our relationships in our models but now we can utilize our serializer to include the associated data when we render. 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%2Fmgvc3yrbjgxx0ocde3gc.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%2Fmgvc3yrbjgxx0ocde3gc.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we get the pet data and all the comment data associated with the pet. We can stop here but lets say we want to also control the comment data we render to the front end. To do so we create another serializer for the comment model using our &lt;code&gt;rails g serializer comment&lt;/code&gt;. Lets see how this looks below:&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%2Fqfovdc4a6621ri41fvr2.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%2Fqfovdc4a6621ri41fvr2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Utilizing this serializer we are only included each comments ":id" and ":body" when rendering the data. No additional syntax is needed in our pet controller:&lt;/p&gt;

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

   def show
        pet = Pet.find_by(id: params[:id])
        if pet
            render json: pet,
        else
            render json: {error: "Pet not found"}, status: :not_found 
        end 
    end


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

&lt;/div&gt;

&lt;p&gt;We have our pet data and our comment data, but now we need the user that posted the comment's username. To do this we have to include more syntax in our controller because AMS or active model serializer is not able to render deeply nested data. &lt;/p&gt;

&lt;p&gt;A simple fix is to "include:" the data in our pet controller like so:&lt;/p&gt;

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


    def show
        pet = Pet.find_by(id: params[:id])
        if pet
            render json: pet, include: ['comments', 'comments.user']
        else
            render json: {error: "Pet not found"}, status: :not_found 
        end 
    end


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

&lt;/div&gt;

&lt;p&gt;Establishing the relationships and utilizing AMS we can finally render the following data to our front end:&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%2Flxozig5uz3euiba6xkn2.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%2Flxozig5uz3euiba6xkn2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What an amazing functionality! I hope this helped you learn more about AMS.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Sinatra/Active Record Full CRUD</title>
      <dc:creator>Joshua Shane Doud Jr.</dc:creator>
      <pubDate>Fri, 24 Mar 2023 23:40:28 +0000</pubDate>
      <link>https://forem.com/jdoud1993/sinatraactive-record-full-crud-53nc</link>
      <guid>https://forem.com/jdoud1993/sinatraactive-record-full-crud-53nc</guid>
      <description>&lt;p&gt;Welcome and get ready to link your front-end to your back end using Sinatra with Active Record. In this blog I will be showing you how to use Sinatra and Active Record to access your Ruby based models and present this data to your front-end React application. To get started lets take a look at a simple GET request from our front-end server below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  fetch("http://localhost:9292/animals")
  .then(res =&amp;gt; res.json())
  .then(data =&amp;gt; {
    setAnimals(data);
  },)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this bit of code shows that we are asking our server for data using a GET request. After this data is received we are turning that data which is JSON into data that can be used by our front-end and then updating our state to that data.&lt;/p&gt;

&lt;p&gt;How does this look on the back-end? How is this request being processed after it is sent to the server? Lets take a look at the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApplicationController &amp;lt; Sinatra::Base

  get '/games' do

  end

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

&lt;/div&gt;



&lt;p&gt;This code above is the beginning of the server's job to receive the GET request. Now we must obtain all of the data needed by the request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; get '/animals' do 
    animals = Animal.all 

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

&lt;/div&gt;



&lt;p&gt;This is where Active Record comes in handy. Using the method .all we can obtain all of the animals from the database. We are not done yet though we need to convert these animals to JSON. Luckily Active Record has a method for this as well &lt;code&gt;.to_json&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; get '/animals' do 
    animals = Animal.all 
    animals.to_json
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yay! Now our React application can make a GET request for the "/animals" database; but wait we also want to set the content type headers for all of the responses. We do this by adding the following line: &lt;code&gt;set :default_content_type, 'application/json'&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApplicationController &amp;lt; Sinatra::Base
  set :default_content_type, 'application/json'

  get '/animals' do 
    animals = Animal.all 
    animals.to_json(include: :shelter) 
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our ApplicationController inherits from the Sinatra::Base class and can send a response to a GET request with a default json content type for the headers.&lt;/p&gt;

&lt;p&gt;We can modify this request using Active Record methods to control the data the server sends back as a response to the request. &lt;/p&gt;

&lt;p&gt;We can only send the first 10 entries in our database and we can make sure to include the shelter that the animal has a relationship with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  get '/animals' do 
    animals = Animal.all.limit(10) 
    animals.to_json(include: :shelter) 
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Methods like this help us as developers to use our back-end to control server responses.&lt;/p&gt;

&lt;p&gt;So we have successfully made a GET request and set up our server's response, but our application is going to require more than just readable data from our database. We would also like to create, update, and remove from our database. These options are known to developers as CRUD (create, read, update, delete) or our POST, GET, PATCH, and DELETE requests.&lt;/p&gt;

&lt;p&gt;Lets start with our POST request using similar looking code. Here is our request from our React application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch("http://localhost:9292/animals", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(formData)
        })
        .then(res =&amp;gt; res.json())
        .then(newAnimal =&amp;gt; onAddAnimal(newAnimal))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The beginning of our code is going to start off with similar syntax to our GET response but will be &lt;code&gt;post&lt;/code&gt; instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApplicationController &amp;lt; Sinatra::Base
  set :default_content_type, 'application/json'

 post '/animals' do


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

&lt;/div&gt;



&lt;p&gt;Unlike our GET response we need to use an Active Record method &lt;code&gt;.create&lt;/code&gt; to create an animal using the body of the request sent by the react application and add it to the database sending a response from the server with the added animal and its new id generated by the database. How does this look?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; post '/animals' do
    animal = Animal.create(
      name: params[:name],
      species: params[:species],
      breed: params[:breed],
      sex: params[:sex],
      age: params[:age],
      image: params[:image],
      shelter_id: params[:shelter_id]
    )
    animal.to_json
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We added to our database!&lt;br&gt;
This allows our react application to make a request sending over data to the server. The server then takes this data and using Sinatra and the params hash creates a new animal. Our create part of CRUD is complete. Our next two requests are going to be our update or PATCH request and our DELETE request. Here is the final result of all four requests in our controller file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApplicationController &amp;lt; Sinatra::Base
  set :default_content_type, 'application/json'


  # Animal Controllers Full CRUD
  get '/animals' do 
    animals = Animal.all 
    animals.to_json(include: :shelter) 
  end

  get '/animals/:id' do
    animal = Animal.find(params[:id])
    animal.to_json(include: :shelter)
  end

  delete '/animals/:id' do
    animal = Animal.find(params[:id])
    animal.destroy
    animal.to_json
  end

  post '/animals' do
    animal = Animal.create(
      name: params[:name],
      species: params[:species],
      breed: params[:breed],
      sex: params[:sex],
      age: params[:age],
      image: params[:image],
      shelter_id: params[:shelter_id]
    )
    animal.to_json
  end

  patch '/animals/:id' do
    animal = Animal.find(params[:id])
    animal.update(
      name: params[:name],
      species: params[:species],
      breed: params[:breed],
      sex: params[:sex],
      age: params[:age],
      image: params[:image],
      shelter_id: params[:shelter_id]
    )
    animal.to_json
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you for going on this CRUD journey with me. Tune in next time!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>ruby</category>
      <category>sinatra</category>
      <category>activerecord</category>
    </item>
    <item>
      <title>React Application vs Single Page HTML</title>
      <dc:creator>Joshua Shane Doud Jr.</dc:creator>
      <pubDate>Fri, 27 Jan 2023 03:01:54 +0000</pubDate>
      <link>https://forem.com/jdoud1993/react-application-vs-single-page-html-34ph</link>
      <guid>https://forem.com/jdoud1993/react-application-vs-single-page-html-34ph</guid>
      <description>&lt;p&gt;What a transformation you go through when you move on from the basic HTML and CSS structure. You get to open up that door into the magical realm of React and JSX which seems to just make everything more "crisp". In this post I want to share with you my experience and what a difference React and JSX made for me. &lt;/p&gt;

&lt;p&gt;To start off the organization that you get to have when using React components just makes my OCD skyrocket to the satisfactory level. To give an example having the ability to organize your application into different JS files that individually render JSX to the DOM is amazing. Below you will find a screenshot of a mock challenge that I will be using in this blog to talk about my experience and how much using React differs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6ezglqzc9m5hcyvhlnzh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6ezglqzc9m5hcyvhlnzh.png" alt="Image description" width="800" height="860"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just look at that organization. You have your main 'App.JS" which is parent to your "Header.JS", "ListingCard.JS", and "Search.JS. Then your "ListingContainer.JS" is parent to your "ListingCard.JS". This allows you to group what you render to the DOM and when you want to edit any of the code you do not have to sift through lines of code to find what you need. I am of course talking about having everything rendered within your HTML file and having to group lines of HTML elements using Divs with IDs and Class Names. This is a thing of the past using React and components. &lt;/p&gt;

&lt;p&gt;I am a stickler when it comes to organization but that's not even the best part! With React over simple HTML you can use this amazing function called useState. First let me give you an example using my mock challenge code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, {useState, useEffect} from "react";
import Header from "./Header";
import ListingsContainer from "./ListingsContainer";

function App() {

  const [listings, setListings] = useState([]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you look closely in order to use "useState" you must import it from react using the top line of code. After this you can create a variable using an array. The first input inside the array is going to be your variable's name and how you reference the variable. The next input is going to be how you set the value of the variable. More on this later. After the array you then set the initial value of the variable using useState() like so: &lt;code&gt;const [listings, setListings] = useState([]);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;"listings" can now be used just like any normal variable and so in this example I was able to make my fetch request and set the value of listings to the data I received from the server. See below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const [listings, setListings] = useState([]);

  useEffect(() =&amp;gt; {
    fetch(" http://localhost:6001/listings")
      .then((res) =&amp;gt; res.json())
      .then((data) =&amp;gt; setListings(data))
  }, [])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the value has been basically updated I can continue to update that value using setListings() and with a bit of destructuring I can retain the previous value and add to it, remove from it, or update it. Following up with an example please look at how this function filters the array of data and then updates the state to a new array. This exact snippet of code is used to delete a listing within the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function handleDeleteListing (deletedListing) {
    const updatedListings = listings.filter((listing) =&amp;gt; listing.id !== deletedListing.id)
    setListings(updatedListings)
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What an amazing feature! Without giving you more examples I do want to mention how this state that we can create has so many applications. One of which is creating a controlled "Form" but this subject deserves a post on its own. I encourage you though that if you are interested please look forward to another post on controlled forms in the near future.&lt;/p&gt;

&lt;p&gt;Now that I have explained a bit about "useState" lets just recap on how it makes life so much easier compared to simple HTML. With simple HTML you would have needed to use JavScript to grab pieces of HTML and fetch the data and then use the data to create more HTML elements updating the elements rendering them to the element you already grabbed. Oh how I do not miss any of that.&lt;/p&gt;

&lt;p&gt;Now let me explain the wonders of "useEffect". This feature acts as a function and allows you to set a time or a que as to when the function runs. Say for example you are making a fetch request and you want this to run when on the initial load of your DOM. Yoiur code would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  useEffect(() =&amp;gt; {
    fetch(" http://localhost:6001/listings")
      .then((res) =&amp;gt; res.json())
      .then((data) =&amp;gt; setListings(data))
  }, [])

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

&lt;/div&gt;



&lt;p&gt;The array after the function is your ability to set either how often you would like this function to run or if you leave this blank it will run one time on the initial load of your Dom.&lt;/p&gt;

&lt;p&gt;Compared to simple HTML and JS this makes fetching data from a server so much easier. Instead of having to run the code after setting up a listener for DOMfullyload you just simply "useEffect".&lt;/p&gt;

&lt;p&gt;This is my take on some of the pros to React over simple HTML and JS. It takes some time to learn but you will be happy you took that step forward. Thank you for the consideration and tuning in to my blog. Ill catch you on the next one!&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>development</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>My JavaScript Pseudocode steps.</title>
      <dc:creator>Joshua Shane Doud Jr.</dc:creator>
      <pubDate>Sat, 26 Nov 2022 18:56:04 +0000</pubDate>
      <link>https://forem.com/jdoud1993/my-javascript-pseudocode-steps-105h</link>
      <guid>https://forem.com/jdoud1993/my-javascript-pseudocode-steps-105h</guid>
      <description>&lt;p&gt;While I am just starting out as a web developer, I believe that the overall ability to problem solve and view a problem from different angles will help you succeed in a development position. When you are confronted with a particular problem it really helps to simplify the problem and create a task list for yourself. This is where my Pseudocode comes in handy for myself. Lets break it down into a few steps to make it easier to understand.&lt;/p&gt;

&lt;p&gt;First lets start with a problem that needs solving. I have a web application that I want to take information from a JSON server and distribute this information into a card format for each item within the server. I want to do this when the DOM loads. &lt;/p&gt;

&lt;p&gt;Now that is a very specific problem that needs solving but separating this paragraph into Pseudocode steps is going to help with several issues that you may run into. First where do I begin? Well we can start with the "when" we want this code to run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Add an event listener for when the DOM is loaded
document.addEventListener("DOMContentLoaded", () =&amp;gt; {
getAllNinjas()
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is step one and very important because it is going to lead us in the right direction. With the event listener being added to &lt;code&gt;"DOMContentLoaded"&lt;/code&gt; we know that this is going to use a callback function. Here is our next step! We are also going to use pseudocode to break down our fetch function as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create a callback function that "gets" the needed information
function getAllNinjas() {
    // "GET" fetch from JSON server
    fetch("http://localhost:3000/ninjas")
    // Convert result to "json"
    .then(res =&amp;gt; res.json())
    // take json data and send to callback function to render a card for each item within the data object
    .then(ninjaData =&amp;gt; ninjaData.forEach(ninja =&amp;gt; renderOneNinja(ninja)))
}

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

&lt;/div&gt;



&lt;p&gt;So after pseudo coding and coding out the next step you can see how complex the problem is beginning to get, but how basic and simple the steps that we are creating. These basic steps help keep focus and guide yourself through the process of creating working code.&lt;/p&gt;

&lt;p&gt;So lets just look at our overall steps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Add an event listener for when the DOM is loaded
// Create a callback function that "gets" the needed information
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An look how easy figuring out the next step is going to be! We need to create another callback function that handles the data that we just fetched from our JSON server. Within the creation of our function it is important again that we use our pseudocode to our advantage to create steps in creating the "handle" function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create a callback function that "handles" the fetch data and renders each item to the DOM or a "DOM manipulation function

function renderOneNinja(ninja) {
    // Create an container element to hold our card
    const ninjaCard = document.createElement("div")
    // Add a class name to each card
    ninjaCard.classList.add("ninja-card")
    // Add an ID to the element that includes the `dataitem.name`
    ninjaCard.id = `${ninja.name}`
    // Format the elements innerHTML using iteration through the item within the data object
    ninjaCard.innerHTML =`
            &amp;lt;div class="front"&amp;gt;
                &amp;lt;img class="img" src="${ninja.imageUrl}"&amp;gt;
                &amp;lt;p class="name"&amp;gt;Name:${ninja.name}&amp;lt;/p&amp;gt;
                &amp;lt;p class="village"&amp;gt;Village:${ninja.village}&amp;lt;/p&amp;gt;
                &amp;lt;p class="rank"&amp;gt;Rank:${ninja.rank}&amp;lt;/p&amp;gt;
                &amp;lt;p class="power" style="display:none;"&amp;gt;${ninja.power}&amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="back"&amp;gt;
                &amp;lt;button id=one class=addBtn&amp;gt;Add to Squad 1&amp;lt;/button&amp;gt;
                &amp;lt;button id=two class=addBtn&amp;gt;Add to Squad 2&amp;lt;/button&amp;gt;
            &amp;lt;/div&amp;gt;
    `
    // Add event listeners for the two buttons and the mouseenter and mouseleave (these event listeners were added on at a later time due to a different desired outcome or a different "PROBLEM".
    ninjaCard.querySelector("#one").addEventListener("click", handleClickOne)
    ninjaCard.querySelector("#two").addEventListener("click", handleClickTwo)
    ninjaCard.addEventListener("mouseenter", handleMouse)
    ninjaCard.addEventListener("mouseleave", handleMouse)

    // Append the element to a container already on the DOM
    document.querySelector("#ninjaContainer").appendChild(ninjaCard)
}

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

&lt;/div&gt;



&lt;p&gt;Wow! That was a lot to go through for only one function, but leaving out our pseudocode for the functions creation lets see how our task list looks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Add an event listener for when the DOM is loaded
// Create a callback function that "gets" the needed information
// Create a callback function that "handles" the fetch data and renders each item to the DOM or a "DOM manipulation function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whoa! Only three steps top solve our problem; however, within each steps there are a series of "children steps" that allow a developer to simplify the road to a desired result.&lt;br&gt;
This was a large problem that really needed to be simplified to get yourself pointed in the right direction, but lets just take a look at a more simplified problem that we can still use pseudocode for.&lt;/p&gt;

&lt;p&gt;So here is our problem:&lt;/p&gt;

&lt;p&gt;Create a function that when given an array with three element objects that contain two items, returns an array that combines the value of power to one value.&lt;/p&gt;

&lt;p&gt;This is a more simple problem but look at how confusing it can get. So lets pseudocode this problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create function "reducePower" that takes in one argument "array of three element objects"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This handles a couple of simple tasks like the name of the function and what we are going to be using as an argument. The problem can be referenced in writing this pseudocode. Our name is describing what our desired result will be and the problem gives us the argument we need to use. Now we need to write out what to do with the argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Iterate through each object and find the power value
// Create a variable to hold each power value ([])
// Push each power value to the variable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to obtain the power value for each object and combine them into an array. Why do we need to combine them into an array? Lets see why.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Use the reduce method on the array of power values which will return the total of the values.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We just pseudo coded our problem out and quickly and effectively came up with a task list to use as a guide when creating our code. Now we are thinking like problem solvers. I promise this will help any developer new or veteran. With that I bid you good day and hope this has helped with your development journey!&lt;/p&gt;

</description>
      <category>career</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Code Organization</title>
      <dc:creator>Joshua Shane Doud Jr.</dc:creator>
      <pubDate>Sat, 05 Nov 2022 18:21:45 +0000</pubDate>
      <link>https://forem.com/jdoud1993/code-organization-5gd6</link>
      <guid>https://forem.com/jdoud1993/code-organization-5gd6</guid>
      <description>&lt;p&gt;Hello eager developers. I want to take some time to talk about Code organization and how a well placed comment that describes your functions can really make a difference in readability of your code.&lt;/p&gt;

&lt;p&gt;Take the example below for instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchUser(userName) {
    fetch(`https://api.github.com/users/${userName}`, {
        method: "GET",
        header: {
            "Content-Type": "Application/json",
            Accept: "application/vnd.github.v3+json"
        }
    })
    .then(resp =&amp;gt; resp.json())
    .then(data =&amp;gt; addUser(data)) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function serves a primary purpose. Ask yourself what that purpose is and how can you organize this function with other functions? Well lets take a look at another function who's primary purpose is similar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchRepos(userName) {
    fetch(`https://api.github.com/users/${userName}/repos`)
    .then(resp =&amp;gt; resp.json())
    .then(data =&amp;gt; {
        console.log(data);
        addRepos(data)
    })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To put these functions into perspective the first function is taking in a &lt;code&gt;userName&lt;/code&gt; and fetching a &lt;code&gt;json&lt;/code&gt; object that matches that &lt;code&gt;userName&lt;/code&gt;. The second function is doing the same but returning an object of repositories for the &lt;code&gt;userName&lt;/code&gt; provided. This &lt;code&gt;userName&lt;/code&gt; is provided by a &lt;code&gt;&amp;lt;button&amp;gt;&amp;lt;/button&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that we understand the functions a little better, how can we organize them together? Well their functions are both fetching data from an API. So lets group them with a comment "API fetch functions":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// API fetch functions

function fetchUser(userName) {
    fetch(`https://api.github.com/users/${userName}`, {
        method: "GET",
        header: {
            "Content-Type": "Application/json",
            Accept: "application/vnd.github.v3+json"
        }
    })
    .then(resp =&amp;gt; resp.json())
    .then(data =&amp;gt; addUser(data)) 
}

function fetchRepos(userName) {
    fetch(`https://api.github.com/users/${userName}/repos`)
    .then(resp =&amp;gt; resp.json())
    .then(data =&amp;gt; {
        console.log(data);
        addRepos(data)
    })
}

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

&lt;/div&gt;



&lt;p&gt;Now when I come back to this &lt;code&gt;js&lt;/code&gt; file I can clearly see that these two functions are fetching data from the API and then sending that data to go do something. Instead of taking that data and doing something with it within the fetch functions it is better to send it off to another function that is organized under a different comment. See below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Dom Manipulation Functions

function addUser (userObj) {
    let userCard = document.createElement("li")
    let userContainer = document.querySelector("#user-list")
    userCard.id = userObj.login
    userCard.innerHTML = `
        &amp;lt;img src="${userObj.avatar_url}"&amp;gt;
        &amp;lt;h4&amp;gt;UserName : ${userObj.login}&amp;lt;/h4&amp;gt;
        &amp;lt;h5&amp;gt;Profile Link: ${userObj.html_url}&amp;lt;/h5&amp;gt;
        &amp;lt;button class="${userObj.login}"&amp;gt;Obtain Repos&amp;lt;/button&amp;gt;
    `
    userContainer.appendChild(userCard)
    let userBtn = document.querySelector(`.${userObj.login}`)
    userBtn.addEventListener("click", handleBtn)
}

function addRepos(repoObj) {
    let repoArr=[...repoObj]
    repoArr.forEach((repo) =&amp;gt; {
        let repoList = document.querySelector("#repos-list")
        let repoName = document.createElement("li")
        repoName.innerHTML = `${repo.name}`
        repoList.appendChild(repoName)
    })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This separation of responsibility for the functions allows us as developers to better describe a functions purpose. This way when an issue arises we can go back to our code and have an easier time pinpointing where that issue is coming from. This also helps with testing. If you know exactly which function is causing an issue a few well placed &lt;code&gt;console.log()&lt;/code&gt; can really help solve the problem and verify that your return is what you think it will be.&lt;/p&gt;

&lt;p&gt;And here is the full &lt;code&gt;js&lt;/code&gt; file so you can see how organization really helps with readability.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener("DOMContentLoaded", () =&amp;gt; {

document.querySelector("#github-form").addEventListener("submit", handleSubmit)


})

// Handle Event Functions
function handleSubmit(event) {
    event.preventDefault()
    let githubUserName = event.target.search.value
    fetchUser(githubUserName)
}

function handleBtn(event) {
    let btnClass = event.target.classList.value
    fetchRepos(btnClass)
}

// API fetch functions

function fetchUser(userName) {
    fetch(`https://api.github.com/users/${userName}`, {
        method: "GET",
        header: {
            "Content-Type": "Application/json",
            Accept: "application/vnd.github.v3+json"
        }
    })
    .then(resp =&amp;gt; resp.json())
    .then(data =&amp;gt; addUser(data)) 
}

function fetchRepos(userName) {
    fetch(`https://api.github.com/users/${userName}/repos`)
    .then(resp =&amp;gt; resp.json())
    .then(data =&amp;gt; {
        console.log(data);
        addRepos(data)
    })
}

// Dom Manipulation Functions

function addUser (userObj) {
    let userCard = document.createElement("li")
    let userContainer = document.querySelector("#user-list")
    userCard.id = userObj.login
    userCard.innerHTML = `
        &amp;lt;img src="${userObj.avatar_url}"&amp;gt;
        &amp;lt;h4&amp;gt;UserName : ${userObj.login}&amp;lt;/h4&amp;gt;
        &amp;lt;h5&amp;gt;Profile Link: ${userObj.html_url}&amp;lt;/h5&amp;gt;
        &amp;lt;button class="${userObj.login}"&amp;gt;Obtain Repos&amp;lt;/button&amp;gt;
    `
    userContainer.appendChild(userCard)
    let userBtn = document.querySelector(`.${userObj.login}`)
    userBtn.addEventListener("click", handleBtn)
}

function addRepos(repoObj) {
    let repoArr=[...repoObj]
    repoArr.forEach((repo) =&amp;gt; {
        let repoList = document.querySelector("#repos-list")
        let repoName = document.createElement("li")
        repoName.innerHTML = `${repo.name}`
        repoList.appendChild(repoName)
    })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you! Have a great day!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>My journey using .find() to return keys and values in an array!</title>
      <dc:creator>Joshua Shane Doud Jr.</dc:creator>
      <pubDate>Thu, 20 Oct 2022 18:11:56 +0000</pubDate>
      <link>https://forem.com/jdoud1993/my-journey-using-find-to-return-keys-and-values-in-an-array-5kk</link>
      <guid>https://forem.com/jdoud1993/my-journey-using-find-to-return-keys-and-values-in-an-array-5kk</guid>
      <description>&lt;p&gt;Below you will find my final product, but after that I will take you through my failed attempts from which I was able to learn from to get to this final product. After this I will take you even further and "clean up" my final code block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const record = [
    { year: "2015", result: "W"},
    { year: "2014", result: "N/A"},
    { year: "2013", result: "L"},
    //...
  ]

  function superbowlWin(array) {
    let win = array.find(isWin)
    if (win) {
      return win.year
    }
    return win
  }

  function isWin(score){
      return score.result === "W"

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

&lt;/div&gt;



&lt;p&gt;With the above code block in mind and our variable "record" I was tasked with creating a function that would loop through the objects in the array and return the year value when the result value (===) "W" or a win. I started out with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; function superbowlWin(array) {
   return array.find(isWin).year

  function isWin(score){
      return score.result === "W"

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

&lt;/div&gt;



&lt;p&gt;Now this was ok and it gives me the first year that produced a winning result; however, it did not give me a way to find out if there were no winning results within my array. Attempting to change the code to do so I ended up with a couple of failing functions like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function superbowlWin(array) {
   return array.find(isWin).year;
       else return undefined


  function isWin(score){
      return score.result === "W"

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

&lt;/div&gt;



&lt;p&gt;Now this and others did not work. I then realized that instead of trying to return the functions return directly I can place the function inside a variable and then use that variable to write an if....else statement to compare the results to what I would be looking for. This meant that I could take the result of the .find() function and compare it to being true or false. If true it would return the variable.year (win). This returns 2015 which is a winning year. Now the else statment returns the variable (win) if the function turns out to be false. This returns undefined due to the .find() functions default. Please see below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const record = [
    { year: "2015", result: "W"},
    { year: "2014", result: "N/A"},
    { year: "2013", result: "L"},
    //...
  ]

  function superbowlWin(array) {
    let win = array.find(isWin)
    if (win) {
      return win.year
    }
    return win
  }

  function isWin(score){
      return score.result === "W"

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

&lt;/div&gt;



&lt;p&gt;If we want to take this a step further we can clean the code block and turn it into an anonymous function within our main function and write it as an arrow function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function superbowlWin(array){
        let win = array.find(array =&amp;gt; array.result === "W")
        if (win){
            return win.year;
        } else return win;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it our final function which uses the .find() function to take in a callback function (arrow function) to loop through the array and return the winning year! Although you probably read this in a minute or two let me tell you the journey was a lot longer. Have a great day and look for more little bits of my code learning journey.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Starting out learning to code with flatiron online coding boot-camp.</title>
      <dc:creator>Joshua Shane Doud Jr.</dc:creator>
      <pubDate>Mon, 17 Oct 2022 17:59:48 +0000</pubDate>
      <link>https://forem.com/jdoud1993/starting-out-learning-to-code-with-flatiron-online-coding-boot-camp-1ndg</link>
      <guid>https://forem.com/jdoud1993/starting-out-learning-to-code-with-flatiron-online-coding-boot-camp-1ndg</guid>
      <description>&lt;p&gt;On October 10th, 2022 I started the flex-software-engineer program at flatiron school. Wow! I just want to say how amazing the present can be. One day I woke up and asked myself if what I was doing was worth it? Was I making a difference and enjoying my craft? As fast as I could make the decision to switch career paths I was on my way to learning to code. &lt;/p&gt;

&lt;p&gt;After researching several online coding boot-camps that could work around my current career I finally chose flatiron school. This was the best choice I have made, and although I am only a week into the program, I can say that this school sets you up for success. You immediately have access to a pre-work program that sets you up with a solid foundation. &lt;/p&gt;

&lt;p&gt;During the pre-work I learned the basis of HTML, JavaScript, and CSS. You are also given a step by step process to make sure the device you are using is ready for the program by installing Visual Studios to helping you first discover how to use GitHub. I am well on my way and will be posting about my experience as often as I can.&lt;/p&gt;

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