<?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: Juneiker</title>
    <description>The latest articles on DEV Community by Juneiker (@juneikerc).</description>
    <link>https://dev.to/juneikerc</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%2F410357%2Fb1d1a365-711e-41eb-892a-94d936731cca.jpg</url>
      <title>DEV Community: Juneiker</title>
      <link>https://dev.to/juneikerc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/juneikerc"/>
    <language>en</language>
    <item>
      <title>Keys and Tips for Migrating a WordPress Site to Gatsby Without Losing SEO Quality.</title>
      <dc:creator>Juneiker</dc:creator>
      <pubDate>Sun, 21 Jan 2024 18:22:34 +0000</pubDate>
      <link>https://dev.to/juneikerc/keys-and-tips-for-migrating-a-wordpress-site-to-gatsby-without-losing-seo-quality-3c8a</link>
      <guid>https://dev.to/juneikerc/keys-and-tips-for-migrating-a-wordpress-site-to-gatsby-without-losing-seo-quality-3c8a</guid>
      <description>&lt;p&gt;Are you thinking of switching your website from WordPress to Gatsby? If so, you're likely wondering how to do it without losing the organic positioning you've achieved with so much effort.&lt;/p&gt;

&lt;p&gt;In this article, I will recommend some things you should keep in mind before and during the migration so that it can be carried out without losing the positioning of the old website.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Gatsby is a tool that offers many advantages over WordPress, such as better performance, security, modernity, and flexibility. Although it is currently a bit outdated and a framework like Astro is more recommended.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Key Points to Consider Before Migrating from WordPress to Gatsby Without Losing SEO
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Hosting for the New Site Created with Gatsby
&lt;/h3&gt;

&lt;p&gt;One of the most important things to consider for migration is using a specialized hosting provider to separate the visible part of the site (frontend) from the content management in this case WordPress. Some of the hosting options for deploying static websites created with Gatsby are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Netlify:&lt;/strong&gt; a web development automation platform that provides hosting and backend tools for static websites. It stands out for its easy integration with version control systems like GitHub, allowing automatic and continuous deployments. It offers features like custom domains, free SSL certificates, and a global content delivery network (CDN).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vercel:&lt;/strong&gt; a hosting platform focused on developer experience, optimized for static websites and JavaScript web applications (especially Next.js). It provides automatic deployments from Git repositories, a global CDN, and other functionalities like automatic scaling and support for serverless functions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloudflare Pages:&lt;/strong&gt; a hosting and deployment service from Cloudflare designed specifically for static websites and JavaScript frontend applications. It offers direct integration with Git repositories for automatic deployments, a global CDN, and security and performance features inherent in Cloudflare's infrastructure.
&lt;strong&gt;Important Note&lt;/strong&gt;
Although you can host the new site within your current hosting, it is more advisable to do it on one of these platforms as they are prepared to offer better performance and speed, thus improving the SEO and user experience of the web.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Technically, you don't need to touch anything in the hosting of your current WordPress to carry out the migration. Except for changing the DNS to point to the new site.&lt;/p&gt;

&lt;h3&gt;
  
  
  Domain Management
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Maintaining the Current Domain:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Preserve the Domain&lt;/strong&gt;: Ideally, keep your current domain to preserve brand recognition and SEO.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DNS Configuration&lt;/strong&gt;: Update the DNS records to point to the new hosting provider. This involves changing records such as A, AAAA, or CNAME, according to your new host's instructions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  SEO Considerations When Changing the Domain:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Minimize Downtime&lt;/strong&gt;: Coordinate the DNS change to minimize site downtime, which can affect your search engine ranking.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Redirects and URLs&lt;/strong&gt;: Ensure that all redirects are correctly set up in the new environment to maintain SEO integrity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If for some reason you change any URL on your website, you must do a 301 redirect to inform the browser and search engines that the page has moved. This is one of the factors why many migrations fail from an SEO standpoint, as if you do not inform Google (search engines) that a URL has changed, it will not know that it should assign the positioning to the new URL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Note&lt;/strong&gt;&lt;br&gt;
When pointing the domain to the new site, you will lose it on the current site. The ideal is to assign a subdomain to the WordPress site, which will be the API from which we will consume the content, for easy recognition.&lt;/p&gt;
&lt;h2&gt;
  
  
  Tips for Migrating from WordPress to Gatsby Without Losing SEO Quality
