<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://alexvalle.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 07:24:27 GMT</lastBuildDate><atom:link href="https://alexvalle.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Lynx: The mobile framework for web devs]]></title><description><![CDATA[Every week, it feels like there's a new JavaScript framework hitting the market—so much so that it's become overwhelming. Still, the reigning giants are React, Angular, Vue, and Svelte (which continues to gain traction). But when it comes to mobile a...]]></description><link>https://alexvalle.dev/lynx-the-mobile-framework-for-web-devs</link><guid isPermaLink="true">https://alexvalle.dev/lynx-the-mobile-framework-for-web-devs</guid><category><![CDATA[lynx]]></category><category><![CDATA[React]]></category><category><![CDATA[Mobile Development]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Mon, 14 Apr 2025 14:00:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1744589962857/ed67fe9c-6ea0-4226-9df6-62b66c5fb436.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Every week, it feels like there's a new JavaScript framework hitting the market—so much so that it's become overwhelming. Still, the reigning giants are React, Angular, Vue, and Svelte (which continues to gain traction). But when it comes to mobile apps, there's really only one <em>popular</em> option: React Native. And while it's a solid choice, the fact that it's so dominant—especially with tools like Expo—comes with its own limitations, such as being tied to React and the specific constraints of that ecosystem.</p>
<p>In March 2025, <strong>Lynx</strong> was officially announced on its <a target="_blank" href="https://lynxjs.org/blog/lynx-unlock-native-for-more.html">official blog</a>. The team describes it as a family of technologies designed to build both web and truly native mobile experiences from a single codebase—similar to React Native, but with a focus on performance, versatility, and modern development practices.</p>
<h2 id="heading-a-true-web-alternative">A True Web Alternative</h2>
<p>While React Native may feel like web development, it’s not entirely the same—especially when it comes to styling. Native CSS support is missing, which creates friction with libraries like Tailwind or Shadcn. Lynx, on the other hand, supports native CSS, transitions, animations, and more, out of the box—bringing it closer to the real web dev experience.</p>
<h2 id="heading-performance">Performance</h2>
<p>Lynx's impressive performance is thanks to its dual-runtime architecture:</p>
<p><strong>1. Main-thread runtime</strong>: A performance-optimized library focused on handling high-priority events.</p>
<p><strong>2. Background runtime</strong>: Keeps the main thread light and unblocked.</p>
<p>Because of this setup, Lynx delivers two key features:</p>
<ul>
<li><p><strong>Instant First-Frame Rendering (IFR)</strong>: If rendering is fast enough, Lynx can eliminate loading screens altogether, creating a <em>snappy</em>, app-like experience.</p>
</li>
<li><p><strong>Main-Thread Scripting (MTS)</strong>: Handles high-priority interactions like gestures and user input, resulting in buttery-smooth interfaces that <em>feel</em> native.</p>
</li>
</ul>
<h2 id="heading-not-everything-is-perfect">Not Everything Is Perfect</h2>
<p>Lynx sounds like an ideal alternative for web developers moving into mobile—but there are some caveats.</p>
<p>One of the best things about web development is the massive library ecosystem that helps us build faster. Unfortunately, Lynx doesn't support standard HTML elements like <code>&lt;button&gt;</code>, <code>&lt;input&gt;</code>, etc. Instead, you're limited to custom elements such as <code>view</code>, <code>text</code>, and <code>image</code>.</p>
<p>That means importing a button from a UI library like Shadcn likely won't work—it'll throw an error because those components are built using standard HTML tags.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1744587412377/13f30e86-dab0-412b-a3e1-0651a7b2720a.png" alt class="image--center mx-auto" /></p>
<p>So while Tailwind works beautifully, libraries like Shadcn and similar component kits are a no-go—for now.</p>
<h2 id="heading-code-just-react">Code: Just React</h2>
<p>The nice thing? You still write React code—just swap your import:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'@lynx-js/react'</span>;
</code></pre>
<p>Here’s an example where tapping the logo switches between two images:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> { useCallback, useEffect, useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'@lynx-js/react'</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>
<span class="hljs-keyword">import</span> arrow <span class="hljs-keyword">from</span> <span class="hljs-string">'./assets/arrow.png'</span>
<span class="hljs-keyword">import</span> lynxLogo <span class="hljs-keyword">from</span> <span class="hljs-string">'./assets/lynx-logo.png'</span>
<span class="hljs-keyword">import</span> reactLynxLogo <span class="hljs-keyword">from</span> <span class="hljs-string">'./assets/react-logo.png'</span>

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [alterLogo, setAlterLogo] = useState(<span class="hljs-literal">false</span>)

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">console</span>.info(<span class="hljs-string">'Hello, ReactLynx'</span>)
  }, [])

  <span class="hljs-keyword">const</span> onTap = useCallback(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-string">'background only'</span>
    setAlterLogo(!alterLogo)
  }, [alterLogo])

  <span class="hljs-keyword">return</span> (
    &lt;view&gt;
      &lt;view className=<span class="hljs-string">'Background'</span> /&gt;
      &lt;view className=<span class="hljs-string">'App'</span>&gt;
        &lt;view className=<span class="hljs-string">'Banner'</span>&gt;
          &lt;view className=<span class="hljs-string">'Logo'</span> bindtap={onTap}&gt;
            {alterLogo
              ? &lt;image src={reactLynxLogo} className=<span class="hljs-string">'Logo--react'</span> /&gt;
              : &lt;image src={lynxLogo} className=<span class="hljs-string">'Logo--lynx'</span> /&gt;}
          &lt;/view&gt;
          &lt;text className=<span class="hljs-string">'Title'</span>&gt;React&lt;/text&gt;
          &lt;text className=<span class="hljs-string">'Subtitle'</span>&gt;on Lynx&lt;/text&gt;
        &lt;/view&gt;
        &lt;view className=<span class="hljs-string">'Content'</span>&gt;
          &lt;image src={arrow} className=<span class="hljs-string">'Arrow'</span> /&gt;
          &lt;text className=<span class="hljs-string">'Description'</span>&gt;Tap the logo and have fun!&lt;/text&gt;
          &lt;text className=<span class="hljs-string">'Hint'</span>&gt;
            Edit&lt;text style={{ fontStyle: <span class="hljs-string">'italic'</span> }}&gt;{<span class="hljs-string">' src/App.tsx '</span>}&lt;/text&gt;
            to see updates!
          &lt;/text&gt;
        &lt;/view&gt;
        &lt;view style={{ flex: <span class="hljs-number">1</span> }}&gt;&lt;/view&gt;
      &lt;/view&gt;
    &lt;/view&gt;
  )
}
</code></pre>
<p>Here’s what that code renders:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1744587437179/6815b112-6621-4571-996e-294c81352967.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-final-thoughts">Final Thoughts</h2>
<p>Lynx is brand new to the open-source world, but its community is growing fast, and it shows real promise. That said, it's still far from achieving the maturity, ecosystem, and support React Native enjoys. There’s currently limited documentation, few tutorials, and most AI tools won’t be able to help you much when you run into bugs or roadblocks.</p>
<p>My take? Lynx is fantastic for MVPs, side projects, or just experimenting. But if you're building something large-scale and production-ready, React Native is still the safer choice—for now.</p>
<p><strong>What do you think of this new framework?</strong></p>
]]></content:encoded></item><item><title><![CDATA[AdventJS 2023: Day 11 Challenge]]></title><description><![CDATA[The solution to challenge #11 of AdventJS 2023
The solution to the previous challenge
The solution to the next challenge
Challenge Description
In Santa's workshop, the elves love puzzles 🧠. This year, they have created a special one: a challenge to ...]]></description><link>https://alexvalle.dev/adventjs-2023-day-11-challenge</link><guid isPermaLink="true">https://alexvalle.dev/adventjs-2023-day-11-challenge</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[challenge]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Mon, 11 Dec 2023 06:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1702609004456/daad69e9-e4d1-4298-9462-dc69ae715e46.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>The solution to</strong> <a target="_blank" href="https://adventjs.dev/en/challenges/2023/11"><strong>challenge #11</strong></a> <strong>of</strong> <a target="_blank" href="https://adventjs.dev/en"><strong>AdventJS 2023</strong></a></p>
<p><a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-10-challenge"><strong>The solution to the</strong> previous challenge</a></p>
<p><a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-11-challenge"><strong>The solution to the next challenge</strong></a></p>
<h2 id="heading-challenge-description">Challenge Description</h2>
<p>In Santa's workshop, the elves love puzzles 🧠. This year, they have created a special one: a challenge to form a Christmas palindrome.</p>
<p><strong>A palindrome is a word that reads the same forwards and backward.</strong> The elves want to know if it is possible to form a palindrome by making, at most, one exchange of letters.</p>
<p>Create a function <code>getIndexsForPalindrome</code> that receives a string and returns:</p>
<ul>
<li><p>If it is already a palindrome, an empty array.</p>
</li>
<li><p>If it is not possible, null.</p>
</li>
<li><p>If a palindrome can be formed with one change, an array with the two positions (indexes) must be swapped to create it.</p>
</li>
</ul>
<p>For example:</p>
<pre><code class="lang-javascript">getIndexsForPalindrome(<span class="hljs-string">'anna'</span>) <span class="hljs-comment">// []</span>
getIndexsForPalindrome(<span class="hljs-string">'abab'</span>) <span class="hljs-comment">// [0, 1]</span>
getIndexsForPalindrome(<span class="hljs-string">'abac'</span>) <span class="hljs-comment">// null</span>
getIndexsForPalindrome(<span class="hljs-string">'aaaaaaaa'</span>) <span class="hljs-comment">// []</span>
getIndexsForPalindrome(<span class="hljs-string">'aaababa'</span>) <span class="hljs-comment">// [1, 3]</span>
getIndexsForPalindrome(<span class="hljs-string">'caababa'</span>) <span class="hljs-comment">// null</span>
</code></pre>
<p>If the palindrome can be formed with different swaps, <strong>always return the first one found.</strong></p>
<h2 id="heading-analysis">Analysis</h2>
<p>In this challenge, our goal is to find a palindrome by swapping two elements of the string and return the indices of those two elements. If a palindrome cannot be found, return an empty array.</p>
<h3 id="heading-input">Input</h3>
<ol>
<li>Word (<code>word</code>): A string with the word to evaluate.</li>
</ol>
<h3 id="heading-output">Output</h3>
<ul>
<li><p>An array of two elements with the indices to change to find the palindrome.</p>
</li>
<li><p>If it's already a palindrome, return an empty array.</p>
</li>
<li><p>If none of the above conditions are met, return <code>null</code>.</p>
</li>
</ul>
<h3 id="heading-considerations">Considerations</h3>
<ul>
<li>If multiple palindromes can be formed, always return the first one found.</li>
</ul>
<h2 id="heading-solution">Solution</h2>
<p>It can be solved by iterating over each element of the string, and within that iteration, iterating again and swapping elements to compare.</p>
<h3 id="heading-code">Code</h3>
<pre><code class="lang-javascript"><span class="hljs-comment">/**
 * Returns the indices of characters that need to be reversed in order to make a word a palindrome.
 *
 * <span class="hljs-doctag">@param <span class="hljs-type">{string}</span> <span class="hljs-variable">word</span></span> - The word to check for palindromic properties.
 * <span class="hljs-doctag">@return <span class="hljs-type">{number[] | null}</span> </span>- An array of two indices representing the characters to be reversed, or null if no such indices exist.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getIndexsForPalindrome</span>(<span class="hljs-params">word</span>) </span>{
  <span class="hljs-comment">// Check if the given word is already a palindrome</span>
  <span class="hljs-keyword">const</span> isPalindrome = <span class="hljs-function">(<span class="hljs-params">s</span>) =&gt;</span> s === s.split(<span class="hljs-string">""</span>).reverse().join(<span class="hljs-string">""</span>);

  <span class="hljs-comment">// If the word is already a palindrome, return an empty array</span>
  <span class="hljs-keyword">if</span> (isPalindrome(word)) {
    <span class="hljs-keyword">return</span> [];
  }

  <span class="hljs-comment">// Iterate through all possible pairs of indices</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; word.length; i += <span class="hljs-number">1</span>) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = i + <span class="hljs-number">1</span>; j &lt; word.length; j += <span class="hljs-number">1</span>) {
      <span class="hljs-comment">// Create a new word by swapping the characters at indices i and j</span>
      <span class="hljs-keyword">const</span> newWord =
        word.substring(<span class="hljs-number">0</span>, i) +
        word[j] +
        word.substring(i + <span class="hljs-number">1</span>, j) +
        word[i] +
        word.substring(j + <span class="hljs-number">1</span>);

      <span class="hljs-comment">// If the new word is a palindrome, return the indices i and j</span>
      <span class="hljs-keyword">if</span> (isPalindrome(newWord)) {
        <span class="hljs-keyword">return</span> [i, j];
      }
    }
  }

  <span class="hljs-comment">// If no such indices exist, return null</span>
  <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
}
</code></pre>
<h3 id="heading-community-solutions">Community Solutions</h3>
<p>Solution by <a target="_blank" href="https://github.com/iswilljr/adventjs/blob/master/2023/challenge-11/challenge-11.ts">iswilljr</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getIndexsForPalindrome</span>(<span class="hljs-params">word: string</span>) </span>{
  <span class="hljs-keyword">const</span> _letters = [...word]
  <span class="hljs-keyword">const</span> palindrome = word === [..._letters].reverse().join(<span class="hljs-string">''</span>)

  <span class="hljs-keyword">let</span> initial: number[] | <span class="hljs-literal">null</span> = [<span class="hljs-literal">null</span>, []][+palindrome]
  <span class="hljs-keyword">let</span> index = <span class="hljs-number">0</span>
  <span class="hljs-keyword">let</span> aux = <span class="hljs-number">1</span>

  <span class="hljs-keyword">const</span> letters = [_letters, []][+palindrome]
  <span class="hljs-keyword">let</span> auxLetters = letters.slice(<span class="hljs-number">1</span>)

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> letter <span class="hljs-keyword">of</span> letters) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> auxLetter <span class="hljs-keyword">of</span> auxLetters) {
      <span class="hljs-keyword">const</span> w = [...letters]
      w[index] = auxLetter
      w[aux] = letter

      <span class="hljs-keyword">const</span> isPalindrome = +(w.join(<span class="hljs-string">''</span>) === w.reverse().join(<span class="hljs-string">''</span>))
      <span class="hljs-keyword">const</span> isInitialNull = +(initial == <span class="hljs-literal">null</span>)
      <span class="hljs-keyword">const</span> isDifferentIndex = +(index !== aux)

      <span class="hljs-keyword">const</span> values = [initial, initial, initial, [index, aux]]

      initial = values[isInitialNull + isDifferentIndex + isPalindrome]
      aux++
    }

    index++
    aux = <span class="hljs-number">1</span>
    auxLetters = [[], auxLetters][+(initial == <span class="hljs-literal">null</span>)]
  }

  <span class="hljs-keyword">return</span> initial
}
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/Achalogy/advent-js-2023/blob/main/retos/reto-11/main.ts">Achalogy</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getIndexsForPalindrome</span>(<span class="hljs-params">word</span>) </span>{
  <span class="hljs-keyword">let</span> res: any = <span class="hljs-literal">null</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> a <span class="hljs-keyword">of</span> <span class="hljs-built_in">Array</span>.from({ <span class="hljs-attr">length</span>: word.length }).keys()) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> <span class="hljs-built_in">Array</span>.from({ <span class="hljs-attr">length</span>: word.length }).keys()) {
      <span class="hljs-keyword">let</span> swapped = [...word]
      <span class="hljs-keyword">let</span> aux = word[a]
      swapped[a] = word[b]
      swapped[b] = aux

      <span class="hljs-keyword">let</span> left = swapped.slice(<span class="hljs-number">0</span>, <span class="hljs-built_in">Math</span>.floor(word.length / <span class="hljs-number">2</span>)).join(<span class="hljs-string">""</span>)
      <span class="hljs-keyword">let</span> right = swapped.slice(<span class="hljs-built_in">Math</span>.ceil(word.length / <span class="hljs-number">2</span>)).reverse().join(<span class="hljs-string">""</span>)

      res = [
        [
          <span class="hljs-literal">null</span>
          , [
            []
            , [a, b]
          ][+((a + b) &gt; <span class="hljs-number">0</span>)]
        ][+(left == right)]
        , res
      ][+!!res]
    }
  }
  <span class="hljs-keyword">return</span> res
}
</code></pre>
<p>And that was the challenge for December 11th and its solutions. Give it a like if you enjoyed the challenge or the solution! Do you have an alternative solution? Leave it in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[AdventJS 2023: Day 9 Challenge]]></title><description><![CDATA[The solution to the Challenge #9 of AdventJS 2023
The solution to the previous challenge
The solution to the next challenge
Challenge Description
They are turning on the Christmas lights 🎄 in the city and, as every year, they have to be fixed!
The l...]]></description><link>https://alexvalle.dev/adventjs-2023-day-9-challenge</link><guid isPermaLink="true">https://alexvalle.dev/adventjs-2023-day-9-challenge</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[challenge]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Mon, 11 Dec 2023 00:00:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1702252050321/471ace4d-18f5-4761-a8c8-c98b26239e93.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The solution to the <a target="_blank" href="https://adventjs.dev/challenges/2023/9">Challenge #9</a> of <a target="_blank" href="https://adventjs.dev">AdventJS 2023</a></p>
<p>The solution to <a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-8-challenge">the previous challenge</a></p>
<p>The solution to <a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-10-challenge">the next challenge</a></p>
<h2 id="heading-challenge-description">Challenge Description</h2>
<p>They are turning on the <strong>Christmas lights 🎄</strong> in the city and, as every year, they have to be fixed!</p>
<p>The lights are of two colors: 🔴 and 🟢 . For the effect to be appropriate, <strong>they must always alternate</strong>. That is, if the first light is red, the second must be green, the third red, the fourth green, etc.</p>
<p>We have been asked to write a function <code>adjustLights</code> that, given an array of strings with the color of each light, return the <strong>minimum number</strong> of lights that need to be changed for the colors to alternate.</p>
<pre><code class="lang-javascript">adjustLights([<span class="hljs-string">'🟢'</span>, <span class="hljs-string">'🔴'</span>, <span class="hljs-string">'🟢'</span>, <span class="hljs-string">'🟢'</span>, <span class="hljs-string">'🟢'</span>])
<span class="hljs-comment">// -&gt; 1 (you change the fourth light to 🔴)</span>

adjustLights([<span class="hljs-string">'🔴'</span>, <span class="hljs-string">'🔴'</span>, <span class="hljs-string">'🟢'</span>, <span class="hljs-string">'🟢'</span>, <span class="hljs-string">'🔴'</span>])
<span class="hljs-comment">// -&gt; 2 (you change the second light to 🟢 and the third to 🔴)</span>

adjustLights([<span class="hljs-string">'🟢'</span>, <span class="hljs-string">'🔴'</span>, <span class="hljs-string">'🟢'</span>, <span class="hljs-string">'🔴'</span>, <span class="hljs-string">'🟢'</span>])
<span class="hljs-comment">// -&gt; 0 (they are already alternating)</span>

adjustLights([<span class="hljs-string">'🔴'</span>, <span class="hljs-string">'🔴'</span>, <span class="hljs-string">'🔴'</span>])
<span class="hljs-comment">// -&gt; 1 (you change the second light to 🟢)</span>
</code></pre>
<h2 id="heading-analysis"><strong>Analysis</strong></h2>
<p>For this exercise, the goal is to find the least number of changes necessary for the lights to alternate. The complexity lies not so much in leaving them alternating, but in finding the fewest number of changes to get to that point.</p>
<h3 id="heading-input"><strong>Input</strong></h3>
<ol>
<li>Lights (<code>lights</code>): An array of strings where each color of light goes.</li>
</ol>
<h3 id="heading-output"><strong>Output</strong></h3>
<ul>
<li>The minimum number of movements necessary for the lights to alternate.</li>
</ul>
<h3 id="heading-considerations"><strong>Considerations</strong></h3>
<ul>
<li>It is important to consider that it's not just about alternating the colors, but also finding a way to do it with the least number of movements, as in this case: <code>["🟢", "🔴", "🔴", "🟢", "🔴", "🟢", "🔴", "🟢", "🔴"]</code>. We start with a green, then a red and subsequently another red, if we change that red to green we realize that the next one is also green and we must also change it, in this case to a red, but then another red would follow... and so on, resulting in a <code>7</code>. The point is that if we simply change the first green to a red and the first red to a green, we would have everything solved with only <code>2</code> changes.</li>
</ul>
<h2 id="heading-solution"><strong>Solution</strong></h2>
<p>Considering that the colors must alternate, we can reason that if we arrange the array in such a way, it will start with either the red or green color and alternate from there, so we can evaluate both scenarios and count how many changes would be needed, thus mitigating the problem mentioned in the considerations.</p>
<h2 id="heading-code">Code</h2>
<pre><code class="lang-javascript"><span class="hljs-comment">/**
 * Organizes alternately the lights in an array.
 *
 * <span class="hljs-doctag">@param <span class="hljs-type">{string[]}</span> <span class="hljs-variable">lights</span></span> - The array of lights.
 * <span class="hljs-doctag">@return <span class="hljs-type">{number}</span> </span>Number of changes needed to organize the lights.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">adjustLights</span>(<span class="hljs-params">lights</span>) </span>{
  <span class="hljs-comment">// Initialize variables to count the necessary changes</span>
  <span class="hljs-comment">// both if starting by red or green</span>
  <span class="hljs-keyword">let</span> redCount = <span class="hljs-number">0</span>;
  <span class="hljs-keyword">let</span> greenCount = <span class="hljs-number">0</span>;

  <span class="hljs-comment">// Iterate over each light color in the array</span>
  lights.forEach(<span class="hljs-function">(<span class="hljs-params">light, index</span>) =&gt;</span> {
    <span class="hljs-comment">// Check if the index is even or odd</span>
    <span class="hljs-comment">// this in order to count the changes</span>
    <span class="hljs-comment">// for both cases, red even and green odd</span>
    <span class="hljs-comment">// or green even and red odd</span>
    <span class="hljs-keyword">if</span> (index % <span class="hljs-number">2</span>) {
      <span class="hljs-comment">// We will evaluate the even cases</span>

      <span class="hljs-comment">// If it's green, add +1 to green</span>
      <span class="hljs-keyword">if</span> (light === <span class="hljs-string">"🟢"</span>) greenCount++;

      <span class="hljs-comment">// If it's red, add +1 to red</span>
      <span class="hljs-keyword">if</span> (light === <span class="hljs-string">"🔴"</span>) redCount++;
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-comment">// Otherwise for the odd ones</span>

      <span class="hljs-comment">// If the light is red, we do the opposite and +1 for green</span>
      <span class="hljs-keyword">if</span> (light === <span class="hljs-string">"🔴"</span>) greenCount++;

      <span class="hljs-comment">// If the light is green, +1 for red</span>
      <span class="hljs-keyword">if</span> (light === <span class="hljs-string">"🟢"</span>) redCount++;
    }
  });

  <span class="hljs-comment">// Return the minimum between red and green</span>
  <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.min(redCount, greenCount);
}
</code></pre>
<h3 id="heading-community-solutions"><strong>Community Solutions</strong></h3>
<p>Solution by <a target="_blank" href="https://github.com/Jioh19/adventjs-2023/blob/main/09/script.js">Jioh19</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">adjustLights</span>(<span class="hljs-params">lights</span>) </span>{
  <span class="hljs-keyword">const</span> color = [<span class="hljs-string">'🟢'</span>, <span class="hljs-string">'🔴'</span>];
  <span class="hljs-keyword">let</span> res = <span class="hljs-number">0</span>;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [i, light] <span class="hljs-keyword">of</span> lights.entries()) {
    res += +(light == color[i % <span class="hljs-number">2</span>]);
  }
  <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.min(res, lights.length - res);
}
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/yasai59/adventjs-solutions/blob/main/day9.js">Yasai</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">adjustLights</span>(<span class="hljs-params">lights</span>) </span>{
  <span class="hljs-keyword">const</span> init = lights[<span class="hljs-number">0</span>];
  <span class="hljs-keyword">let</span> rev = <span class="hljs-literal">false</span>;
  <span class="hljs-keyword">let</span> count1 = <span class="hljs-number">0</span>;
  <span class="hljs-keyword">let</span> count2 = <span class="hljs-number">0</span>;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> light <span class="hljs-keyword">of</span> lights) {
    count1 += rev == (light == init);
    rev = !rev;
  }

  rev = <span class="hljs-literal">true</span>;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> light <span class="hljs-keyword">of</span> lights) {
    count2 += rev == (light == init);
    rev = !rev;
  }

  count1 = <span class="hljs-built_in">Math</span>.min(count1, count2);

  <span class="hljs-comment">// Code here</span>
  <span class="hljs-keyword">return</span> count1;
}
</code></pre>
<p>And that was the challenge for December 9th and its solutions. Give a like if you liked the challenge or the solution!<br />Do you have another alternative solution? Leave it in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[AdventJS 2023: Day 10 Challenge]]></title><description><![CDATA[Solution to challenge #10 of AdventJS 2023
Solution to the previous challenge
Solution to the next challenge
Challenge Description
What an idea Sam Elfman has had! He wants to offer a service that creates a customized Christmas tree 🎄 in a matter of...]]></description><link>https://alexvalle.dev/adventjs-2023-day-10-challenge</link><guid isPermaLink="true">https://alexvalle.dev/adventjs-2023-day-10-challenge</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[challenge]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Sun, 10 Dec 2023 06:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1702441770807/06cc27f0-85da-4dd8-9bf2-de8d8985011e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Solution to <a target="_blank" href="https://adventjs.dev/en/challenges/2023/10">challenge #10</a> of <a target="_blank" href="https://adventjs.dev">AdventJS 2023</a></p>
<p><a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-9-challenge">Solution to the previous challenge</a></p>
<p><a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-11-challenge">Solution to the next challenge</a></p>
<h2 id="heading-challenge-description">Challenge Description</h2>
<p>What an idea <em>Sam Elfman</em> has had! He wants to offer a service that creates a <strong>customized Christmas tree 🎄</strong> in a matter of seconds.</p>
<p>To create it, we are given a <strong>string to form the tree</strong> and a <strong>number that indicates its height</strong>.</p>
<p>Each character of the string represents an ornament of the tree, and we use them <strong>cyclically</strong> until we reach the indicated height. At <strong>least, they will always pass us one</strong>.</p>
<p>We must return a multiline <strong>string</strong> with the Christmas tree made with the ornaments, the indicated height <strong>plus a final line with the trunk formed by the character</strong> <code>|</code> in the center and, finally, a new line <code>\n</code>.</p>
<p>For example, if we receive the string "123" and the number <code>4</code> as height, we would have to build this tree:</p>
<pre><code class="lang-javascript">   <span class="hljs-number">1</span>
  <span class="hljs-number">2</span> <span class="hljs-number">3</span>
 <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span>
<span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">1</span>
   |
</code></pre>
<p>If we receive the string <code>*@o</code> and the number <code>3</code>, the tree we should return is:</p>
<pre><code class="lang-javascript">  *
 @ o
* @ o
  |
</code></pre>
<p>Note:</p>
<ul>
<li><p>The tree should always be centered, for that reason add blank spaces to the left of each line.</p>
</li>
<li><p>Create spaces only to the left of each line of the tree. Do not leave blank spaces to the right.</p>
</li>
<li><p>The ornaments have a white space between them for separation.</p>
</li>
</ul>
<h2 id="heading-analysis">Analysis</h2>
<p>And we're back to drawing! This time it's Christmas trees, which we must draw using the strings passed as parameters and sizing them as indicated.</p>
<h3 id="heading-input">Input</h3>
<ol>
<li><p>Ornaments (<code>ornaments</code>): A string containing the character sequence that will fill the tree.</p>
</li>
<li><p>Height (<code>height</code>): The height and maximum width of the tree.</p>
</li>
</ol>
<h3 id="heading-output">Output</h3>
<ul>
<li>A string with the tree's drawing.</li>
</ul>
<h3 id="heading-considerations">Considerations</h3>
<ul>
<li><p>We must add spaces at the beginning to give it the shape of a Christmas tree.</p>
</li>
<li><p>At the end, we must insert a line break.</p>
</li>
</ul>
<h2 id="heading-solution">Solution</h2>
<p>A simple way to complete the challenge is to calculate the total number of characters required, for which we can use mathematics. Once we have this, we can iterate and fill each row of the tree.</p>
<h3 id="heading-code">Code</h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createChristmasTree</span>(<span class="hljs-params">ornaments, height</span>) </span>{
  <span class="hljs-keyword">let</span> tree = <span class="hljs-string">""</span>;
  <span class="hljs-keyword">const</span> totalOrnaments = (height * (height + <span class="hljs-number">1</span>)) / <span class="hljs-number">2</span>;
  <span class="hljs-keyword">const</span> allOrnaments = ornaments.repeat(totalOrnaments);

  <span class="hljs-keyword">let</span> lastIndex = <span class="hljs-number">0</span>;

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">1</span>; i &lt;= height; i += <span class="hljs-number">1</span>) {
    <span class="hljs-keyword">const</span> spaces = <span class="hljs-string">" "</span>.repeat(height - i);
    <span class="hljs-keyword">const</span> ornamentRow = allOrnaments
      .slice(lastIndex, lastIndex + i)
      .split(<span class="hljs-string">""</span>)
      .join(<span class="hljs-string">" "</span>);

    tree += spaces + ornamentRow + <span class="hljs-string">"\n"</span>;

    lastIndex += i;
  }

  tree += <span class="hljs-string">"|"</span>.padStart(height, <span class="hljs-string">" "</span>) + <span class="hljs-string">"\n"</span>;
  <span class="hljs-keyword">return</span> tree;
}
</code></pre>
<h3 id="heading-community-solutions">Community Solutions</h3>
<p>Solution by <a target="_blank" href="https://github.com/iswilljr/adventjs/blob/master/2023/challenge-10/challenge-10.ts">iswilljr</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createChristmasTree</span>(<span class="hljs-params">ornaments, height</span>) </span>{
  <span class="hljs-keyword">let</span> result = <span class="hljs-string">''</span>
  <span class="hljs-keyword">const</span> spaces = <span class="hljs-string">' '</span>.repeat(height - <span class="hljs-number">1</span>)

  <span class="hljs-keyword">const</span> ornamentsArray = [...ornaments.repeat(height)].join(<span class="hljs-string">' '</span>)
  <span class="hljs-keyword">let</span> currentPosition = <span class="hljs-number">0</span>

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> index <span class="hljs-keyword">of</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(height).keys()) {
    <span class="hljs-keyword">const</span> length = index + <span class="hljs-number">1</span>
    <span class="hljs-keyword">const</span> total = currentPosition + length

    result +=
      spaces.slice(index) +
      ornamentsArray.slice(currentPosition * <span class="hljs-number">2</span>, total * <span class="hljs-number">2</span> - <span class="hljs-number">1</span>) +
      <span class="hljs-string">'\n'</span>

    currentPosition = total % ornaments.length
  }

  <span class="hljs-keyword">return</span> result + spaces + <span class="hljs-string">'|\n'</span>
}
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/yasai">yasai</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createChristmasTree</span>(<span class="hljs-params">ornaments, height</span>) </span>{
    <span class="hljs-keyword">let</span> tree = <span class="hljs-string">""</span>;
    <span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;

    <span class="hljs-comment">// pyramid</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; height; i++) {
      <span class="hljs-keyword">let</span> line = <span class="hljs-string">""</span>;
      <span class="hljs-comment">// spaces</span>
      line += <span class="hljs-string">" "</span>.repeat(height - i - <span class="hljs-number">1</span>);
      <span class="hljs-comment">// ornaments</span>
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">0</span>; j &lt; i + <span class="hljs-number">1</span>; j++) {
        <span class="hljs-keyword">let</span> dec = ornaments[count];
        count++;
        <span class="hljs-keyword">if</span> (count == ornaments.length) count = <span class="hljs-number">0</span>;
         <span class="hljs-keyword">if</span> (j == i) {
          line += <span class="hljs-string">`<span class="hljs-subst">${dec}</span>`</span>;
          <span class="hljs-keyword">break</span>;
        }
        line += <span class="hljs-string">`<span class="hljs-subst">${dec}</span> `</span>;
      }
      <span class="hljs-comment">// add to the tree</span>
      tree += line + <span class="hljs-string">"\n"</span>;
    }

    <span class="hljs-comment">// trunk</span>
    tree += <span class="hljs-string">" "</span>.repeat(height - <span class="hljs-number">1</span>) + <span class="hljs-string">"|\n"</span>;

    <span class="hljs-keyword">return</span> tree;
  }
</code></pre>
<p>And that was the challenge for December 10th and its solutions. Give it a like if you enjoyed the challenge or the solution! Do you have an alternative solution? Leave it in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[AdventJS 2023: Day 8 Challenge]]></title><description><![CDATA[The solution to the Challenge #8 of AdventJS 2023
The solution to the previous challenge
The solution to the next challenge
Challenge Description
The elves are very busy in Santa Claus' workshop organizing gifts 🎁 for Christmas Eve 🎄.
The input for...]]></description><link>https://alexvalle.dev/adventjs-2023-day-8-challenge</link><guid isPermaLink="true">https://alexvalle.dev/adventjs-2023-day-8-challenge</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[challenge]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Sun, 10 Dec 2023 05:17:38 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1702184961627/60499cd9-d7ef-40c8-8153-f5a2cf947ce3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The solution to the <a target="_blank" href="https://adventjs.dev/challenges/2023/8">Challenge #8</a> of <a target="_blank" href="https://adventjs.dev">AdventJS 2023</a></p>
<p>The solution to <a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-7-challenge">the previous challenge</a></p>
<p>The solution to <a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-9-challenge">the next challenge</a></p>
<h2 id="heading-challenge-description">Challenge Description</h2>
<p>The elves are very busy in Santa Claus' workshop organizing gifts 🎁 for Christmas Eve 🎄.</p>
<p>The input format is special, as it indicates the number of gifts and the type of gift with letters from <code>a</code> to <code>z</code>. For example, <code>'66a11b'</code> means 66 <code>a</code> gifts and 11 <code>b</code> gifts.</p>
<p>The elves have a <strong>special system</strong> for organizing the gifts:</p>
<ul>
<li><p>Every 10 gifts of the same type are packed in a box, represented by <code>{x}</code>. For example, 20 type <code>a</code> gifts are packed in two boxes like this: <code>{a}{a}</code>.</p>
</li>
<li><p>Every 5 boxes are stacked on a pallet, represented by <code>[x]</code>. For example, 10 <code>a</code> boxes are stacked on 2 pallets in this way: <code>[a][a]</code></p>
</li>
<li><p>Any additional gift is placed in a bag, represented by <code>()</code> and all of them are placed inside. For example, 4 <code>b</code> gifts are placed in a bag like this <code>(bbbb)</code></p>
</li>
</ul>
<p><strong>The gifts are then placed in the following order</strong>: <em>pallets, boxes and bags</em>. The gifts appear in the same order as the input string.</p>
<p>Your task is to write a function <code>organizeGifts</code> takes a string of gifts as an argument and returns a string representing the warehouse.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> result1 = organizeGifts(<span class="hljs-string">'76a11b'</span>)
<span class="hljs-built_in">console</span>.log(result1)
<span class="hljs-comment">// `[a]{a}{a}(aaaaaa){b}(b)`</span>

<span class="hljs-comment">/* Explanation:

  76a: 76 gifts type 'a' would be packed in 7 boxes and 6 gifts would be left, resulting in 1 pallet [a] (for the first 5 boxes), 2 loose boxes {a}{a} and a bag with 6 gifts (aaaaaa)

  11b: 11 gifts type 'b' would be packed in 1 box and 1 gift would be left, resulting in 1 loose box {b} and a bag with 1 gift (b)
*/</span>
</code></pre>
<h2 id="heading-analysis"><strong>Analysis</strong></h2>
<p>The goal is to break down the string into parts, to divide the quantities of each gift, and once that is done, they can be packaged as marked by the requirements.</p>
<h3 id="heading-input"><strong>Input</strong></h3>
<ol>
<li>Gifts (<code>gifts</code>): A string with the quantity of each type of gift.</li>
</ol>
<h3 id="heading-output"><strong>Output</strong></h3>
<ul>
<li>A string with the packaged gifts.</li>
</ul>
<h3 id="heading-considerations"><strong>Considerations</strong></h3>
<ul>
<li>The return value should be in the same order as the input, following the order of the gifts.</li>
</ul>
<h2 id="heading-solution"><strong>Solution</strong></h2>
<p>We can take the input string and split it between the number of gifts of each type, using RegExp. Once that is done, we iterate through each of them and proceed to divide to find the number of pallets, boxes, and bags.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">/**
 * Organizes a list of gifts.
 *
 * <span class="hljs-doctag">@param <span class="hljs-type">{string}</span> <span class="hljs-variable">gifts</span></span> - The list of gifts to be organized.
 * <span class="hljs-doctag">@returns <span class="hljs-type">{string}</span> </span>- The organized representation of the gifts.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">organizeGifts</span>(<span class="hljs-params">gifts</span>) </span>{
  <span class="hljs-keyword">let</span> res = <span class="hljs-string">""</span>;
  <span class="hljs-keyword">const</span> giftsSplit = gifts.match(<span class="hljs-regexp">/\d+[a-z]/g</span>);

  <span class="hljs-comment">// Iterate over each gift in the giftsSplit array</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> gift <span class="hljs-keyword">of</span> giftsSplit) {
    <span class="hljs-keyword">const</span> quantity = <span class="hljs-built_in">Number</span>(gift.slice(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>));
    <span class="hljs-keyword">const</span> giftType = gift.charAt(gift.length - <span class="hljs-number">1</span>);

    <span class="hljs-comment">// Calculate the number of pallets needed</span>
    <span class="hljs-keyword">const</span> pallets = <span class="hljs-string">`[<span class="hljs-subst">${giftType}</span>]`</span>.repeat(<span class="hljs-built_in">Math</span>.floor(quantity / <span class="hljs-number">50</span>));

    <span class="hljs-comment">// Calculate the remainder after packing in pallets</span>
    <span class="hljs-keyword">const</span> remainder = quantity % <span class="hljs-number">50</span>;

    <span class="hljs-comment">// Calculate the number of boxes needed</span>
    <span class="hljs-keyword">const</span> boxes = <span class="hljs-string">`{<span class="hljs-subst">${giftType}</span>}`</span>.repeat(<span class="hljs-built_in">Math</span>.floor(remainder / <span class="hljs-number">10</span>));

    <span class="hljs-comment">// Calculate the remainder gifts after packing in boxes</span>
    <span class="hljs-keyword">const</span> remainderGifts = giftType.repeat(remainder % <span class="hljs-number">10</span>);

    <span class="hljs-comment">// Check if there are remainder gifts and create a bag</span>
    <span class="hljs-keyword">const</span> bags = remainderGifts &amp;&amp; <span class="hljs-string">`(<span class="hljs-subst">${remainderGifts}</span>)`</span>;

    <span class="hljs-comment">// Concatenate the pallets, boxes, and bags for the current gift</span>
    res += pallets + boxes + bags;
  }

  <span class="hljs-comment">// Return the organized representation of the gifts</span>
  <span class="hljs-keyword">return</span> res;
}
</code></pre>
<h3 id="heading-community-solutions"><strong>Community Solutions</strong></h3>
<p>Solution by <a target="_blank" href="https://github.com/cristianstu">cristianstu</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">organizeGifts</span>(<span class="hljs-params">gifts</span>) </span>{
  <span class="hljs-keyword">const</span> matches = [...gifts.matchAll(<span class="hljs-regexp">/(\d+)([a-z])/g</span>)];

  <span class="hljs-keyword">return</span> matches.map(<span class="hljs-function">(<span class="hljs-params">[,qtty, letter]</span>) =&gt;</span> {
    qtty = +qtty

    <span class="hljs-keyword">const</span> pales = <span class="hljs-built_in">Math</span>.trunc(qtty / <span class="hljs-number">50</span>)
    <span class="hljs-keyword">const</span> boxes = <span class="hljs-built_in">Math</span>.trunc((qtty - (<span class="hljs-number">50</span> * pales)) / <span class="hljs-number">10</span>)
    <span class="hljs-keyword">const</span> bags = qtty - (pales * <span class="hljs-number">50</span>) - (boxes * <span class="hljs-number">10</span>)

    <span class="hljs-keyword">return</span> <span class="hljs-string">`[<span class="hljs-subst">${letter}</span>]`</span>.repeat(pales) +
           <span class="hljs-string">`{<span class="hljs-subst">${letter}</span>}`</span>.repeat(boxes) +
           <span class="hljs-string">`(<span class="hljs-subst">${letter}</span>)`</span>.repeat(bags).replace(<span class="hljs-regexp">/\)\(/g</span>, <span class="hljs-string">''</span>)

  }).join(<span class="hljs-string">''</span>);
}
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/jamerrq/advent-js-2023/blob/master/src/challenges/08.ts">jamerrq</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">organizeGifts</span> (<span class="hljs-params">gifts: string</span>): <span class="hljs-title">string</span> </span>{
  <span class="hljs-keyword">const</span> giftsRegex = <span class="hljs-regexp">/(\d+)([a-z])/g</span>
  <span class="hljs-keyword">const</span> giftsMatches = gifts.matchAll(giftsRegex)
  <span class="hljs-keyword">let</span> ans = <span class="hljs-string">''</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> match <span class="hljs-keyword">of</span> giftsMatches) {
    <span class="hljs-keyword">const</span> [, qty, symbol] = match
    <span class="hljs-keyword">let</span> left = +qty
    ans = <span class="hljs-string">`<span class="hljs-subst">${ans}</span><span class="hljs-subst">${<span class="hljs-string">`[<span class="hljs-subst">${symbol}</span>]`</span>.repeat((left - (left % <span class="hljs-number">50</span>)) / <span class="hljs-number">50</span>)}</span>`</span>
    left -= <span class="hljs-number">50</span> * ((left - (left % <span class="hljs-number">50</span>)) / <span class="hljs-number">50</span>)
    ans = <span class="hljs-string">`<span class="hljs-subst">${ans}</span><span class="hljs-subst">${<span class="hljs-string">`{<span class="hljs-subst">${symbol}</span>}`</span>.repeat((left - (left % <span class="hljs-number">10</span>)) / <span class="hljs-number">10</span>)}</span>`</span>
    left -= <span class="hljs-number">10</span> * (left - (left % <span class="hljs-number">10</span>)) / <span class="hljs-number">10</span>
    <span class="hljs-keyword">const</span> x = ((left - <span class="hljs-number">1</span>) + ((left - <span class="hljs-number">1</span>) &gt;&gt; <span class="hljs-number">31</span>)) ^ ((left - <span class="hljs-number">1</span>) &gt;&gt; <span class="hljs-number">31</span>)
    ans = <span class="hljs-string">`<span class="hljs-subst">${ans}</span><span class="hljs-subst">${<span class="hljs-string">`(<span class="hljs-subst">${symbol.repeat(left)}</span>)`</span>.repeat((left + <span class="hljs-number">1</span> - x) / <span class="hljs-number">2</span>)}</span>`</span>
  }
  <span class="hljs-keyword">return</span> ans
}
</code></pre>
<p>And that was the challenge for December 8th and its solutions. Give a like if you liked the challenge or the solution! Do you have another alternative solution? Leave it in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[AdventJS 2023: Day 7 Challenge]]></title><description><![CDATA[The solution to the Challenge #7 of AdventJS 2023
The solution to the previous challenge
The solution to the next challenge
Challenge Description
Santa is experimenting with new gift designs and he needs your help to visualize them in 3D.
Your task i...]]></description><link>https://alexvalle.dev/adventjs-2023-day-7-challenge</link><guid isPermaLink="true">https://alexvalle.dev/adventjs-2023-day-7-challenge</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[challenge]]></category><category><![CDATA[Node.js]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Thu, 07 Dec 2023 06:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1702097148145/7ffa81d8-71b8-41c8-9cd7-6c8758109e4e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The solution to the <a target="_blank" href="https://adventjs.dev/challenges/2023/7">Challenge #7</a> of <a target="_blank" href="https://adventjs.dev/">AdventJS 2023</a></p>
<p>The solution to the <a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-6-challenge">previous challenge</a></p>
<p>The solution to the <a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-8-challenge">next challenge</a></p>
<h2 id="heading-challenge-description">Challenge Description</h2>
<p>Santa is experimenting with new gift designs and <strong>he needs your help to visualize them in 3D</strong>.</p>
<p>Your task is to write a function that, given a size <code>n</code> (integer), <strong>generates a drawing of a 3D gift</strong> using ASCII characters.</p>
<p>The lines of the gifts are drawn with <code>#</code> and the faces with the symbol passed to us as a parameter:</p>
<pre><code class="lang-javascript">drawGift(<span class="hljs-number">4</span>, <span class="hljs-string">'+'</span>)

