<?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: tewkesburyd</title>
    <description>The latest articles on DEV Community by tewkesburyd (@tewkesburyd).</description>
    <link>https://dev.to/tewkesburyd</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%2F973795%2Ffa350e8e-ab7a-4afc-ae68-57a0ade9b8c7.png</url>
      <title>DEV Community: tewkesburyd</title>
      <link>https://dev.to/tewkesburyd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tewkesburyd"/>
    <language>en</language>
    <item>
      <title>Ruby, Generating Seeds with Excel</title>
      <dc:creator>tewkesburyd</dc:creator>
      <pubDate>Tue, 24 Jan 2023 19:14:23 +0000</pubDate>
      <link>https://dev.to/tewkesburyd/ruby-generating-seeds-with-excel-eln</link>
      <guid>https://dev.to/tewkesburyd/ruby-generating-seeds-with-excel-eln</guid>
      <description>&lt;p&gt;While working on the backend of a e-commerce clone application, my group had all of the product data stored in a excel file. It seemed tedious to go through and create the products again in the seeds.rb. There had to be a simpler way, and there is. This was our process. Yours may be different based on the version of Ruby you have, but the core concepts should be the same.&lt;/p&gt;

&lt;p&gt;To start, make a new csv directory within the db directory.&lt;/p&gt;

&lt;p&gt;Next, we need to make sure that the spreadsheet is formatted properly. The data needs to be stored in an actual table. &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%2Fisx0ltktzenz60fxdqzy.jpg" 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%2Fisx0ltktzenz60fxdqzy.jpg" alt="Excel" width="800" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If your table has headers, make sure that the "My table has headers" box is checked. Ours included headers.&lt;/p&gt;

&lt;p&gt;From here, save your file as CSV format to the csv directory you created earlier. Its important to note that each table needs to be its own file. You can not have multiple sheets tabs. So, if you have multiple tables, they need to be saved as individual csv files.&lt;/p&gt;

&lt;p&gt;If you haven't generated your tables or models, now is a good time. Make sure your columns names match up with your csv columns names. If not, you will end up with your data in the wrong columns.  &lt;/p&gt;

&lt;p&gt;Now we can start coding in the seeds.rb file.&lt;/p&gt;

&lt;p&gt;At the top of the file we need to include:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;This provides the built in CSV class methods.&lt;/p&gt;