&lt;/h2&gt;

&lt;p&gt;In the previous section, I developed the key points you should consider before carrying out a migration from WordPress to Gatsby. Next, I give you some tips that you should keep in mind in the development phase of the site.&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Obtaining the Content:
&lt;/h3&gt;

&lt;p&gt;The content of your website is the most important element in maintaining your SEO quality. Therefore, you should ensure to keep the same content, or improve it if possible, when migrating to Gatsby.&lt;/p&gt;

&lt;p&gt;To obtain content from WordPress, you should use the &lt;code&gt;gatsby-source-wordpress&lt;/code&gt; plugin, and within WordPress install the plugins: WPGatsby WPGraphQL, which are responsible for formatting all the content so it can be easily obtained from Gatsby.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Meta Data:
&lt;/h3&gt;

&lt;p&gt;The meta data of your website provide information about the title, description, keywords, author, etc., of each page. These data are used by search engines and social networks to display your website in search results and posts.&lt;/p&gt;

&lt;p&gt;Therefore, you should maintain or improve the meta data of your website when migrating to Gatsby.&lt;br&gt;
From version 4.0 of Gatsby, you can create a Head function within the pages where you can define the meta tags for each page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const Head = ({ data }) =&amp;gt; {
  const { wordpressTitle, wordpressMetaDescription } = data;


  return (
    &amp;lt;&amp;gt;
      &amp;lt;title&amp;gt;{wordpressTitle}&amp;lt;/title&amp;gt;
      &amp;lt;meta name="description" content={wordpressMetaDescription} /&amp;gt;

    &amp;lt;/&amp;gt;
  );
};


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

&lt;/div&gt;



&lt;p&gt;You can also use the &lt;code&gt;gatsby-plugin-react-helmet&lt;/code&gt; plugin to add the meta data of each page using the &lt;code&gt;&amp;lt;Helmet&amp;gt;&lt;/code&gt; component of React.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Management of Sitemap and Robots.txt File
&lt;/h3&gt;

&lt;p&gt;Effective management of the sitemap and the &lt;code&gt;robots.txt&lt;/code&gt; file is crucial in the migration of a website, especially for maintaining and improving the SEO and indexing of the site in search engines.&lt;/p&gt;

&lt;h4&gt;
  
  
  Sitemap
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Generation and Updating of Sitemap:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Automatic Creation&lt;/strong&gt;: In Gatsby, you can automatically generate a sitemap using plugins like &lt;code&gt;gatsby-plugin-sitemap&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structure and URLs&lt;/strong&gt;: Make sure the sitemap reflects the current structure of the site and all important URLs are included.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SEO Considerations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Submission to Search Engines&lt;/strong&gt;: Submit the updated sitemap to tools like Google Search Console to facilitate indexing of your site.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Post-Migration Review&lt;/strong&gt;: Verify that all URLs in the sitemap are accessible and do not lead to 404 errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Robots.txt File&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Proper Configuration:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use of &lt;code&gt;gatsby-plugin-robots-txt&lt;/code&gt;&lt;/strong&gt;: This plugin allows you to create and configure the &lt;code&gt;robots.txt&lt;/code&gt; file on your Gatsby site, which is crucial for controlling how search engines crawl and access your site.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom Directives&lt;/strong&gt;: Customize the directives in &lt;code&gt;robots.txt&lt;/code&gt; to exclude specific pages from crawling or to give specific instructions to search bots.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Impact on SEO and Indexing:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Crawl Control&lt;/strong&gt;: A well-configured &lt;code&gt;robots.txt&lt;/code&gt; file can improve SEO by avoiding the crawling of duplicate or irrelevant pages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoid Blocking Important Content&lt;/strong&gt;: Make sure not to accidentally block access to important CSS, JS, or image files that are important for the indexing and ranking of the site.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Image Optimization:
&lt;/h3&gt;