<span class="hljs-comment">/*
   ####
  #++##
 #++#+#
####++#
#++#+#
#++##
####
*/</span>

drawGift(<span class="hljs-number">5</span>, <span class="hljs-string">'*'</span>)
<span class="hljs-comment">/*
    #####
   #***##
  #***#*#
 #***#**#
#####***#
#***#**#
#***#*#
#***##
#####
*/</span>

drawGift(<span class="hljs-number">1</span>, <span class="hljs-string">'^'</span>)
<span class="hljs-comment">/*
#
*/</span>
</code></pre>
<p>Important: We have been told that <strong>there is always a new line at the end of the drawing</strong>.</p>
<h2 id="heading-analysis"><strong>Analysis</strong></h2>
<p>In this challenge, you have to draw gifts based on the parameters given by the exercise, in the pure style of ASCII Art. In case you haven't fully understood it, here's an example:</p>
<pre><code class="lang-plaintext">    #####
   #***##
  #***#*#
 #***#**#
#####***#
#***#**#
#***#*#
#***##
#####
</code></pre>
<p>The above is a 3D rectangle</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1702096877455/68783b30-13f0-4f9a-8677-abc101a34dba.png" alt class="image--center mx-auto" /></p>
<p>Where <code>#</code> represents the outline of the rectangle, that is the lines, while the symbol they pass us as a parameter (in this case <code>*</code>) represents the face or surface of the rectangle.</p>
<h3 id="heading-inputs"><strong>Inputs</strong></h3>
<ol>
<li><p>Size (<code>size</code>): A number representing the size of the gift, it is the length in characters of each line of the gift.</p>
</li>
<li><p>Symbol (<code>symbol</code>): A string that will fill the gift, basically fills each face of the rectangle.</p>
</li>
</ol>
<h3 id="heading-output"><strong>Output</strong></h3>
<ul>
<li>A string that will look like the drawing of the requested gift.</li>
</ul>
<h3 id="heading-considerations"><strong>Considerations</strong></h3>
<ul>
<li><p>Always leave a line break at the end of the drawing.</p>
</li>
<li><p>Take into account the spaces in the first half of the drawing, they are important to shape the gift.</p>
</li>
</ul>
<h2 id="heading-solution"><strong>Solution</strong></h2>
<p>The exercise itself is not very complex, the tricky part can be managing the string to draw, but JavaScript offers enough methods for it.</p>
<h3 id="heading-code"><strong>Code</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">drawGift</span>(<span class="hljs-params">size, symbol</span>) </span>{
  <span class="hljs-comment">// The space at the beginning of the gift</span>
  <span class="hljs-keyword">let</span> space = size;

  <span class="hljs-comment">// Initialize the first part of the gift</span>
  <span class="hljs-comment">// the space at the beginning of the gift</span>
  <span class="hljs-comment">// and concatenate # with the size of the gift</span>
  <span class="hljs-keyword">let</span> firstPart = <span class="hljs-string">" "</span>.repeat(--space) + <span class="hljs-string">"#"</span>.repeat(size) + <span class="hljs-string">"\n"</span>;
  <span class="hljs-comment">// if the size is 1 return the first part `#`</span>
  <span class="hljs-keyword">if</span> (size === <span class="hljs-number">1</span>) {
    <span class="hljs-keyword">return</span> firstPart;
  }

  <span class="hljs-comment">// Initialize the second part of the gift</span>
  <span class="hljs-comment">//this part is after the half of the gift</span>
  <span class="hljs-keyword">let</span> secondPart = <span class="hljs-string">""</span>;
  <span class="hljs-comment">// loop through the size of the gift</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">1</span>; i &lt; size - <span class="hljs-number">1</span>; i += <span class="hljs-number">1</span>) {
    <span class="hljs-comment">// start with the space at the beginning of the gift</span>
    firstPart +=
      <span class="hljs-string">" "</span>.repeat(--space) +
      <span class="hljs-comment">// add `#` and the symbol with the size of the gift - 1</span>
      <span class="hljs-string">"#"</span>.padEnd(size - <span class="hljs-number">1</span>, symbol) +
      <span class="hljs-comment">// add `#` and fill the space with the symbol</span>
      <span class="hljs-string">"#"</span>.padEnd(i + <span class="hljs-number">1</span>, symbol) +
      <span class="hljs-comment">// add the break line</span>
      <span class="hljs-string">"#\n"</span>;

    <span class="hljs-comment">// The second part is the same but reversed</span>
    secondPart +=
      <span class="hljs-string">"#"</span> + symbol.repeat(size - <span class="hljs-number">2</span>) + <span class="hljs-string">"#"</span>.padEnd(size - i, symbol) + <span class="hljs-string">"#\n"</span>;
  }

  <span class="hljs-comment">// half is `#` with the size of the gift and the symbol with the size of the gift - 2</span>
  <span class="hljs-keyword">const</span> half = <span class="hljs-string">"#"</span>.repeat(size) + symbol.repeat(size - <span class="hljs-number">2</span>) + <span class="hljs-string">"#\n"</span>;

  <span class="hljs-comment">// return the first part, the half and the second part</span>
  <span class="hljs-keyword">return</span> firstPart + half + secondPart + <span class="hljs-string">"#"</span>.repeat(size) + <span class="hljs-string">"\n"</span>;
}
</code></pre>
<h3 id="heading-community-solutions"><strong>Community Solutions</strong></h3>
<p>Solution by <a target="_blank" href="https://github.com/marcode24/adventjs-solutions/blob/main/2023/07-las-cajas-en-3d/index.js">marcode24</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> drawGift = <span class="hljs-function">(<span class="hljs-params">size, symbol</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> WRAPPER = <span class="hljs-string">'#'</span>;
  <span class="hljs-keyword">const</span> SPACE = <span class="hljs-string">' '</span>;

  <span class="hljs-keyword">if</span> (size &lt;= <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${WRAPPER}</span>\n`</span>;

  <span class="hljs-keyword">const</span> top = [SPACE.repeat(size - <span class="hljs-number">1</span>) + WRAPPER.repeat(size)];
  <span class="hljs-keyword">const</span> bottom = [<span class="hljs-string">`<span class="hljs-subst">${WRAPPER.repeat(size)}</span>`</span>];
  <span class="hljs-keyword">const</span> middle = <span class="hljs-string">`<span class="hljs-subst">${WRAPPER.repeat(size)}</span><span class="hljs-subst">${symbol.repeat(<span class="hljs-built_in">Math</span>.abs(size - <span class="hljs-number">2</span>))}</span>`</span>
    + <span class="hljs-string">`<span class="hljs-subst">${WRAPPER}</span>\n`</span>;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">1</span>; i &lt; size; i++) {
    <span class="hljs-keyword">const</span> line = <span class="hljs-string">`<span class="hljs-subst">${WRAPPER}</span><span class="hljs-subst">${symbol.repeat(size - <span class="hljs-number">2</span>)}</span><span class="hljs-subst">${WRAPPER}</span>`</span>
      + <span class="hljs-string">`<span class="hljs-subst">${symbol.repeat(i - <span class="hljs-number">1</span>)}</span><span class="hljs-subst">${WRAPPER}</span>`</span>;
    top.push(SPACE.repeat(size - i - <span class="hljs-number">1</span>) + line);
    bottom.push(line);
  }

  top.pop();
  bottom.pop();
  top.push(middle);
  bottom.reverse();
  <span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${top.join(<span class="hljs-string">'\n'</span>)}</span><span class="hljs-subst">${bottom.join(<span class="hljs-string">'\n'</span>)}</span>\n`</span>;
};
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/Achalogy/advent-js-2023/blob/main/retos/reto-7/main.ts">Achalogy</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">drawGift</span>(<span class="hljs-params">size, symbol</span>) </span>{
  <span class="hljs-keyword">let</span> bgSize = size - <span class="hljs-number">2</span>

  bgSize += +!(bgSize + <span class="hljs-number">1</span>)

  <span class="hljs-keyword">let</span> response = <span class="hljs-string">""</span>

  <span class="hljs-keyword">let</span> topCenter = <span class="hljs-string">""</span>
  <span class="hljs-keyword">let</span> bottomCenter = <span class="hljs-string">""</span>

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> a <span class="hljs-keyword">of</span> [...Array.from({ <span class="hljs-attr">length</span>: bgSize }).keys()]) {
    <span class="hljs-keyword">const</span> c = <span class="hljs-string">"#"</span>
      + symbol.repeat(bgSize)
      + <span class="hljs-string">"#"</span> + symbol.repeat(a) + <span class="hljs-string">"#"</span>
    bottomCenter = c + <span class="hljs-string">"\n"</span> + bottomCenter
    topCenter += <span class="hljs-string">" "</span>.repeat(bgSize - a) + c + <span class="hljs-string">"\n"</span>
  }

  response = <span class="hljs-string">" "</span>.repeat(size - <span class="hljs-number">1</span>) + <span class="hljs-string">"#"</span>.repeat(size) + <span class="hljs-string">"\n"</span>
    + (topCenter
      + <span class="hljs-string">"#"</span>.repeat(size) + symbol.repeat(bgSize) + <span class="hljs-string">"#"</span> + <span class="hljs-string">"\n"</span>
      + bottomCenter
      + <span class="hljs-string">"#"</span>.repeat(size) + <span class="hljs-string">"\n"</span>).repeat(+!!(size - <span class="hljs-number">1</span>))

  <span class="hljs-keyword">return</span> response
}
</code></pre>
<p>And that was the challenge for December 7th and its solutions. Do you have another alternative solution? Leave it in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[AdventJS 2023: Day 6 Challenge]]></title><description><![CDATA[Solution to the Challenge #6 of AdventJS 2023
Solution to the Solution to the next challengeprevious challenge
Solution to the next challenge
Challenge Description
The elves are cataloging Santa's reindeer 🦌 based on the distance they can travel.
Fo...]]></description><link>https://alexvalle.dev/adventjs-2023-day-6-challenge</link><guid isPermaLink="true">https://alexvalle.dev/adventjs-2023-day-6-challenge</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Wed, 06 Dec 2023 06:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1701978008547/60996d82-32c6-4635-8f6e-a3b36b1dc228.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Solution to the <a target="_blank" href="https://adventjs.dev/challenges/2023/6">Challenge #6</a> of <a target="_blank" href="https://adventjs.dev">AdventJS 2023</a></p>
<p><a target="_blank" href="https://dev.to/fenriuz/adventjs-2023-reto-del-dia-5-1dpk">Solution to the</a> <a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-5-challenge">Solution to the next challenge</a><a target="_blank" href="https://dev.to/fenriuz/adventjs-2023-reto-del-dia-5-1dpk">previous challenge</a></p>
<p><a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-7-challenge">Solution to the next challenge</a></p>
<h2 id="heading-challenge-description">Challenge Description</h2>
<p>The elves are cataloging Santa's reindeer 🦌 based on the distance they can travel.</p>
<p>For this, they have a text string <code>movements</code> where each character represents the direction of the reindeer's movement:</p>
<ul>
<li><p><code>&gt;</code> = Moves to the right</p>
</li>
<li><p><code>&lt;</code> = Moves to the left</p>
</li>
<li><p><code>*</code> = Can move forward or backward</p>
</li>
</ul>
<p>For example, if the movement is <code>&gt;&gt;*&lt;</code>, it goes to the right twice, then it can go either left or right (whichever maximizes the final traveled distance) and then left.</p>
<p>The elves want to know what the maximum distance the reindeer travels is <strong>after completing all movements.</strong></p>
<p><strong>In the previous example, the maximum distance the reindeer travels is</strong> <code>2</code>. It goes to the right twice <code>+2</code>, then with the <code>*</code> it can go to the right again to maximize the distance <code>+1</code> and then it goes to the left <code>-1</code>.</p>
<p>Create a <code>maxDistance</code> function that takes the text string <code>movements</code> and returns <strong>the maximum distance</strong> that the reindeer can travel <strong>in any direction</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> movements = <span class="hljs-string">'&gt;&gt;*&lt;'</span>
<span class="hljs-keyword">const</span> result = maxDistance(movements)
<span class="hljs-built_in">console</span>.log(result) <span class="hljs-comment">// -&gt; 2</span>