&lt;p&gt;Next, we need to tell Ruby to read the content of the file and set that to a variable we can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;csv_text = File.read('./db/csv/Products.csv')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Now, we need to parse the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;csv_text = File.read('./db/csv/Products.csv')
csv = CSV.parse(csv_text, 
    :headers =&amp;gt; true, 
    :header_converters =&amp;gt; :symbol
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we included the read file variable we just set, set headers to true, and converted the headers to symbols.&lt;/p&gt;




&lt;p&gt;The next step is to iterate through the data and create a new product for each row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;csv_text = File.read('./db/csv/Products.csv')
csv = CSV.parse(csv_text, 
    :headers =&amp;gt; true, 
    :header_converters =&amp;gt; :symbol
)
csv.each do |row|
    Product.create!(row.to_hash)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we used .each as the iterator and told Ruby that for each row we wanted to call Product.create. We also included the bang operator "!" to raise an exception if the data does not match the data types that were defined in the Product table. Finally, we called the to_hash method on each row, to ensure that each row is returned as a hash.&lt;/p&gt;

&lt;p&gt;You can repeat this process for each of your csv files.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>React Controlled Form Fetch Post, With Multiple Inputs</title>
      <dc:creator>tewkesburyd</dc:creator>
      <pubDate>Sun, 11 Dec 2022 23:02:30 +0000</pubDate>
      <link>https://dev.to/tewkesburyd/react-controlled-form-fetch-post-with-multiple-inputs-23h8</link>
      <guid>https://dev.to/tewkesburyd/react-controlled-form-fetch-post-with-multiple-inputs-23h8</guid>
      <description>&lt;p&gt;We will be using state to make a controlled form React. The form wants to get the first name, age, and location of a user. The first thing we need to do is import state to the component and set up 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;import React, {useState} from "react";

function form (){
   const [formValues, setFormValues] = useState({
                                        firstName: "",
                                        age: 0,
                                        location: ""
                                        })

   return(
    &amp;lt;form&amp;gt;
      &amp;lt;input name="firstName" /&amp;gt;
      &amp;lt;input name="age" /&amp;gt;
      &amp;lt;input name="location" /&amp;gt;
      &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
   )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the code above, we will be collection all he input in one state. Now that everything is set up we can move forward. We need to write a function that can handle changes to the form inputs and set the values in the state.&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";

function form (){
   const [formValues, setFormValues] = useState({
                                        firstName: "",
                                        age: 0,
                                        location: ""
                                        })

   const handleChange = (e) =&amp;gt; {
       setFormValues({...formValues, [e.target.name]:e.target.value})
   }

   return(
    &amp;lt;form&amp;gt;
      &amp;lt;input 
         name="firstName" 
         value={formValues.firstName} 
         onChange={handleChange} 
      /&amp;gt;
      &amp;lt;input 
         name="age" 
         value={formValues.age} 
         onChange={handleChange}
      /&amp;gt;
      &amp;lt;input 
         name="location" 
         value={formValues.location} 
         onChange={handleChange}
      /&amp;gt;
      &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
   )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, you can see that the handleChange function uses the spread operator on the initial state structure. It then uses [e.target.name], by placing this in square brackets, the e.target.name becomes more flexible. This will then associate name value that is respective input. The handleChange also sets the state to an object withe the respective key:value pairs. &lt;/p&gt;

&lt;p&gt;The next step is to submit the form and post it to the database.&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";

function form (){
   const [formValues, setFormValues] = useState({
                                        firstName: "",
                                        age: 0,
                                        location: ""
                                        })

   const handleChange = (e) =&amp;gt; {
       setFormValues({...formValues, [e.target.name]:             e.target.value})
   }

   const handleSubmit = (e) =&amp;gt; {
       e.preventDefault()
       fetch('SOME API HERE', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify(formValues)
        })
   }

   return(
    &amp;lt;form onSubmit={handleSubmit} &amp;gt;
      &amp;lt;input 
         name="firstName" 
         value={formValues.firstName} 
         onChange={handleChange} 
      /&amp;gt;
      &amp;lt;input 
         name="age" 
         value={formValues.age} 
         onChange={handleChange}
      /&amp;gt;
      &amp;lt;input 
         name="location" 
         value={formValues.location} 
         onChange={handleChange}
      /&amp;gt;
      &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
   )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>php</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Array Has Target Sum</title>
      <dc:creator>tewkesburyd</dc:creator>
      <pubDate>Fri, 18 Nov 2022 17:39:01 +0000</pubDate>
      <link>https://dev.to/tewkesburyd/array-has-target-sum-1c58</link>
      <guid>https://dev.to/tewkesburyd/array-has-target-sum-1c58</guid>
      <description>&lt;p&gt;Here is a fun challenge to practice nested looping.&lt;/p&gt;

&lt;p&gt;We are going to write a function that takes two parameter, an array of numbers, and a target number.&lt;/p&gt;

&lt;p&gt;The goal of the function is to determine if the array contains two numbers, when added together, equal the target number.&lt;/p&gt;

&lt;p&gt;Lets start:&lt;/p&gt;

&lt;p&gt;Let's first declare a function and set the parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const hasTargetSum = (array, target) =&amp;gt; {...}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need need to loop over the array. We will use simple a for loop. Here we need to set three expressions.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for (let i = 0; i &amp;lt; array.length; i++)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The first is which index position to start at. Index 0 is the first number of the array.&lt;/p&gt;

&lt;p&gt;The second is a condition for when to stop the loop. We want i to go over all numbers in the array and stop at the last number. &lt;/p&gt;

&lt;p&gt;The third is how the loop will move over the array. There are many ways to write this. i++ is stating that we will increase i by 1 after each loop.&lt;/p&gt;

&lt;p&gt;Here are all the current steps together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const hasTargetSum = (array, target) =&amp;gt; {
  for (let i = 0; i &amp;lt; array.length; i++){
    ....
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We know we have to add two numbers in the array together. To do this, we will need to loop over the array again. We can use another for loop, but this one will be slightly different that the previous for loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const hasTargetSum = (array, target) =&amp;gt; {
  for (let i = 0; i &amp;lt; array.length; i++){
    for (let n = 1; n &amp;lt; array.length; n++){
      ....
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we will start at index i+1. The reason we set n to i+1 is because we want to stay one index position ahead of i. This will prevent iterating over the same numbers. The rest of the for loop will be the same.&lt;/p&gt;

&lt;p&gt;Now we need to write an expression for adding two numbers in the array and to see if their sums equal the target. We also need to determine what happens if that expression is true or false. Remember, we want both loops to run completely before we determine if the statement is false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const hasTargetSum = (array, target) =&amp;gt; {
  for (let i = 0; i &amp;lt; array.length; i++){
    for (let n = 1; n &amp;lt; array.length; n++){
      if(array[i]+array[n] === target){
        return true
    }
  }
return false
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's try an example and explain what's going on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 3, 8, 10, 5, 3]
const target = 15

const hasTargetSum = (array, target) =&amp;gt; {
  for (let i = 0; i &amp;lt; array.length; i++){
    for (let n = i+1; n &amp;lt; array.length; n++){
      if(array[i]+array[n] === target){
        return true
      }
    }
  }
  return false
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example, the first loop starts at index 0 (the number 1). Then the second for loop is initiated and starts at index 1 (the number 3). The if statement then checks if the sum of those numbers equals the target, 6. Since it does not, i moves to the next index number. The reason n does not change yet is because the second for loop is nested and will continue to run until it reaches the point it is told to stop (n &amp;lt; array.length). &lt;/p&gt;

&lt;p&gt;Here is what's happening:&lt;br&gt;
1+3&lt;br&gt;
1+8&lt;br&gt;
1+10&lt;br&gt;
1+5&lt;br&gt;
1+3&lt;/p&gt;

&lt;p&gt;Since none of these sums equals the target, it moves back to the first for loop and moves the start index forwards one. Then the process repeats.&lt;/p&gt;

&lt;p&gt;None of these sums equal the target. The pattern repeats:&lt;br&gt;
3+8&lt;br&gt;
3+10&lt;br&gt;
3+5&lt;br&gt;
3+3&lt;/p&gt;

&lt;p&gt;None of these sums equal the target. The pattern repeats:&lt;br&gt;
8+10&lt;br&gt;
8+5&lt;br&gt;
8+3&lt;/p&gt;

&lt;p&gt;Still none. The pattern repeats, until:&lt;br&gt;
10+5&lt;/p&gt;

&lt;p&gt;Now that we have two numbers, that when added, equal the target. "true" gets returned.&lt;/p&gt;

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