&lt;p&gt;The images on your website are what add visual value and appeal to your content. They are also important for your SEO, as they can improve load time, engagement, and traffic to your website.&lt;/p&gt;

&lt;p&gt;Therefore, you should optimize and adapt the images of your website when migrating to Gatsby. You can use the &lt;code&gt;gatsby-plugin-image&lt;/code&gt; plugin to process and display your website's images optimally. This plugin allows you to use the &lt;code&gt;&amp;lt;GatsbyImage&amp;gt;&lt;/code&gt; component of React, which applies techniques such as responsive design, lazy-loading, blur-up, webp, etc., to your images.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Add Structured Data Markup
&lt;/h3&gt;

&lt;p&gt;Structured data are snippets of code that help search engines understand the content of your website and display it more attractively in search results.&lt;/p&gt;

&lt;p&gt;For example, you can use structured data to indicate the type of content, author, date, title, image, rating, etc. To add structured data to your website, you can use the gatsby-plugin-jsonld plugin, which allows you to generate a JSON-LD with the information you want.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Note&lt;/strong&gt;&lt;br&gt;
Keep in mind that by default WordPress, thanks to plugins like Yoast SEO or Rank Math, generates structured data markup, so it's important to maintain them in the new site to not lose positions.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;Publish your Gatsby Website and Monitor the Results&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Finally, when you are satisfied with the result, you can publish your Gatsby website and enjoy its benefits. You can use services like Netlify or Vercel to easily deploy your Gatsby website, or use your own server or hosting if you prefer.&lt;/p&gt;

&lt;p&gt;You can also use tools like Google Search Console, Google Analytics, Lighthouse, etc., to monitor and evaluate the results of your migration, and make necessary adjustments if needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Farewell
&lt;/h2&gt;

&lt;p&gt;As you can see, migrating your WordPress website to Gatsby without losing SEO quality is possible if you follow the keys and tips I have given you in this article.&lt;/p&gt;

&lt;p&gt;I hope this article has been useful to you. If you have any questions, feel free to contact me. If you liked the article, don't hesitate to share it, and if you need a &lt;a href="https://juneikerc.com/en/services/development-with-gatsbyjs/"&gt;Gatsby.JS developer&lt;/a&gt;, don't hesitate to hire my services. See you soon!&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>wordpress</category>
      <category>seo</category>
    </item>
    <item>
      <title>AdventJS Challenge 4, midudev 2023 - Turn the parentheses around</title>
      <dc:creator>Juneiker</dc:creator>
      <pubDate>Tue, 05 Dec 2023 15:29:07 +0000</pubDate>
      <link>https://dev.to/juneikerc/adventjs-challenge-4-midudev-2023-turn-the-parentheses-around-5ehi</link>
      <guid>https://dev.to/juneikerc/adventjs-challenge-4-midudev-2023-turn-the-parentheses-around-5ehi</guid>
      <description>&lt;h2&gt;
  
  
  Instructions for the challenge:
&lt;/h2&gt;

&lt;p&gt;In 🎅 Santa's workshop, some Christmas messages have been written in a peculiar way: the words within the brackets must be read backwards.&lt;/p&gt;

&lt;p&gt;Santa needs these messages to be correctly formatted. Your task is to write a function that takes a string and reverses the characters within each pair of parentheses, removing the parentheses as well.&lt;/p&gt;

&lt;p&gt;However, bear in mind that there may be nested parentheses, so you should reverse the characters in the correct order.&lt;/p&gt;

&lt;p&gt;const a = decode('hola (odnum)')&lt;br&gt;
console.log(a) // hola mundo&lt;/p&gt;

&lt;p&gt;const b = decode('(olleh) (dlrow)!')&lt;br&gt;
console.log(b) // hello world!&lt;/p&gt;

&lt;p&gt;const c = decode('sa(u(cla)atn)s')&lt;br&gt;
console.log(c) // santaclaus&lt;/p&gt;

&lt;p&gt;// Step by step:&lt;br&gt;
// 1. Reverse the nested -&amp;gt; sa(ualcatn)s&lt;br&gt;
// 2. Reverse the remaining one -&amp;gt; santaclaus&lt;br&gt;
Notes:&lt;/p&gt;