<span class="hljs-keyword">const</span> movements2 = <span class="hljs-string">'&lt;&lt;&lt;&gt;'</span>
<span class="hljs-keyword">const</span> result2 = maxDistance(movements2)
<span class="hljs-built_in">console</span>.log(result2) <span class="hljs-comment">// -&gt; 2</span>

<span class="hljs-keyword">const</span> movements3 = <span class="hljs-string">'&gt;***&gt;'</span>
<span class="hljs-keyword">const</span> result3 = maxDistance(movements3)
<span class="hljs-built_in">console</span>.log(result3) <span class="hljs-comment">// -&gt; 5</span>
</code></pre>
<p>Keep in mind that it doesn't matter whether it is to the left or right, the distance is <strong>the absolute value of the maximum distance traveled at the end of the movements</strong>.</p>
<p>Here's the translation of your blog from Spanish to English, leaving the code and links untouched:</p>
<hr />
<p>Solution to the <a target="_blank" href="https://adventjs.dev/es/challenges/2023/6">Challenge #6</a> of <a target="_blank" href="https://adventjs.dev/es">AdventJS 2023</a> <a target="_blank" href="https://dev.to/fenriuz/adventjs-2023-reto-del-dia-5-1dpk">Solution to the previous challenge</a> <a target="_blank" href="https://dev.to/fenriuz">Solution to the next challenge</a></p>
<p>{%- # TOC start (generated with <a target="_blank" href="https://adventjs.dev/es/challenges/2023/6">https://github.com/derlin/bitdowntoc</a>) -%}</p>
<ul>
<li><p><a target="_blank" href="https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#challenge-description">Challenge Description</a></p>
</li>
<li><p><a target="_blank" href="https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#analysis">Analysis</a></p>
<ul>
<li><p><a target="_blank" href="https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#inputs">Inputs</a></p>
</li>
<li><p><a target="_blank" href="https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#output">Output</a></p>
</li>
<li><p><a target="_blank" href="https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#considerations">Considerations</a></p>
</li>
</ul>
</li>
<li><p><a target="_blank" href="https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#solution">Solution</a></p>
<ul>
<li><p><a target="_blank" href="https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#code">Code</a></p>
</li>
<li><p><a target="_blank" href="https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#community-solutions">Community Solutions</a></p>
</li>
</ul>
</li>
</ul>
<p>{%- # TOC end -%}</p>
<h2 id="heading-challenge-description-1"><strong>Challenge Description</strong></h2>
<p>The elves are cataloging Santa's reindeer 🦌 based on the distance they can travel.</p>
<p>For this, they have a string of text movements where each character represents the direction of the reindeer's movement:</p>
<ul>
<li><p><code>&gt;</code> = Moves to the right</p>
</li>
<li><p><code>&lt;</code> = Moves to the left</p>
</li>
<li><p><em>\= Can move forward or backward For example, if the movement is &gt;&gt;</em>&lt;, it goes to the right twice, then can go right or left (whichever maximizes the final traveled distance), and then goes to the left.</p>
</li>
</ul>
<p>The elves want to know what is the maximum distance the reindeer travels at the end of all movements.</p>
<p>In the example above, the maximum distance the reindeer travels is 2. It goes to the right two times +2, then with the * can go to the right again to maximize the distance +1, and then goes to the left -1.</p>
<p>Create a function maxDistance that receives the text string movements and returns the maximum distance the reindeer can travel in any direction:</p>
<pre><code class="lang-javascript">javascriptCopy codeconst movements = <span class="hljs-string">'&gt;&gt;*&lt;'</span>
<span class="hljs-keyword">const</span> result = maxDistance(movements)
<span class="hljs-built_in">console</span>.log(result) <span class="hljs-comment">// -&gt; 2</span>

<span class="hljs-keyword">const</span> movements2 = <span class="hljs-string">'&lt;&lt;&lt;&gt;'</span>
<span class="hljs-keyword">const</span> result2 = maxDistance(movements2)
<span class="hljs-built_in">console</span>.log(result2) <span class="hljs-comment">// -&gt; 2</span>

<span class="hljs-keyword">const</span> movements3 = <span class="hljs-string">'&gt;***&gt;'</span>
<span class="hljs-keyword">const</span> result3 = maxDistance(movements3)
<span class="hljs-built_in">console</span>.log(result3) <span class="hljs-comment">// -&gt; 5</span>
</code></pre>
<p>Keep in mind that it doesn't matter if it's to the left or right, the distance is the absolute value of the maximum traveled distance at the end of the movements.</p>
<h2 id="heading-analysis"><strong>Analysis</strong></h2>
<p>The goal is to find the maximum distance that the reindeers can travel. It doesn't matter if they travel it to the left or right, the point is to find the maximum distance.</p>
<h3 id="heading-inputs"><strong>Inputs</strong></h3>
<ol>
<li>Movements (<code>movements</code>): A string with the movements, where each character represents the direction of the reindeer's movement.</li>
</ol>
<h3 id="heading-output"><strong>Output</strong></h3>
<ul>
<li>The number of the maximum distance that can be reached with those movements</li>
</ul>
<h3 id="heading-considerations"><strong>Considerations</strong></h3>
<ul>
<li>The maximum distance must be found regardless of whether the movement is to the right or left.</li>
</ul>
<h2 id="heading-solution"><strong>Solution</strong></h2>
<p>Solving this exercise is not that complicated and, as always, we have many paths and alternatives. A simple approach is to count how many times each movement is repeated and then do the final sum to find the result.</p>
<h3 id="heading-code"><strong>Code</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-comment">/**
 * Calculates the maximum distance based on the given movements.
 *
 * <span class="hljs-doctag">@param <span class="hljs-type">{string}</span> <span class="hljs-variable">movements</span></span> - A string representing the movements.
 * <span class="hljs-doctag">@returns <span class="hljs-type">{number}</span> </span>The maximum distance.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">maxDistance</span>(<span class="hljs-params">movements</span>) </span>{
    <span class="hljs-comment">// Initialize the directions object with default values</span>
    <span class="hljs-keyword">const</span> directions = {
        <span class="hljs-string">"&lt;"</span>: <span class="hljs-number">0</span>, <span class="hljs-comment">// Represents left movement</span>
        <span class="hljs-string">"&gt;"</span>: <span class="hljs-number">0</span>, <span class="hljs-comment">// Represents right movement</span>
        <span class="hljs-string">"*"</span>: <span class="hljs-number">0</span>, <span class="hljs-comment">// Represents movement in any direction</span>
    };

    <span class="hljs-comment">// Count the occurrences of each movement</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> movement <span class="hljs-keyword">of</span> movements) {
        directions[movement] += <span class="hljs-number">1</span>;
    }

    <span class="hljs-comment">// Calculate the maximum distance</span>
    <span class="hljs-comment">// Math.abs() returns the absolute value of a number</span>
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.abs(directions[<span class="hljs-string">"&lt;"</span>] - directions[<span class="hljs-string">"&gt;"</span>]) + directions[<span class="hljs-string">"*"</span>];
}
</code></pre>
<h3 id="heading-community-solutions"><strong>Community Solutions</strong></h3>
<p>Solution by <a target="_blank" href="https://github.com/cristianstu">cristianstu</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">maxDistance</span>(<span class="hljs-params">movements</span>) </span>{
    <span class="hljs-keyword">const</span> a = movements.split(<span class="hljs-regexp">/&gt;/g</span>).length - <span class="hljs-number">1</span>
    <span class="hljs-keyword">const</span> b = movements.split(<span class="hljs-regexp">/&lt;/g</span>).length - <span class="hljs-number">1</span>
    <span class="hljs-keyword">const</span> c = movements.length - a - b;

    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.abs(a - b) + c
}
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/jfes29">jfes29</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> maxDistance(movements) {
  <span class="hljs-keyword">let</span> movements1 = movements.replaceAll(<span class="hljs-string">"*"</span>, <span class="hljs-string">""</span>);
  <span class="hljs-keyword">let</span> movements2 = movements1.replaceAll(<span class="hljs-string">"&lt;"</span>, <span class="hljs-string">""</span>);
  <span class="hljs-keyword">let</span> movements3 = movements1.replaceAll(<span class="hljs-string">"&gt;"</span>, <span class="hljs-string">""</span>);

  <span class="hljs-keyword">return</span> movements.length - <span class="hljs-number">2</span> * <span class="hljs-built_in">Math</span>.min(movements2.length, movements3.length);
}
</code></pre>
<p>And that was the challenge for December 6th and its solutions. Do you have another alternative solution? Leave it in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[AdventJS 2023: Day 5 Challenge]]></title><description><![CDATA[The solution to the Challenge #5 of AdventJS 2023
The solution to the previous challenge
The solution to the next chprevious challengeallenge
Challenge Description
Santa 🎅 is testing his new electric sled, the CyberReindeer, on a North Pole road. Th...]]></description><link>https://alexvalle.dev/adventjs-2023-day-5-challenge</link><guid isPermaLink="true">https://alexvalle.dev/adventjs-2023-day-5-challenge</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Node.js]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Tue, 05 Dec 2023 06:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1701896969443/ac18f080-fea4-41bc-9d7d-0e19fafb8d3e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The solution to the <a target="_blank" href="https://adventjs.dev/es/challenges/2023/5">Challenge #5</a> of <a target="_blank" href="https://adventjs.dev/es">AdventJS 2023</a></p>
<p>The solution to the <a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-4-challenge">previous challenge</a></p>
<p>The solution to the <a target="_blank" href="https://alexvalle.dev/">next ch</a><a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-6-challenge">previous challenge</a><a target="_blank" href="https://alexvalle.dev/">allenge</a></p>
<h2 id="heading-challenge-description"><strong>Challenge Description</strong></h2>
<p>Santa 🎅 is testing his new electric sled, the <em>CyberReindeer</em>, on a North Pole road. The road is represented by a string of characters, where:</p>
<ul>
<li><p><code>.</code> = Road</p>
</li>
<li><p><code>S</code> = Santa's Sled</p>
</li>
<li><p><code>*</code> = Open barrier</p>
</li>
<li><p><code>|</code> = Closed barrier</p>
</li>
</ul>
<p>Example of a road: <code>S...|....|.....</code></p>
<p>For each unit of time, <strong>the sled moves one position to the right</strong>. If it encounters a closed barrier, it stops until the barrier opens. If it is open, it goes through directly.</p>
<p><strong>All barriers start closed</strong>, but after 5 units of time, they all open <strong>forever</strong>.</p>
<p><strong>Create a function that simulates the sled's movement</strong> for a given time and <strong>returns an array</strong> of strings representing the state of the road at each unit of time:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> road = <span class="hljs-string">'S..|...|..'</span>
<span class="hljs-keyword">const</span> time = <span class="hljs-number">10</span> <span class="hljs-comment">// units of time</span>
<span class="hljs-keyword">const</span> result = cyberReindeer(road, time)

