<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daniel Jiménez</title>
    <description>The latest articles on DEV Community by Daniel Jiménez (@danieljimenez).</description>
    <link>https://dev.to/danieljimenez</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2901452%2F0600f741-280f-48ac-a8c5-b182e5037134.jpg</url>
      <title>DEV Community: Daniel Jiménez</title>
      <link>https://dev.to/danieljimenez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/danieljimenez"/>
    <language>en</language>
    <item>
      <title>Luhn Algorithm for Dummies</title>
      <dc:creator>Daniel Jiménez</dc:creator>
      <pubDate>Fri, 28 Feb 2025 14:22:07 +0000</pubDate>
      <link>https://dev.to/danieljimenez/luhn-algorithm-for-dummies-1fba</link>
      <guid>https://dev.to/danieljimenez/luhn-algorithm-for-dummies-1fba</guid>
      <description>&lt;p&gt;Are you crying until midnight trying to understand how to do the Luhn Algorithm?&lt;br&gt;
Are you trying hard for hours and then giving up when your only error was about when to double?&lt;/p&gt;

&lt;p&gt;This is your place. Here, I will explain to you how to do it with JavaScript. From a dummy to another dummy. Let's go.&lt;/p&gt;
&lt;h2&gt;
  
  
  What the #### is the Luhn Algorithm?
&lt;/h2&gt;



&lt;p&gt;To make a long story short, it’s an algorithm used for validating credit cards. Basically, if at the end your result is 0, you have a valid card. If not, be ready because Ocean’s Eleven is entering your bank.&lt;/p&gt;

&lt;p&gt;For implementing the algorithm in code, you have to take into consideration these 5 steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1)&lt;/strong&gt; You need to iterate through each number from right to left (aka backwards).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2)&lt;/strong&gt; Then, you multiply by 2 only the digits in even positions (starting from the right).&lt;/p&gt;

&lt;p&gt;Imagine you have '2097'. We would multiply 9 and 2 by 2. The other numbers remain unchanged.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3)&lt;/strong&gt; For the digits you’ve doubled, if they are greater than or equal to 10, you have to subtract 9 from them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4)&lt;/strong&gt; Now, it’s time to sum everything up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5)&lt;/strong&gt; You need to get the module of the division of your total sum by 10. The module is what’s left after division.&lt;/p&gt;

&lt;p&gt;For example, if you divide 10 by 2, the result is 5 and the module is 0 because there’s nothing left. But if you divide 9 by 2, the result is 4 and the module is 1 because 2 * 4 is 8, and you’re missing 1 to reach 9.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79xla9rhrlgw6swip4tq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79xla9rhrlgw6swip4tq.png" alt="Steps explained in a image" width="800" height="668"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Luhn Algorithm Explanation
&lt;/h2&gt;



&lt;p&gt;&lt;strong&gt;Let’s Code It in JavaScript!&lt;/strong&gt;&lt;br&gt;
Now, I will show you the complete code and then explain it. This first code is the best way to understand it because it’s quite raw. There are other ways of doing it with array methods, but let’s start with this first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const luhnAlgorithm = (arr) =&amp;gt; {
    let sum = 0;
    let shouldDouble = false;

    for (let i = arr.length - 1; i &amp;gt;= 0; i--) {
        let digit = arr[i];
        if (shouldDouble) {
            digit *= 2;
            if (digit &amp;gt; 9) digit -= 9;
        }
        sum += digit;
        shouldDouble = !shouldDouble;
    }

    console.log("Total sum:", sum);
    return sum % 10 === 0;
};

// Example credit card numbers (as arrays)
const validCreditCard = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]; // Valid
const invalidCreditCard = [3, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]; // Invalid

console.log(luhnAlgorithm(validCreditCard)); // true
console.log(luhnAlgorithm(invalidCreditCard)); // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code Explanation
&lt;/h2&gt;




&lt;p&gt;It wasn’t so hard, was it? I’ll explain it a little bit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: The iteration from right to left is solved by using a for loop with a decrement (i--).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: We use a variable called shouldDouble, which is a boolean that changes in each iteration. It’s false in the beginning because the last number is never multiplied. Remember, that’s the reason why we start from the end.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: If a digit is greater than 9 after doubling, we subtract 9 from it: if (digit &amp;gt; 9) digit -= 9;. That’s all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: We add the digit to the total sum: sum += digit;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: The result is checked by returning sum % 10 === 0. If the remainder is 0, the credit card number is valid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alternative Solutions
&lt;/h2&gt;




&lt;p&gt;There are other ways of solving this exercise that involve more methods. For example, you can use array methods like map, reduce, and reverse to achieve the same result. If you’re interested, check out this (&lt;a href="https://www.30secondsofcode.org/js/s/luhn-check/" rel="noopener noreferrer"&gt;https://www.30secondsofcode.org/js/s/luhn-check/&lt;/a&gt;) for an alternative implementation.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Rainbow infinite carousel tutorial (CSS)</title>
      <dc:creator>Daniel Jiménez</dc:creator>
      <pubDate>Fri, 28 Feb 2025 10:01:16 +0000</pubDate>
      <link>https://dev.to/danieljimenez/rainbow-infinite-carousel-tutorial-css-3dhl</link>
      <guid>https://dev.to/danieljimenez/rainbow-infinite-carousel-tutorial-css-3dhl</guid>
      <description>&lt;p&gt;We have seen infinite carousels in all directions and with all kinds of functions. The most typical one might be a website displaying the logos of its ambassadors, but there are many original uses for this, such as showcasing an art gallery. The only limit is your imagination!&lt;/p&gt;

&lt;p&gt;I will explain the &lt;strong&gt;easiest way&lt;/strong&gt; I have found to create this effect. If you want a sneak peek of the final result, check out my CodePen at the end of this file. ;)&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;first thing&lt;/strong&gt; we need to do for this project is to create two "divs." A friendly reminder: use the semantic tag that best suits your needs (aside, section or article could be great choices depending on your website). We are going to fill these divs with squares, but you can use whatever you like (images, etc.).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;section&amp;gt;
  &amp;lt;div class="container"&amp;gt;
    &amp;lt;div class="carousel"&amp;gt;
      &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/section&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you've done this correctly, you won't see anything yet. ;) But that will change soon. Let's &lt;strong&gt;add some styles&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.carousel {
  display: inline-flex;
}