&lt;p&gt;The input strings will always be well formed with parentheses that match correctly, you do not need to validate them.&lt;br&gt;
There should not be any parentheses left in the final message.&lt;br&gt;
The maximum nesting level is 2.&lt;/p&gt;
&lt;h2&gt;
  
  
  My solution AdventJS Challenge day 4
&lt;/h2&gt;

&lt;p&gt;Before explaining the solution, I identified that to solve this problem, I needed a complex regular expression to find patterns of open and closed parentheses.&lt;/p&gt;

&lt;p&gt;I'm not good with regular expressions (in fact, I'm quite bad), so for this particular case, I sought help from ChatGPT. Now, I'll proceed to explain the solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\(([^&lt;/span&gt;&lt;span class="sr"&gt;()&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)\)&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Define a regular expression &lt;code&gt;/\(([^()]+)\)/g&lt;/code&gt;: &lt;code&gt;\(&lt;/code&gt; and &lt;code&gt;\)&lt;/code&gt; match open and closed parentheses, respectively. &lt;code&gt;([^()]+)&lt;/code&gt; is a capture group that matches any character except parentheses, at least once. The &lt;code&gt;g&lt;/code&gt; modifier indicates global search, meaning it looks for all matches rather than stopping after the first.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initialize the variable &lt;code&gt;match&lt;/code&gt; using &lt;code&gt;message.match(regex)&lt;/code&gt;. This finds the first match of the regular expression in the message and stores information about the match in the variable &lt;code&gt;match&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start a &lt;code&gt;while&lt;/code&gt; loop that continues as long as there are matches (&lt;code&gt;match !== null&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the loop, you can print the current match to the console with &lt;code&gt;console.log(match)&lt;/code&gt; (this can be helpful for understanding the process). Inspecting the console with &lt;code&gt;match&lt;/code&gt; may return something like this: &lt;code&gt;[ '(cla)', 'cla', index: 4, input: 'sa(u(cla)atn)s', groups: undefined ]&lt;/code&gt;. From these data, we need the value at index 1 to reverse it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reverse the content of the found substring using &lt;code&gt;split('')&lt;/code&gt;, &lt;code&gt;reverse()&lt;/code&gt;, and &lt;code&gt;join('')&lt;/code&gt;. This is stored in the variable &lt;code&gt;reversed&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Replace the original substring (&lt;code&gt;message&lt;/code&gt;) with its reversed version in the message using &lt;code&gt;message.replace(regex, reversed)&lt;/code&gt;. This finds the first match and replaces it with the value of &lt;code&gt;reversed&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assign a new value to the &lt;code&gt;match&lt;/code&gt; variable using the updated value stored in &lt;code&gt;message&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The loop continues until there are no more matches.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, return the modified message.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Previous Challenges:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; &lt;a href="https://dev.to/juneikerc/challenge-1-solution-adventjs-2023-by-midudev-104c"&gt;Challenge 1 advent JS 2023&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; &lt;a href="https://dev.to/juneikerc/challenge-2-advent-js-2023-by-midudev-4p6f"&gt;Challenge 2 advent JS 2023&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/juneikerc/challenge-3-adventjs-midudev-the-naughty-elf-11pb"&gt;Challenge 3 advent JS 2023&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Useful Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions"&gt;MDN Regular Expressions&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while"&gt;While Loop&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://juneikerc.com/en/code-blog/javascript/reverse-string/"&gt;Reverse String&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>adventofcode</category>
      <category>challenge</category>
      <category>programming</category>
    </item>
    <item>
      <title>Challenge 3 adventjs midudev - The Naughty Elf</title>
      <dc:creator>Juneiker</dc:creator>
      <pubDate>Mon, 04 Dec 2023 01:10:23 +0000</pubDate>
      <link>https://dev.to/juneikerc/challenge-3-adventjs-midudev-the-naughty-elf-11pb</link>
      <guid>https://dev.to/juneikerc/challenge-3-adventjs-midudev-the-naughty-elf-11pb</guid>
      <description>&lt;p&gt;&lt;a href="https://www.linkedin.com/in/juneiker-castillo/"&gt;Juneiker Castillo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;In Santa's workshop, &lt;strong&gt;a naughty elf&lt;/strong&gt; has been playing around in the gift manufacturing chain, adding or removing an unplanned step.&lt;/p&gt;

