<?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: Pradhyumn Sharma</title>
    <description>The latest articles on DEV Community by Pradhyumn Sharma (@impradhyumn).</description>
    <link>https://dev.to/impradhyumn</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%2F1191827%2F1d808ed9-3ba0-48f1-8bd1-c44f8adf9ed3.jpeg</url>
      <title>DEV Community: Pradhyumn Sharma</title>
      <link>https://dev.to/impradhyumn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/impradhyumn"/>
    <language>en</language>
    <item>
      <title>Image Carousel using JavaScript, HTML, CSS</title>
      <dc:creator>Pradhyumn Sharma</dc:creator>
      <pubDate>Wed, 07 Feb 2024 17:51:46 +0000</pubDate>
      <link>https://dev.to/impradhyumn/image-carousel-using-javascript-html-css-48ln</link>
      <guid>https://dev.to/impradhyumn/image-carousel-using-javascript-html-css-48ln</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hey there!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We all have seen carousels on many websites. And you too might have used in your web projects with the help of some framework like Bootstrap.&lt;/p&gt;

&lt;p&gt;But today we will implement it on our own using HTML, CSS and JavaScript. And also it could be asked to you in some machine coding interview round.&lt;/p&gt;

&lt;p&gt;Before we dive into the coding part let’s understand the structure of a carousel.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We have main div, we can call it as the container (&lt;strong&gt;div#container&lt;/strong&gt;) with some width and height.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We have let’s say 4 div inside the main div that contains our images (&lt;strong&gt;div.image__container&lt;/strong&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every &lt;strong&gt;inner div&lt;/strong&gt; contains an image taking full width and height of the parent and also &lt;strong&gt;object-fit : cover&lt;/strong&gt; property, so our every image covers the whole div.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, the main div would be having &lt;strong&gt;display : flex&lt;/strong&gt; property so that our &lt;strong&gt;inner divs&lt;/strong&gt; are in a row but we need to show one image at a time so we need to apply &lt;strong&gt;overflow : hidden&lt;/strong&gt; property too.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need two buttons, one to move to the next image and one to get back to the previous one.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;After these 5steps our output would be like this.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fakhg04weyy7dizp1y3hp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fakhg04weyy7dizp1y3hp.jpg" alt="Coding wallpaper"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now here’s one thing to note, by default flex property has &lt;strong&gt;flex-shrink : 1&lt;/strong&gt; set to the children, that’s why our images are being shrunk, but for our use case we want the div to take full width of the main container. In order to achieve that we need to put &lt;strong&gt;flex-shrink : 0&lt;/strong&gt; for &lt;strong&gt;inner divs&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;And after this step our output would be like this.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7i1x4iaygvxb0c3i7ba.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7i1x4iaygvxb0c3i7ba.jpg" alt="Coding wallpaper"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let’s have some dive into coding!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. HTML&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;link href="carousel.css" rel="stylesheet" /&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div id="carousel__container"&amp;gt;
        &amp;lt;div class="img__container"&amp;gt;
        &amp;lt;img class="carousel__img" src="https://i.ibb.co/K2KkmJx/florian-olivo-4hb-J-eym-Z1o-unsplash.jpg" alt="carousel-img" /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="img__container"&amp;gt;
            &amp;lt;img class="carousel__img" src="https://i.ibb.co/3Nmfpsm/jackson-sophat-t-l5-FFH8-VA-unsplash.jpg" alt="carousel-img" /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="img__container"&amp;gt;
            &amp;lt;img class="carousel__img" src="https://i.ibb.co/sVXbVdr/nathan-da-silva-k-r-Kfq-Sm4-L4-unsplash.jpg" alt="carousel-img" /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="img__container"&amp;gt;
        &amp;lt;img class="carousel__img" src="https://i.ibb.co/fMf7S4k/kobu-agency-ip-ARHax-ETRk-unsplash.jpg" alt="carousel-img" /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;button id="prev-btn" class="btn"&amp;gt;P&amp;lt;/button&amp;gt;
        &amp;lt;button id="next-btn" class="btn"&amp;gt;N&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;script src="carousel.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;2. CSS&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#carousel__container {
  position: relative;
  display: flex;
  width: 600px;
  height: 300px;
  overflow: hidden;
  border: 1px solid red;
}