.carousel .square {
  height: 200px;
  width: 300px;
  margin: 0;
}
.carousel .square:nth-child(1) {
  background: red;
}
.carousel .square:nth-child(2) {
  background: orange;
}
.carousel .square:nth-child(3) {
  background: yellow;
}
.carousel .square:nth-child(4) {
  background: green;
}
.carousel .square:nth-child(5) {
  background: blue;
}
.carousel .square:nth-child(6) {
  background: indigo;
}
.carousel .square:nth-child(7) {
  background: violet;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You can check &lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Fmdn-social-share.d893525a4fb5fb1f67a2.png" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child" rel="noopener noreferrer" class="c-link"&gt;
          :nth-child() - CSS: Cascading Style Sheets | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          The :nth-child() CSS pseudo-class matches elements based on the indexes of the elements in the child list of their parents. In other words, the :nth-child() selector selects child elements according to their position among all the sibling elements within a parent element.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Ffavicon-48x48.bc390275e955dacb2e65.png" width="48" height="48"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;if you need a refresher on this pseudo-class.&lt;/p&gt;

&lt;p&gt;Now we have our carousel! &lt;strong&gt;Let's animate it&lt;/strong&gt;. We really love rocking that CSS!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.carousel {
  display: inline-flex;
  animation: 5s infinite-carousel infinite linear;
}

@keyframes infinite-carousel {
  0% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(-100%);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You can check &lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/es/docs/Web/CSS/@keyframes" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Fmdn-social-share.d893525a4fb5fb1f67a2.png" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/es/docs/Web/CSS/@keyframes" rel="noopener noreferrer" class="c-link"&gt;
          @keyframes - CSS | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          La regla arroba @keyframes permite a los autores controlar los pasos intermedios en una secuencia de animación CSS mediante el establecimiento de keyframes (o puntos de trayectoria) a lo largo de la secuencia de animación que debe ser alcanzado por determinados puntos durante la animación. Esto le da un control más específico sobre los pasos intermedios de la secuencia de animación que se obtiene al dejar que el navegador maneje todo automáticamente.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Ffavicon-48x48.bc390275e955dacb2e65.png" width="48" height="48"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;for more information.&lt;/p&gt;

&lt;p&gt;The rainbow is moving, right? :) However, it still doesn't have the infinite effect, and when it ends, it jumps back to the same position. The keyframes name might seem a bit grand for a simple translation animation, but we will fix that soon.&lt;/p&gt;

&lt;p&gt;Now comes the magic. The trick to making this effect infinite is to have not just one but &lt;strong&gt;two carousels playing the same animation simultaneously!&lt;/strong&gt; The key is that both carousels are placed inline next to each other, so even though the animation restarts, we don't see the reset. We will achieve this with pseudo-classes and the &lt;code&gt;overflow&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;Let's start by adding another carousel. Your final HTML should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;section&amp;gt;
    &amp;lt;div class="container"&amp;gt;
      &amp;lt;div class="carousel"&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div class="carousel"&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="square"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/section&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, &lt;strong&gt;let's align them&lt;/strong&gt; properly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.container {
  overflow: hidden;
  padding: 40px 0;
  white-space: nowrap;
  position: relative;
  font-size: 0;
  line-height: 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;white-space: nowrap;&lt;/code&gt; property ensures the elements are displayed inline. The &lt;code&gt;overflow: hidden;&lt;/code&gt; prevents horizontal scrolling and keeps the effect visible. The &lt;code&gt;font-size: 0;&lt;/code&gt; and &lt;code&gt;line-height: 0;&lt;/code&gt; remove unwanted default browser styling for inline elements. If you use &lt;code&gt;gap&lt;/code&gt;, images, or other elements, you may not need these adjustments.&lt;/p&gt;

&lt;p&gt;Always refer to MDN when you need more details. This was the tricky part!&lt;/p&gt;

&lt;p&gt;Now, let's create a &lt;strong&gt;smooth transition effect&lt;/strong&gt; on each side of the carousel using the &lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt; pseudo-classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.container:before,
.container:after {
  position: absolute;
  top: 0;
  width: 20px;
  height: 100%;
  content: "";
  z-index: 2;
}

.container:before {
  left: 0;
  background: linear-gradient(to left, rgba(255, 255, 255, 0), white);
}

.container:after {
  right: 0;
  background: linear-gradient(to right, rgba(255, 255, 255, 0), white);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt; pseudo-elements create two "divs" on each side. We use &lt;code&gt;position: absolute;&lt;/code&gt; inside the &lt;code&gt;.container&lt;/code&gt;, which has &lt;code&gt;position: relative;&lt;/code&gt;. The width will depend on your design needs—most cases will require a wider gradient. Don't forget the &lt;code&gt;content: "";&lt;/code&gt; property; otherwise, the pseudo-elements won't appear.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And there you have it!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(Yes, the animation might be too fast. Adjust the animation duration to fit your needs. ;) )&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check out my example on CodePen:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/danielpsico93/embed/PwoWwJP?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>html</category>
      <category>animation</category>
    </item>
  </channel>
</rss>