&lt;p&gt;You have the original sequence of steps in the manufacturing process &lt;em&gt;original&lt;/em&gt; and the modified sequence &lt;em&gt;modified&lt;/em&gt; that may include an extra step or miss a step.&lt;/p&gt;

&lt;p&gt;Your task is to &lt;strong&gt;write a function that identifies and returns the first extra step that has been added or removed in the manufacturing chain&lt;/strong&gt;. If there is no difference between the sequences, return an empty string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abcd&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abcde&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nf"&gt;findNaughtyStep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 'e'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;stepfor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;stepor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nf"&gt;findNaughtyStep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 'f'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abcde&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abcde&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nf"&gt;findNaughtyStep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ''&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Considerations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There will always be one step of difference or none.&lt;/li&gt;
&lt;li&gt;The modification can occur anywhere in the chain.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;original&lt;/em&gt; sequence can be empty.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My Solution
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findNaughtyStep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;differentElementIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;differentElementIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;differentElementIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;modified&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;differentElementIndex&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Declare a conditional statement that returns an empty string if the original string is equal to the modified string.&lt;/li&gt;
&lt;li&gt;Initialize a variable &lt;code&gt;differentElementIndex&lt;/code&gt; to 0, which will store the index where the elements at the same position in both strings are different.&lt;/li&gt;
&lt;li&gt;Create a for loop that increments a variable &lt;code&gt;i&lt;/code&gt; while &lt;code&gt;original[i] === modified[i]&lt;/code&gt; are equal. When they are not equal, assign the value of the current index + 1 to &lt;code&gt;differentElementIndex&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Finally, check if the string named &lt;code&gt;original&lt;/code&gt; is longer than the &lt;code&gt;modified&lt;/code&gt; string. If true, extract the element at the &lt;code&gt;differentElementIndex&lt;/code&gt; obtained previously. Otherwise, if &lt;code&gt;modified&lt;/code&gt; is longer, extract the element from the &lt;code&gt;modified&lt;/code&gt; string.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--auso5Zrf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.licdn.com/dms/image/D4E12AQGPnAn6Oe3JdA/article-inline_image-shrink_1500_2232/0/1701651857730%3Fe%3D1707350400%26v%3Dbeta%26t%3DdCFkpi0vXCqh2XZ3HCZqGwE8OzYNQnPXcvxLDiGXLO4" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--auso5Zrf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.licdn.com/dms/image/D4E12AQGPnAn6Oe3JdA/article-inline_image-shrink_1500_2232/0/1701651857730%3Fe%3D1707350400%26v%3Dbeta%26t%3DdCFkpi0vXCqh2XZ3HCZqGwE8OzYNQnPXcvxLDiGXLO4" alt="Challenge 3 Code" width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code Challenge 3&lt;/p&gt;

&lt;h2&gt;
  
  
  Previous Challenges:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/juneikerc/challenge-1-solution-adventjs-2023-by-midudev-104c"&gt;Challenge 1 advent JS 2023&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/juneikerc/challenge-2-advent-js-2023-by-midudev-4p6f"&gt;Challenge 2 advent JS 2023&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>adventofcode</category>
      <category>challenge</category>
      <category>programming</category>
    </item>
    <item>
      <title>Challenge 2 Advent JS 2023 by Midudev</title>
      <dc:creator>Juneiker</dc:creator>
      <pubDate>Sun, 03 Dec 2023 00:41:02 +0000</pubDate>
      <link>https://dev.to/juneikerc/challenge-2-advent-js-2023-by-midudev-4p6f</link>
      <guid>https://dev.to/juneikerc/challenge-2-advent-js-2023-by-midudev-4p6f</guid>
      <description>&lt;p&gt;My solution to challenge 2 in this series&lt;/p&gt;