.img__container {
  width: 100%;
  height: 100%;
  flex-shrink: 0;
}
.img__container &amp;gt; img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.btn {
  position: absolute;
  top: 50%;
  border-radius: 50%;
  color: black;
  background-color: white;
  border: none;
  outline: none;
  padding: 5px 7px;
  cursor: pointer;
}
#prev-btn {
  left: 10px;
}
#next-btn {
  right: 10px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;3. JavaScript : This is where all the magic happens.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This block of code is simple to understand, we are adding event listeners to our buttons so to invoke corresponding functions.&lt;/p&gt;

&lt;p&gt;We are getting our images from the DOM and storing them in an array.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nextBtn = document.getElementById("next-btn");
const prevBtn = document.getElementById("prev-btn");

nextBtn.addEventListener("click", showNextImage);
prevBtn.addEventListener("click", showPrevImage);

//document.getElementsByClassName return HTML collection so we are using "Array.from" method to get an iterable

const images = Array.from(document.getElementsByClassName("carousel__img"));
const totalImages = images.length;

let currentImageIndex = 0; //index of image that being display on screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You must have seen that in a carousel, images slide with a smooth sliding effect so to add the same effect, we are creating a function that add CSS styling to every image..&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addTransitionEffectToImages() {
  images.forEach((img) =&amp;gt; {
    img.style.transition = "transform 0.8s ease";
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let’s understand what happens when user click on the next button.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Firstly, we are adding the transition effect to our images so they slide smoothly giving a sliding effect.&lt;/li&gt;
&lt;li&gt;In CSS, we have &lt;strong&gt;transform&lt;/strong&gt; property through which we can animate a HTML element in various ways. But for our use case we want to move our &lt;strong&gt;inner divs&lt;/strong&gt; in X(horizontal) direction by some specific pixels or percentage. For that we have &lt;strong&gt;translateX&lt;/strong&gt; function. If we want our element to move in right direction, the value passed would be positive and vice-versa.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;transform : translateX(50px); //moves elements to right by 50px
transform : translateX(-30px); //moves elements to left by 30px

transform : translateX(100%); //moves elements to right as far as its length
transform : translateX(-100%); //moves elements to left as far as its length
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Now, every time we click on the next button, every image would move to the left based on what position they are at currently on and update the index by 1. &lt;/li&gt;
&lt;li&gt;If we are on last image we will reset our carousel using the &lt;strong&gt;resetCarousel()&lt;/strong&gt; function. 
```
function showNextImage() {
//if we are at last image reset the carousel
if (currentImageIndex == totalImages - 1) {
resetCarousel();
return;
}&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;if (currentImageIndex === 0) addTransitionEffectToImages();&lt;br&gt;
  //translate every image with (-100%) every time we go to next image&lt;br&gt;
  images.forEach((img) =&amp;gt; {&lt;br&gt;
    img.style.transform = &lt;code&gt;translate(${(currentImageIndex + 1) * -100}%)&lt;/code&gt;;&lt;br&gt;
  });&lt;br&gt;
  currentImageIndex++;&lt;br&gt;
}&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;function resetCarousel() {&lt;br&gt;
  images.forEach((img) =&amp;gt; {&lt;br&gt;
    //we are setting "none" so when every image gets back to position we don't want to show sliding effect&lt;br&gt;
    img.style.transition = "none";&lt;br&gt;
    img.style.transform = &lt;code&gt;translate(0)&lt;/code&gt;; //every image back to original position&lt;br&gt;
  });&lt;br&gt;
  currentImageIndex = 0;&lt;br&gt;
}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
In our **resetCarousel()** function we are removing the transition property because every image will return to its normal position but with the sliding effect like a train goes on but we don’t want that. Removing this property will show directly the first image without any effect. You can try removing in your code and see what happens.

We are remaining with one last thing that how to make previous button work.

- If we are on the 1st image then we simply return.
- The logic of this part is opposite of the next button functionality. We simple **translateX** the images by -100% and decrement the index by 1.

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

&lt;/div&gt;

&lt;p&gt;function showPrevImage() {&lt;br&gt;
  if (currentImageIndex === 0) return; //if we are at first image, then simply return&lt;/p&gt;

&lt;p&gt;//reverse logic for what we did in showNextImage&lt;br&gt;
  images.forEach((img) =&amp;gt; {&lt;br&gt;
    img.style.transform = &lt;code&gt;translateX(${(currentImageIndex - 1) * -100}%)&lt;/code&gt;;&lt;br&gt;
  });&lt;br&gt;
  currentImageIndex--;&lt;br&gt;
}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
**_Happy Coding!!_**

**_Thank you for reading…Do like and comment!!_**

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

&lt;/div&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>programming</category>
    </item>
    <item>
      <title>Polyfill for map, filter and reduce in JavaScript</title>
      <dc:creator>Pradhyumn Sharma</dc:creator>
      <pubDate>Tue, 06 Feb 2024 11:04:15 +0000</pubDate>
      <link>https://dev.to/impradhyumn/polyfill-for-map-filter-and-reduce-in-javascript-5638</link>
      <guid>https://dev.to/impradhyumn/polyfill-for-map-filter-and-reduce-in-javascript-5638</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Hey there!!&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you are a JavaScript developer or learner then you must have come around these 3 widely used array methods : map, filter and reduce.&lt;/p&gt;

&lt;p&gt;If you are a consistent learner or preparing for interviews, this article might help you to go little bit deep down in JS. Before we see how Polyfills are implemented lets define what a Polyfill is.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Polyfill :&lt;/strong&gt; A piece of code to implement a specific functionality so that browsers provide them natively. Suppose a browser doesn’t support a particular String method, in that case developers could write their own implementation for the same.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, let’s dive into some coding!!&lt;/p&gt;

&lt;p&gt;In JavaScript if you want add a method to an object we can do it by using the &lt;strong&gt;prototype&lt;/strong&gt;. Suppose, you have an object named Person and you wish to add a method &lt;strong&gt;&lt;em&gt;getName()&lt;/em&gt;&lt;/strong&gt;, then you can do that by following piece of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Person.prototype.getName = function() {
  //code goes here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, using the same syntax we are going to implement our polyfills.&lt;/p&gt;

&lt;p&gt;If you see the syntax of &lt;strong&gt;&lt;em&gt;map()&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;filter()&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;_reduce() _&lt;/strong&gt;functions, they take a callback as an argument. And that callback function takes 3 arguments.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;currentValue&lt;/li&gt;
&lt;li&gt;index of currentValue [Optional]&lt;/li&gt;
&lt;li&gt;array [Optional]&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Polyfill for map : customMap()&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.customMap = function(callback) {
  const arr = this;
  const result = [];
  for (let i = 0; i &amp;lt; arr.length; i++) {
    const val = callback(arr[i], i); //pass 'i' for index
    result.push(val);
  }
  return result;
};

const arr = [1,2,3,4,5];
console.log(arr.customMap((ele, index) =&amp;gt; {
  console.log(index);
  return ele*2;
}))
//Output
//0 1 2 3 4
//[2,4,6,8,10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Polyfill for filter : customFilter()&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Polyfill for filter is same as map with the only difference that we just need to add array element into the result if the callback returns true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.customFilter = function(callback) {
  const arr = this;
  const result = [];
  for (let i = 0; i &amp;lt; arr.length; i++) {
    if(callback(arr[i], i) === true);
      result.push(arr[i]);
  }
  return result;
};

const arr = [1,2,3,4,5];
let result = arr.customFilter((ele) =&amp;gt; {
  return (ele%2 === 0);
})

console.log(result);
//[2,4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Polyfill for reduce : customReduce()&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Polyfill for reduce is little different than the previous ones. It takes one more argument named as &lt;strong&gt;initialValue&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;array.reduce(callback(result, currentValue, index), initialValue = 0)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.customReduce = function (callback, initialValue = 0) {
  const arr = this;
  let result = initialValue;
  for (let i = 0; i &amp;lt; arr.length; i++) {
    result = callback(result, arr[i], i);
  }
  return result;
};

const arr = [1, 2, 3, 4, 5];
let result = arr.customReduce((res, arr) =&amp;gt; {
  return res + arr;
}, 3);

console.log(result);
//18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Happy Coding!!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do like and comment…Thank You for reading!!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

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