<span class="hljs-comment">/* -&gt; result:
[
  'S..|...|..', // initial state
  '.S.|...|..', // sled advances on the road
  '..S|...|..', // sled advances on the road
  '..S|...|..', // sled stops at the barrier
  '..S|...|..', // sled stops at the barrier
  '...S...*..', // barrier opens, sled advances
  '...*S..*..', // sled advances on the road
  '...*.S.*..', // sled advances on the road
  '...*..S*..', // sled advances on the road
  '...*...S..', // passes through the open barrier
]
*/</span>
</code></pre>
<p>The result is an <strong>array where each element shows the road at each unit of time</strong>.</p>
<p>Take into account that <strong>if the sled is in the same position as a barrier</strong>, then it takes its place in the array.</p>
<h2 id="heading-analysis"><strong>Analysis</strong></h2>
<p>To solve this challenge, we must move the letter <code>S</code> within the string and stop it when it finds a barrier (<code>|</code>). The barrier will open (<code>*</code>) after 5 units of time. We need to store the journey of the letter <code>S</code> in an array until the units of time run out.</p>
<h3 id="heading-inputs"><strong>Inputs</strong></h3>
<ol>
<li><p>Road (<code>road</code>): The original string through which the sled will make its journey.</p>
</li>
<li><p>Time (<code>time</code>): A number with the units of time.</p>
</li>
</ol>
<h3 id="heading-output"><strong>Output</strong></h3>
<ul>
<li>An array of the history of the sled's journey</li>
</ul>
<h3 id="heading-considerations"><strong>Considerations</strong></h3>
<ul>
<li><p>We must stop the sled when it encounters a barrier and store it in history even if it does not move from there.</p>
</li>
<li><p>When the sled passes over an open barrier (<code>*</code>), it will replace it in the string and once it has passed, the open barrier (<code>*</code>) must be put back.</p>
</li>
</ul>
<h2 id="heading-solution"><strong>Solution</strong></h2>
<p>The initial position of the sled is identified at the beginning. The function iterates for each unit of time, advancing the sled if there is no closed barrier ahead. On the fifth unit of time, all barriers open permanently. The state of the road is updated and saved in an array at each iteration, reflecting the position of the sled and the state of the barriers. The final result is an array showing the state of the road at each moment of the given time.</p>
<h3 id="heading-code"><strong>Code</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-comment">/**
 * Simulates the movement of Santa's sled on a road for a given time.
 * 
 * <span class="hljs-doctag">@param <span class="hljs-type">{string}</span> <span class="hljs-variable">road</span></span> - Representation of the road with '.', 'S', '|', and '*'.
 * <span class="hljs-doctag">@param <span class="hljs-type">{number}</span> <span class="hljs-variable">time</span></span> - Number of units of time to simulate.
 * <span class="hljs-doctag">@return <span class="hljs-type">{string[]}</span> </span>- Array representing the state of the road at each unit of time.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">cyberReindeer</span>(<span class="hljs-params">road, time</span>) </span>{
  <span class="hljs-comment">// Stores the state of the road at each unit of time</span>
  <span class="hljs-keyword">const</span> roadStates = [road];

  <span class="hljs-comment">// Finds the initial position of the sled</span>
  <span class="hljs-keyword">let</span> sledPosition = road.indexOf(<span class="hljs-string">"S"</span>);

  <span class="hljs-comment">// Character that will be replaced by the sled when it moves</span>
  <span class="hljs-keyword">let</span> replacedChar = <span class="hljs-string">"."</span>;

  <span class="hljs-comment">// Iterates for each unit of time, minus one, since the initial state is already included</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; time - <span class="hljs-number">1</span>; i++) {
    <span class="hljs-comment">// Gets the current state of the road</span>
    <span class="hljs-keyword">let</span> currentRoadState = roadStates[i];

    <span class="hljs-comment">// On the fifth iteration (unit of time 5), all barriers open</span>
    <span class="hljs-keyword">if</span> (i === <span class="hljs-number">4</span>) {
      currentRoadState = currentRoadState.replace(<span class="hljs-regexp">/[|]/g</span>, <span class="hljs-string">"*"</span>);
    }

    <span class="hljs-comment">// Checks if the next position of the sled is not a closed barrier</span>
    <span class="hljs-keyword">if</span> (currentRoadState[sledPosition + <span class="hljs-number">1</span>] !== <span class="hljs-string">"|"</span>) {
      <span class="hljs-comment">// Prepares the new sled position</span>
      <span class="hljs-comment">// concatenating it to the element that previously was in that position</span>
      <span class="hljs-keyword">const</span> newSledPosition = <span class="hljs-string">`<span class="hljs-subst">${replacedChar}</span>S`</span>;

      <span class="hljs-comment">// Updates the character replaced by the sled before being replaced</span>
      replacedChar = currentRoadState[sledPosition + <span class="hljs-number">1</span>];

      <span class="hljs-comment">// Builds the new state of the road with the sled moved one position</span>
      <span class="hljs-keyword">const</span> firstPart = currentRoadState.substring(<span class="hljs-number">0</span>, sledPosition);
      <span class="hljs-keyword">const</span> lastPart = currentRoadState.substring(sledPosition + <span class="hljs-number">2</span>);
      currentRoadState = firstPart + newSledPosition + lastPart;

      <span class="hljs-comment">// Updates the sled position</span>
      sledPosition += <span class="hljs-number">1</span>;
    }

    <span class="hljs-comment">// Adds the updated state of the road to the array</span>
    roadStates.push(currentRoadState);
  }

  <span class="hljs-comment">// Returns the array with the state of the road at each unit of time</span>
  <span class="hljs-keyword">return</span> roadStates;
}
</code></pre>
<h3 id="heading-community-solutions"><strong>Community Solutions</strong></h3>
<p>Solution by <a target="_blank" href="https://github.com/SantiMenendez19/adventjs/blob/main/2023/challenge05/solution.js">SantiMenendez19</a>:</p>
<pre><code class="lang-javascript">javascriptCopy codefunction cyberReindeer(road, time) {
    <span class="hljs-keyword">let</span> result = []
    <span class="hljs-keyword">let</span> timePassed = <span class="hljs-number">0</span>
    <span class="hljs-keyword">let</span> pos = <span class="hljs-number">0</span>
    <span class="hljs-keyword">let</span> aux = <span class="hljs-string">"."</span>
    <span class="hljs-keyword">while</span> (time &gt; <span class="hljs-number">0</span>) {
        result.push(road)
        time--
        timePassed++
        <span class="hljs-keyword">if</span> (timePassed &gt;= <span class="hljs-number">5</span>) road = road.replace(<span class="hljs-regexp">/\|/gi</span>, <span class="hljs-string">"*"</span>)
        <span class="hljs-keyword">if</span> (road[pos+<span class="hljs-number">1</span>] === <span class="hljs-string">'.'</span> || road[pos+<span class="hljs-number">1</span>] === <span class="hljs-string">'*'</span>) {
            road = road.split(<span class="hljs-string">""</span>)
            road[pos] = aux
            aux = road[pos+<span class="hljs-number">1</span>]
            road[pos+<span class="hljs-number">1</span>] = <span class="hljs-string">"S"</span>
            road = road.join(<span class="hljs-string">""</span>)
            pos++
        }
    }
    <span class="hljs-keyword">return</span> result
}
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/Achalogy/advent-js-2023/blob/main/retos/reto-5/main.ts">Achalogy</a>:</p>
<pre><code class="lang-javascript">javascriptCopy codefunction cyberReindeer(road, time) {
  <span class="hljs-keyword">let</span> moves = [road]
  <span class="hljs-keyword">let</span> a = <span class="hljs-number">0</span>
  <span class="hljs-keyword">let</span> b = <span class="hljs-string">"."</span>

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">1</span>; i &lt; time; i++) {
    <span class="hljs-keyword">if</span> (i == <span class="hljs-number">5</span>) road = road.replace(<span class="hljs-regexp">/\|/g</span>, <span class="hljs-string">"*"</span>)
    <span class="hljs-keyword">const</span> newRoad = road.replace(<span class="hljs-regexp">/S[\.\*]/</span>, <span class="hljs-string">`<span class="hljs-subst">${b}</span>S`</span>)
    <span class="hljs-keyword">if</span> (newRoad != road) {
      a++
      b = road[a]
    }
    road = newRoad
    moves.push(road)
  }

  <span class="hljs-keyword">return</span> moves;
}
</code></pre>
<p>And that was the challenge for December 5th and its solutions. Do you have another alternative solution? Leave it in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[AdventJS 2023: Day 4 Challenge]]></title><description><![CDATA[The solution to Challenge #4 of AdventJS 2023
The solution to the previous challenge
The solution to the next challenge
Challenge Description
In 🎅 Santa's workshop, some Christmas messages have been written peculiarly: the words within the brackets ...]]></description><link>https://alexvalle.dev/adventjs-2023-day-4-challenge</link><guid isPermaLink="true">https://alexvalle.dev/adventjs-2023-day-4-challenge</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[challenge]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Mon, 04 Dec 2023 22:09:53 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1701727471843/2401b1ab-0e04-46bd-bd07-8c93f709e768.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The solution to <a target="_blank" href="https://adventjs.dev/challenges/2023/4">Challenge #4</a> of <a target="_blank" href="https://adventjs.dev/">AdventJS 2023</a></p>
<p>The solution to the <a target="_blank" href="https://alexvalle.dev/adjventjs-2023-day-3-challenge">previous challenge</a></p>
<p>The solution to the <a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-5-challenge">next challenge</a></p>
<h2 id="heading-challenge-description"><strong>Challenge Description</strong></h2>
<p>In 🎅 Santa's workshop, <strong>some Christmas messages have been written peculiarly</strong>: the words within the brackets must be read backward.</p>
<p><strong>Santa needs these messages to be correctly formatted.</strong> 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.</p>
<p>However, bear in mind that there may be nested parentheses, so you should reverse the characters in the correct order.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> a = decode(<span class="hljs-string">'hola (odnum)'</span>)
<span class="hljs-built_in">console</span>.log(a) <span class="hljs-comment">// hola mundo</span>

<span class="hljs-keyword">const</span> b = decode(<span class="hljs-string">'(olleh) (dlrow)!'</span>)
<span class="hljs-built_in">console</span>.log(b) <span class="hljs-comment">// hello world!</span>

<span class="hljs-keyword">const</span> c = decode(<span class="hljs-string">'sa(u(cla)atn)s'</span>)
<span class="hljs-built_in">console</span>.log(c) <span class="hljs-comment">// santaclaus</span>

<span class="hljs-comment">// Step by step:</span>
<span class="hljs-comment">// 1. Reverse the nested -&gt; sa(ualcatn)s</span>
<span class="hljs-comment">// 2. Reverse the remaining one -&gt; santaclaus</span>
</code></pre>
<p>Notes:</p>
<ul>
<li><p>The input strings will always be well-formed with parentheses that match correctly, you do not need to validate them.</p>
</li>
<li><p>There should not be any parentheses left in the final message.</p>
</li>
<li><p>The maximum nesting level is 2.</p>
</li>
</ul>
<h2 id="heading-analysis"><strong>Analysis</strong></h2>
<p>In this exercise, the goal is to reverse the letters that are within parentheses. If there are nested parentheses, you'll need to reorder multiple times, depending on the number of nested levels.</p>
<h3 id="heading-input"><strong>Input</strong></h3>
<ol>
<li>Message (<code>message</code>): A string with the message in which we must search and reorder the letters that are inside parentheses.</li>
</ol>
<h3 id="heading-output"><strong>Output</strong></h3>
<ul>
<li>The correctly ordered string</li>
</ul>
<h3 id="heading-considerations"><strong>Considerations</strong></h3>
<ul>
<li><p>There is no need to validate that the parentheses match and are well closed.</p>
</li>
<li><p>All parentheses in the text should be removed.</p>
</li>
</ul>
<h2 id="heading-solution"><strong>Solution</strong></h2>
<p>To cover all cases and considerations, you should start by looking for the last opening parenthesis ( and then match it with the first closing parenthesis ) after the opening one. Extract the letters that are inside and reorder them in reverse.</p>
<p>Then repeat the process with the resulting string, so a while loop can be used to achieve this.</p>
<h3 id="heading-code"><strong>Code</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-comment">/**
 * Decodes a message by reversing the characters between each pair of parentheses.
 *
 * <span class="hljs-doctag">@param <span class="hljs-type">{string}</span> <span class="hljs-variable">message</span></span> - The message to be decoded.
 * <span class="hljs-doctag">@return <span class="hljs-type">{string}</span> </span>- The decoded message.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">decode</span>(<span class="hljs-params">message</span>) </span>{
  <span class="hljs-comment">// Initialize variables to store the indices of the opening and closing parentheses</span>
  <span class="hljs-keyword">let</span> init, end;

  <span class="hljs-comment">// Loop until there are no more opening and closing parentheses</span>
  <span class="hljs-keyword">while</span> (init !== <span class="hljs-number">-1</span> || end !== <span class="hljs-number">-1</span>) {
    <span class="hljs-comment">// Find the index of the last opening parenthesis</span>
    init = message.lastIndexOf(<span class="hljs-string">"("</span>);

    <span class="hljs-comment">// Find the index of the closing parenthesis after the last opening parenthesis</span>
    end = message.indexOf(<span class="hljs-string">")"</span>, init);

    <span class="hljs-comment">// Extract the encoded string between the opening and closing parentheses</span>
    <span class="hljs-keyword">const</span> encoded = message.substring(init, end + <span class="hljs-number">1</span>);

    <span class="hljs-comment">// Reverse the characters in the encoded string, excluding the opening and closing parentheses</span>
    <span class="hljs-keyword">const</span> decoded = encoded.split(<span class="hljs-string">""</span>).slice(<span class="hljs-number">1</span>, <span class="hljs-number">-1</span>).reverse().join(<span class="hljs-string">""</span>);

    <span class="hljs-comment">// Replace the encoded string with the decoded string in the original message</span>
    message = message.replace(encoded, decoded);
  }

  <span class="hljs-comment">// Return the decoded message</span>
  <span class="hljs-keyword">return</span> message;
}
</code></pre>
<h3 id="heading-community-solutions"><strong>Community Solutions</strong></h3>
<p>Solution by <a target="_blank" href="https://github.com/cristianstu">cristianstu</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">decode</span>(<span class="hljs-params">message</span>) </span>{
  <span class="hljs-keyword">const</span> msg = message.replace(<span class="hljs-regexp">/\(([^()]*)\)/g</span>, <span class="hljs-function">(<span class="hljs-params">_, match</span>) =&gt;</span> 
    match.split(<span class="hljs-string">''</span>).reverse().join(<span class="hljs-string">''</span>)
  );

  <span class="hljs-keyword">return</span> msg.includes(<span class="hljs-string">'('</span>) ? decode(msg) : msg;
}
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/TimmyElTaco">TimmyElTaco</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">decode</span>(<span class="hljs-params">message</span>) </span>{
  <span class="hljs-keyword">let</span> matches = [];

  <span class="hljs-keyword">while</span> (matches = message.match(<span class="hljs-regexp">/\(([^()]*)\)/</span>)) {
    matches = matches.map(<span class="hljs-function">(<span class="hljs-params">word</span>) =&gt;</span> {
      <span class="hljs-keyword">return</span> word.split(<span class="hljs-string">''</span>).reverse().join(<span class="hljs-string">''</span>)
    })
    <span class="hljs-keyword">let</span> string = matches[<span class="hljs-number">0</span>].replace(<span class="hljs-regexp">/[()]/g</span>, <span class="hljs-string">''</span>);
    message = message.replace(<span class="hljs-regexp">/\(([^()]*)\)/</span>, string);
  }

  <span class="hljs-keyword">return</span> message;
}
</code></pre>
<p>And that was the challenge for December 4th and its solutions. Do you have another alternative solution? Leave it in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[AdjventJS 2023: Day 3 Challenge]]></title><description><![CDATA[The solution to Challenge #3 of AdventJS 2023
The solution to the previous challenge
The solution to the next challenge
Challenge Description
In Santa's workshop, a mischievous elf has been playing around with the gift production line, adding or remo...]]></description><link>https://alexvalle.dev/adjventjs-2023-day-3-challenge</link><guid isPermaLink="true">https://alexvalle.dev/adjventjs-2023-day-3-challenge</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[challenge]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Sun, 03 Dec 2023 06:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1701726368520/476bbe82-8646-4947-bc91-49e3f86714e3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The solution to <a target="_blank" href="https://adventjs.dev/challenges/2023/3">Challenge #3</a> of <a target="_blank" href="https://adventjs.dev">AdventJS</a> 2023</p>
<p>The solution to the <a target="_blank" href="https://alexvalle.dev/adjventjs-2023-day-2-challenge">previous challenge</a></p>
<p>The solution to the <a target="_blank" href="https://alexvalle.dev/adjventjs-2023-day-4-challenge">next challenge</a></p>
<h2 id="heading-challenge-description"><strong>Challenge Description</strong></h2>
<p>In Santa's workshop, <strong>a mischievous elf</strong> has been playing around with the gift production line, adding or removing an unplanned step.</p>
<p>You have the original sequence of <em>original</em> manufacturing steps and the modified <em>modified</em> sequence that may include an extra step or be missing a step.</p>
<p>Your task is to <strong>write a function that identifies and returns the first extra step that was added or removed in the manufacturing chain</strong>. If there is no difference between the sequences, return an empty string.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> original = <span class="hljs-string">'abcd'</span>
<span class="hljs-keyword">const</span> modified = <span class="hljs-string">'abcde'</span>
findNaughtyStep(original, modified) <span class="hljs-comment">// 'e'</span>

<span class="hljs-keyword">const</span> original = <span class="hljs-string">'stepfor'</span>
<span class="hljs-keyword">const</span> modified = <span class="hljs-string">'stepor'</span>
findNaughtyStep(original, modified) <span class="hljs-comment">// 'f'</span>

<span class="hljs-keyword">const</span> original = <span class="hljs-string">'abcde'</span>
<span class="hljs-keyword">const</span> modified = <span class="hljs-string">'abcde'</span>
findNaughtyStep(original, modified) <span class="hljs-comment">// ''</span>
</code></pre>
<p>Please, keep in mind:</p>
<ul>
<li><p>There will always be one different step or none.</p>
</li>
<li><p>The modification can occur anywhere in the string.</p>
</li>
<li><p>The <em>original</em> steps could be empty</p>
</li>
</ul>
<h2 id="heading-analysis"><strong>Analysis</strong></h2>
<p>The goal is to write a function that can identify the first difference between two strings, either by an extra char in the string or a missing char.</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong>Char:</strong> In programming, the <strong>char</strong> data type is for characters. It refers to a symbol (letters, numbers, punctuation) like a, +, 6, !, S. In JavaScript, it is still a string, as there is no data type for char, but when we talk about it we refer to a single character. - <a target="_new" href="https://developer.mozilla.org/docs/Glossary/Character">MDN Glossary</a></div>
</div>

<p><strong>Input:</strong></p>
<ol>
<li><p>Original (<code>original</code>): The original string, from which we must start to find the differences. These are the original steps.</p>
</li>
<li><p>Modified (<code>modified</code>): The modified string to which we must find the first extra char or the first missing char. These are the steps modified by the mischievous elf.</p>
</li>
</ol>
<p><strong>Output:</strong></p>
<ul>
<li>A string showing the extra or missing char, if not found returns an empty string <code>''</code></li>
</ul>
<p><strong>Considerations</strong></p>
<ul>
<li>The modified sequence must not only contain the chars from the original but also have them in the same order.</li>
</ul>
<h2 id="heading-solution"><strong>Solution</strong></h2>
<p>You can use a loop to compare char by char in the strings and, upon finding the difference, evaluate whether it is an extra element or a missing element and return it. If the conditions are not met, we return an empty string <code>''</code>.</p>
<h3 id="heading-code"><strong>Code</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-comment">/**
 * Find the first different char between two strings.
 *
 * <span class="hljs-doctag">@param <span class="hljs-type">{string}</span> <span class="hljs-variable">original</span></span> - The original string.
 * <span class="hljs-doctag">@param <span class="hljs-type">{string}</span> <span class="hljs-variable">modified</span></span> - The modified string.
 * <span class="hljs-doctag">@return <span class="hljs-type">{string}</span> </span>The first difference between the original and modified string.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findNaughtyStep</span>(<span class="hljs-params">original, modified</span>) </span>{
  <span class="hljs-comment">// We start by declaring the index we will be iterating</span>
  <span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>;

  <span class="hljs-comment">// While i is less than the sizes of both strings</span>
  <span class="hljs-comment">// we go through each one to compare them</span>
  <span class="hljs-keyword">while</span> (i &lt; original.length || i &lt; modified.length) {
    <span class="hljs-comment">// If the char from the original string is different from the char in the modified string,</span>
    <span class="hljs-comment">// If they are the same we continue iterating and adding 1 to the index</span>

    <span class="hljs-keyword">if</span> (original[i] !== modified[i]) {
      <span class="hljs-comment">// If the original string has more characters than the modified string,</span>
      <span class="hljs-comment">// return the char from the original string.</span>
      <span class="hljs-comment">// otherwise, return the char from the modified string</span>
      <span class="hljs-keyword">return</span> original.length &gt; modified.length ? original[i] : modified[i];
    }

    i += <span class="hljs-number">1</span>;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-string">''</span>;
}
</code></pre>
<h3 id="heading-community-solutions"><strong>Community Solutions</strong></h3>
<p>Solution by <a target="_blank" href="https://github.com/Achalogy/advent-js-2023/blob/main/retos/reto-3/main.ts">Achalogy</a>:</p>
<pre><code class="lang-javascript">findNaughtyStep(original, modified) {

  <span class="hljs-keyword">const</span> [lessWords, mostWords] =
    [original, modified].sort(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a.length - b.length)

  <span class="hljs-keyword">return</span> [...mostWords].find(<span class="hljs-function">(<span class="hljs-params">x, i</span>) =&gt;</span> lessWords[i] != x) ?? <span class="hljs-string">""</span>;
}
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/iswilljr/adventjs-challenges/blob/master/2023/challenge-03/challenge-03.ts">iswilljr</a>:</p>
<pre><code class="lang-javascript">findNaughtyStep(original: string, <span class="hljs-attr">modified</span>: string) {
  <span class="hljs-keyword">const</span> originalLen = original.length
  <span class="hljs-keyword">const</span> modifiedLen = modified.length

  <span class="hljs-keyword">const</span> sequences = {
    [originalLen]: [original, modified],
    [modifiedLen]: [modified, original],
  }

  <span class="hljs-keyword">const</span> [steps, reference] = sequences[<span class="hljs-built_in">Math</span>.max(originalLen, modifiedLen)]

  <span class="hljs-keyword">return</span> [...steps].find(<span class="hljs-function">(<span class="hljs-params">step, index</span>) =&gt;</span> step !== reference[index]) ?? <span class="hljs-string">''</span>
}
</code></pre>
<p>And that was the challenge for December 3rd and its solutions. Do you have another alternative solution? Leave it in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[AdjventJS 2023: Day 2 Challenge]]></title><description><![CDATA[The solution to Challenge #2 of AdventJS 2023
Previous challenge: Day 1
Next Challenge: Day 3
Challenge Description
In Santa's workshop, the elves have a gift list they wish to make and a limited set of materials.
Gifts are strings of text and materi...]]></description><link>https://alexvalle.dev/adjventjs-2023-day-2-challenge</link><guid isPermaLink="true">https://alexvalle.dev/adjventjs-2023-day-2-challenge</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[challenge]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Sat, 02 Dec 2023 06:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1701612956052/0da478fb-678b-4521-bbd4-7d2e641fc07a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The solution to <a target="_blank" href="https://adventjs.dev/challenges/2023/2">Challenge #2</a> of <a target="_blank" href="https://adventjs.dev/">AdventJS 2023</a></p>
<p><a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-1">Previous challenge: Day 1</a></p>
<p><a target="_blank" href="https://alexvalle.dev/adventjs-2023-day-3">Next Challenge: Day 3</a></p>
<h2 id="heading-challenge-description"><strong>Challenge Description</strong></h2>
<p>In Santa's workshop, the elves have a <strong>gift list</strong> they wish to make and a limited set of materials.</p>
<p><em>Gifts are strings of text</em> and <em>materials are characters</em>. Your task is to write a function that, given a list of gifts and the available materials, returns a <strong>list of the gifts that can be made</strong>.</p>
<p>A gift can be made if we have all the necessary materials to make it.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> gifts = [<span class="hljs-string">'tren'</span>, <span class="hljs-string">'oso'</span>, <span class="hljs-string">'pelota'</span>]
<span class="hljs-keyword">const</span> materials = <span class="hljs-string">'tronesa'</span>

manufacture(gifts, materials) <span class="hljs-comment">// ["tren", "oso"]</span>

<span class="hljs-keyword">const</span> gifts = [<span class="hljs-string">'juego'</span>, <span class="hljs-string">'puzzle'</span>]
<span class="hljs-keyword">const</span> materials = <span class="hljs-string">'jlepuz'</span>

manufacture(gifts, materials) <span class="hljs-comment">// ["puzzle"]</span>

<span class="hljs-keyword">const</span> gifts = [<span class="hljs-string">'libro'</span>, <span class="hljs-string">'ps5'</span>]
<span class="hljs-keyword">const</span> materials = <span class="hljs-string">'psli'</span>

manufacture(gifts, materials) <span class="hljs-comment">// []</span>
</code></pre>
<h2 id="heading-analysis"><strong>Analysis</strong></h2>
<p>We have a string with certain letters that are the materials and an array of words that are the gifts. We must search for the words in which all their letters are found within the string materials.</p>
<p><strong>Inputs:</strong></p>
<ol>
<li><p><strong>Gifts (</strong><code>gifts</code>): An array of strings, where each string represents a gift.</p>
</li>
<li><p><strong>Materials (</strong><code>materials</code>): A string representing the available materials.</p>
</li>
</ol>
<p><strong>Output:</strong></p>
<ul>
<li>An array of gifts that can be made using the available materials.</li>
</ul>
<p><strong>Considerations</strong></p>
<ul>
<li><p>Each character in a gift's string represents a necessary material.</p>
</li>
<li><p>It is necessary to verify that all characters of a gift are present in the <code>materials</code> string.</p>
</li>
<li><p>Each character in the materials string can be used multiple times, so there is no limit on materials.</p>
</li>
</ul>
<h2 id="heading-solution"><strong>Solution</strong></h2>
<p>To solve this problem, I found two main approaches, iterating over the gifts and materials or using <a target="_blank" href="https://developer.mozilla.org/docs/Web/JavaScript/Guide/Regular_expressions">regular expressions</a>.</p>
<h3 id="heading-iterating"><strong>Iterating</strong></h3>
<pre><code class="lang-javascript">jsxCopy code<span class="hljs-comment">/**
 * Filters a list of gifts based on the available materials.
 *
 * <span class="hljs-doctag">@param <span class="hljs-type">{string[]}</span> <span class="hljs-variable">gifts</span></span> - The list of gifts to filter.
 * <span class="hljs-doctag">@param <span class="hljs-type">{string}</span> <span class="hljs-variable">materials</span></span> - The available materials.
 * <span class="hljs-doctag">@return <span class="hljs-type">{string[]}</span> </span>The filtered list of gifts that can be manufactured.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">manufacture</span>(<span class="hljs-params">gifts, materials</span>) </span>{
  <span class="hljs-comment">// Use filter to return only the gifts that contain all the materials</span>
  <span class="hljs-keyword">return</span> gifts.filter(<span class="hljs-function">(<span class="hljs-params">gift</span>) =&gt;</span> {

    <span class="hljs-comment">// Check if each character in the gift is in the materials</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> char <span class="hljs-keyword">of</span> gift) {

      <span class="hljs-comment">// If the character is not in the materials, return false</span>
      <span class="hljs-comment">// false means that the gift cannot be manufactured</span>
      <span class="hljs-comment">// this stops checking the rest of the characters for better performance</span>
      <span class="hljs-keyword">if</span> (!materials.includes(char)) {
        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
      }
    }

    <span class="hljs-comment">// If all the characters are in the materials, return true</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
  });
}
</code></pre>
<h3 id="heading-regex"><strong>Regex</strong></h3>
<p>This involves creating a regex based on <code>materials</code> that match any combination (including repetition) of characters within <code>materials</code> and nothing else.</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Want to learn how to make regex like this? <a target="_blank" href="https://regexlearn.com">regexlearn.com</a> is an excellent resource to learn!</div>
</div>

<pre><code class="lang-javascript">jsxCopy code<span class="hljs-comment">/**
 * Filters a list of gifts based on the available materials.
 *
 * <span class="hljs-doctag">@param <span class="hljs-type">{string[]}</span> <span class="hljs-variable">gifts</span></span> - The list of gifts to filter.
 * <span class="hljs-doctag">@param <span class="hljs-type">{string}</span> <span class="hljs-variable">materials</span></span> - The available materials.
 * <span class="hljs-doctag">@return <span class="hljs-type">{string[]}</span> </span>The filtered list of gifts that can be manufactured.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">manufacture</span>(<span class="hljs-params">gifts, materials</span>) </span>{
  <span class="hljs-keyword">const</span> regex = <span class="hljs-keyword">new</span> <span class="hljs-built_in">RegExp</span>(<span class="hljs-string">`^[<span class="hljs-subst">${materials}</span>]+$`</span>);
  <span class="hljs-keyword">return</span> gifts.filter(<span class="hljs-function">(<span class="hljs-params">gift</span>) =&gt;</span> regex.test(gift));
}
</code></pre>
<h3 id="heading-community-solutions"><strong>Community Solutions</strong></h3>
<p>Solution by <a target="_blank" href="https://github.com/SantiMenendez19/adventjs/blob/main/2023/challenge02/solution.js">SantiMenendez19</a></p>
<pre><code class="lang-javascript">jsxCopy codefunction manufacture(gifts, materials) {
    <span class="hljs-keyword">return</span> gifts.filter(<span class="hljs-function"><span class="hljs-params">gift</span> =&gt;</span> [...gift].every(<span class="hljs-function"><span class="hljs-params">c</span> =&gt;</span> materials.includes(c)))
}
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/Savecoders/Solve-AdventJS/blob/main/Ad2023/Challenge02/main.js">Savecoders</a></p>
<pre><code class="lang-javascript">jsxCopy codefunction manufacture(gifts, material) {
  <span class="hljs-keyword">return</span> gifts.filter(<span class="hljs-function">(<span class="hljs-params">gift</span>) =&gt;</span>
    gift.split(<span class="hljs-string">""</span>).every(<span class="hljs-function">(<span class="hljs-params">matt</span>) =&gt;</span> material.includes(matt))
  );
}
</code></pre>
<p>And that was the challenge for December 2nd and its solutions. Do you have another alternative solution? Leave it in the comments!</p>
]]></content:encoded></item><item><title><![CDATA[AdventJS 2023: Day 1]]></title><description><![CDATA[Solution for Challenge #1 of AdventJS 2023
Introduction
On December 1st, AdventJS started a series of challenges to practice and improve as a software developer. It consists of a new programming challenge being enabled every day, from December 1st un...]]></description><link>https://alexvalle.dev/adventjs-2023-day-1</link><guid isPermaLink="true">https://alexvalle.dev/adventjs-2023-day-1</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[TypeScript]]></category><category><![CDATA[challenge]]></category><dc:creator><![CDATA[Alex Valle]]></dc:creator><pubDate>Fri, 01 Dec 2023 06:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1701542986627/e19a8344-0d83-4b84-be8c-0c7a5e605070.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Solution for <a target="_blank" href="https://adventjs.dev/challenges/2023/1">Challenge #1</a> of <a target="_blank" href="https://adventjs.dev/">AdventJS 2023</a></p>
<h2 id="heading-introduction">Introduction</h2>
<p>On December 1st, <a target="_blank" href="https://adventjs.dev/">AdventJS</a> started a series of challenges to practice and improve as a software developer. It consists of a new programming challenge being enabled every day, from December 1st until Christmas on the 25th. 🎄</p>
<h2 id="heading-challenge-description">Challenge Description</h2>
<p>In the toy factory of the North Pole, each toy has a unique identification number.</p>
<p>However, due to an error in the toy machine, some numbers have been assigned to more than one toy.</p>
<p>Find the first identification number that has been repeated, <strong>where the second occurrence has the smallest index</strong>!</p>
<p>In other words, if there is more than one repeated number, you must return the number whose second occurrence appears first in the list. If there are no repeated numbers, return -1.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> giftIds = [<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>]
<span class="hljs-keyword">const</span> firstRepeatedId = findFirstRepeated(giftIds)
<span class="hljs-built_in">console</span>.log(firstRepeatedId) <span class="hljs-comment">// 3</span>
<span class="hljs-comment">// Even though 2 and 3 are repeated</span>
<span class="hljs-comment">// 3 appears second time first</span>

<span class="hljs-keyword">const</span> giftIds2 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]
<span class="hljs-keyword">const</span> firstRepeatedId2 = findFirstRepeated(giftIds2)
<span class="hljs-built_in">console</span>.log(firstRepeatedId2) <span class="hljs-comment">// -1</span>
<span class="hljs-comment">// It is -1 since no number is repeated</span>

<span class="hljs-keyword">const</span> giftIds3 = [<span class="hljs-number">5</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>]
<span class="hljs-keyword">const</span> firstRepeatedId3 = findFirstRepeated(giftIds3)
<span class="hljs-built_in">console</span>.log(firstRepeatedId3) <span class="hljs-comment">// 5</span>
</code></pre>
<p><strong>Watch out!</strong> The elves say this is a Google technical test.</p>
<h2 id="heading-analysis">Analysis</h2>
<p>The goal is to find the first repeated number where the second occurrence of that number has the lowest possible index in the list. If there are no repeated numbers, -1 will be returned.</p>
<h3 id="heading-input">Input</h3>
<ul>
<li>A list of integer numbers (gifts), where each number represents a unique identifier of a toy.</li>
</ul>
<h3 id="heading-output">Output</h3>
<ul>
<li><p>The first identification number is repeated with the lowest index in its second appearance.</p>
</li>
<li><p><code>-1</code> if there are no repeated numbers.</p>
</li>
</ul>
<h3 id="heading-conceptual-analysis">Conceptual Analysis</h3>
<ul>
<li><p>The first repetition needs to be found, so it's not necessary to iterate more than once.</p>
</li>
<li><p>We need an efficient way to keep track of the numbers already checked.</p>
</li>
<li><p>We will stop execution when we find the first repeated number and return it.</p>
</li>
</ul>
<h2 id="heading-solution">Solution</h2>
<p>By creating a <a target="_blank" href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set">Set</a>, you can store the repeated values while traversing the array. Each stored element is evaluated, and upon finding a repetition, the answer is returned.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">/**
 * Finds the first repeated gift in the given array.
 *
 * <span class="hljs-doctag">@param <span class="hljs-type">{number[]}</span> <span class="hljs-variable">gifts</span></span> - The array of gifts to search for repeated items.
 * <span class="hljs-doctag">@returns <span class="hljs-type">{number}</span> </span>- The first repeated gift, or -1 if no repeated gift is found.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findFirstRepeated</span>(<span class="hljs-params">gifts</span>) </span>{
  <span class="hljs-comment">// Create a set to store unique gifts</span>
  <span class="hljs-keyword">const</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>();

  <span class="hljs-comment">// Iterate through the gifts array</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> gift <span class="hljs-keyword">of</span> gifts) {
    <span class="hljs-comment">// If the gift is already in the set, return it as the first repeated gift</span>
    <span class="hljs-keyword">if</span> (set.has(gift)) {
      <span class="hljs-keyword">return</span> gift;
    }

    <span class="hljs-comment">// Add the gift to the set</span>
    set.add(gift);
  }

  <span class="hljs-comment">// Return -1 if no repeated gift is found</span>
  <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
}
</code></pre>
<h3 id="heading-alternative-solutions">Alternative Solutions</h3>
<p>Here are some solutions provided by the community:</p>
<p>Solution by <a target="_blank" href="https://github.com/SantiMenendez19/adventjs/blob/main/2023/challenge01/solution.js">SantiMenendez19</a></p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findFirstRepeated</span>(<span class="hljs-params">gifts</span>) </span>{
  <span class="hljs-keyword">const</span> repeated = gifts.filter(<span class="hljs-function">(<span class="hljs-params">gift, i</span>) =&gt;</span> gifts.indexOf(gift) !== i)
  <span class="hljs-keyword">return</span> repeated.length &gt; <span class="hljs-number">0</span> ? repeated[<span class="hljs-number">0</span>] : <span class="hljs-number">-1</span>
}
</code></pre>
<p>Solution by <a target="_blank" href="https://github.com/marta-vilaseca/adventjs-2023/blob/main/01/01.js">marta-vilaseca</a></p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findFirstRepeated</span>(<span class="hljs-params">gifts</span>) </span>{
  <span class="hljs-keyword">const</span> unique = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>();
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; gifts.length; i++) {
    <span class="hljs-keyword">if</span> (unique.has(gifts[i])) {
      <span class="hljs-keyword">return</span> gifts[i];
    } <span class="hljs-keyword">else</span> {
      unique.add(gifts[i]);
    }
  }
  <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
}
</code></pre>
<p>Do you have another alternative? Leave it in the comments!</p>
]]></content:encoded></item></channel></rss>