&lt;h2&gt;
  
  
  Instructions:
&lt;/h2&gt;

&lt;p&gt;In Santa's workshop, the elves have a &lt;strong&gt;list of gifts&lt;/strong&gt; they want to make and a limited set of materials.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;gifts are strings&lt;/em&gt; and the &lt;em&gt;materials are characters&lt;/em&gt;. Your task is to write a function that, given a list of gifts and the available materials, returns a &lt;strong&gt;list of gifts that can be manufactured&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A gift can be manufactured if we have all the necessary materials to make it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gifts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tren&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;oso&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pelota&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;materials&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tronesa&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;manufacture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gifts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;materials&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ["train", "bear"]&lt;/span&gt;
&lt;span class="c1"&gt;// 'train' YES because its letters are in 'tronesa'&lt;/span&gt;
&lt;span class="c1"&gt;// 'bear' YES because its letters are in 'tronesa'&lt;/span&gt;
&lt;span class="c1"&gt;// 'ball' NO because its letters are NOT in 'tronesa'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gifts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;juego&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;puzzle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;materials&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jlepuz&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;manufacture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gifts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;materials&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ["puzzle"]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gifts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;libro&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ps5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;materials&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;psli&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;manufacture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gifts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;materials&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// []&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;manufacture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gifts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;materials&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;materialsSet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;materials&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lettersInWord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;letter&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;materialsSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;letter&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;gifts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gift&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;gift&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nf"&gt;lettersInWord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gift&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a Set that separates all the letters from materials.&lt;/li&gt;
&lt;li&gt;Create an auxiliary function &lt;code&gt;lettersInWord&lt;/code&gt; that takes a word, splits it into letters (using &lt;code&gt;.split()&lt;/code&gt;), filters the letters based on whether they belong to the &lt;code&gt;materialsSet&lt;/code&gt; previously defined, and finally joins them back into a string.&lt;/li&gt;
&lt;li&gt;The final step is to apply a filter on the &lt;code&gt;gifts&lt;/code&gt; array and return only those elements whose length is equal to the length of the word returned by the &lt;code&gt;lettersInWord&lt;/code&gt; function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TWt2wXwQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.licdn.com/dms/image/D4E12AQGMcCymxwa07g/article-inline_image-shrink_1500_2232/0/1701563777119%3Fe%3D1707350400%26v%3Dbeta%26t%3DTW2ROtjVeDKPTC6uK2ZiFrvZy-WDL0EUcrTuZE9iW4w" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TWt2wXwQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.licdn.com/dms/image/D4E12AQGMcCymxwa07g/article-inline_image-shrink_1500_2232/0/1701563777119%3Fe%3D1707350400%26v%3Dbeta%26t%3DTW2ROtjVeDKPTC6uK2ZiFrvZy-WDL0EUcrTuZE9iW4w" alt="Solution Image" width="800" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Solution&lt;/p&gt;

&lt;h2&gt;
  
  
  Previous Challenge Solutions
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/juneikerc/challenge-1-solution-adventjs-2023-by-midudev-104c"&gt;Challenge 1 Advent JS 2023&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>adventofcode</category>
      <category>challenge</category>
      <category>programming</category>
    </item>
    <item>
      <title>Challenge 1 Solution AdventJs 2023 by Midudev.</title>
      <dc:creator>Juneiker</dc:creator>
      <pubDate>Fri, 01 Dec 2023 22:23:17 +0000</pubDate>
      <link>https://dev.to/juneikerc/challenge-1-solution-adventjs-2023-by-midudev-104c</link>
      <guid>https://dev.to/juneikerc/challenge-1-solution-adventjs-2023-by-midudev-104c</guid>
      <description>&lt;p&gt;As is customary for the past couple of years, Miguel Ángel Durán García (midudev) has published the traditional &lt;a href="https://adventjs.dev/en"&gt;AdventJs&lt;/a&gt; with programming challenges from December 1st to December 25th.&lt;/p&gt;

&lt;p&gt;Honestly, I haven't been very active in past events, but I've set out to complete this challenge, and what better way to do it than by documenting it here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Description of the First AdventJs 2023 Challenge
&lt;/h2&gt;

&lt;p&gt;"In the toy factory at the North Pole, each toy has a unique identification number.&lt;/p&gt;

&lt;p&gt;However, due to an error in the toy machine, some numbers have been assigned to more than one toy.&lt;/p&gt;

&lt;p&gt;Find the first identification number that has been repeated, &lt;strong&gt;where the second occurrence has the smallest index&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;In other words, if there is more than one repeated number, you should return the number whose second occurrence appears first in the list. If there are no repeated numbers, return -1."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;giftIds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstRepeatedId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findFirstRepeated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;giftIds&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstRepeatedId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="c1"&gt;// Although 2 and 3 are repeated,&lt;/span&gt;
&lt;span class="c1"&gt;// 3 appears first for the second time&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;giftIds2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstRepeatedId2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findFirstRepeated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;giftIds2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstRepeatedId2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// -1&lt;/span&gt;
&lt;span class="c1"&gt;// It is -1 since no number is repeated&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;giftIds3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstRepeatedId3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findFirstRepeated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;giftIds3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstRepeatedId3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  My solution to this challenge
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findFirstRepeated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gifts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="cm"&gt;/* The filter function checks if the current index of the element is different from the first index of occurrence of the element. */&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;repeatElements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gifts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;repeatElements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;repeatElements&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Explanation:
&lt;/h3&gt;

&lt;p&gt;The important part of this code is the constant &lt;strong&gt;repeatElements&lt;/strong&gt;. I created a filter on the &lt;strong&gt;gifts&lt;/strong&gt; array that essentially iterates through the array and applies the condition &lt;code&gt;(arr.indexOf(elem) !== index)&lt;/code&gt; which adds the element if the index of the first occurrence of the current element (using the &lt;code&gt;indexOf&lt;/code&gt; method) is different from the current index.&lt;/p&gt;

&lt;p&gt;Finally, thanks to the ternary operator, if the length of the &lt;strong&gt;repeatElements&lt;/strong&gt; array is greater than 0, we return the value of &lt;strong&gt;repeatElements&lt;/strong&gt; at index 0 (which is precisely what we need in this case), and if not, it returns -1.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Score Did I Get?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--as-cHm-e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.licdn.com/dms/image/D4E12AQE5qk0nthH59g/article-inline_image-shrink_1500_2232/0/1701452101155%3Fe%3D1706745600%26v%3Dbeta%26t%3DljXhde-NSRk3HNPyHXSqK-rvzN01XVC4zXHYVgyU6-g" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--as-cHm-e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.licdn.com/dms/image/D4E12AQE5qk0nthH59g/article-inline_image-shrink_1500_2232/0/1701452101155%3Fe%3D1706745600%26v%3Dbeta%26t%3DljXhde-NSRk3HNPyHXSqK-rvzN01XVC4zXHYVgyU6-g" alt="My score" width="514" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My score was 400 initially (hence the screenshot), then I tried again and it changed to 270.&lt;/p&gt;

&lt;p&gt;About the Use of Chat-GPT&lt;br&gt;
Midudev clarified that the spirit of this challenge is to solve problems on our own to improve our problem-solving skills.&lt;/p&gt;

&lt;p&gt;He also mentioned that if you get stuck, it's not wrong to ask the AI for a bit of help. After all, this is not a technical job test but rather a way to enhance our abilities as developers in a more entertaining manner than typical programming challenges.&lt;/p&gt;

&lt;p&gt;Personally, I didn't use chat-GPT, but the idea of using the .filter() method along with indexOf() was inspired by a tutorial I had recently created on removing duplicate elements from an array. &lt;a href="https://juneikerc.com/en/code-blog/javascript/remove-duplicate-elements-from-array/"&gt;Link to the tutorial&lt;/a&gt; One of the methods is similar to the use case of this challenge.&lt;/p&gt;

&lt;p&gt;If you've made it this far, I hope you've understood my explanation 😊😊 Thank you very much for reading. I invite you to participate in this challenge and share your achievements.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>adventofcode</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
