<?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: Anish Pujari</title>
    <description>The latest articles on DEV Community by Anish Pujari (@anishpuj).</description>
    <link>https://dev.to/anishpuj</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%2F1188853%2F8c3566b2-7c0f-4186-91b9-c163b8e52198.jpeg</url>
      <title>DEV Community: Anish Pujari</title>
      <link>https://dev.to/anishpuj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anishpuj"/>
    <language>en</language>
    <item>
      <title>Pokemon Card Generator</title>
      <dc:creator>Anish Pujari</dc:creator>
      <pubDate>Tue, 07 Nov 2023 06:56:05 +0000</pubDate>
      <link>https://dev.to/anishpuj/pokemon-card-generator-3pgm</link>
      <guid>https://dev.to/anishpuj/pokemon-card-generator-3pgm</guid>
      <description>&lt;h6&gt;
  
  
  #Project Folder Structure:
&lt;/h6&gt;

&lt;p&gt;Let us take a quick look at the project structure. We have a project folder called – &lt;code&gt;Pokemon Card Generator.&lt;/code&gt; Inside this folder, we have three files. The first one is the HTML document called &lt;code&gt;index.html.&lt;/code&gt; Next one is the stylesheet named &lt;code&gt;style.css&lt;/code&gt;, and the last one is the script file called &lt;code&gt;script.js&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  HTML:
&lt;/h1&gt;

&lt;p&gt;The HTML consists of a &lt;code&gt;container&lt;/code&gt; div that wraps the &lt;code&gt;card&lt;/code&gt; div and the &lt;code&gt;button&lt;/code&gt; element. We don’t have many other elements in HTML code as we will be generating them using Javascript.&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;!DOCTYPE html&amp;gt;
-  &amp;lt;html lang="en"&amp;gt;
-    &amp;lt;head&amp;gt;
-      &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
-  &amp;lt;title&amp;gt;Pokemon Card Generator&amp;lt;/title&amp;gt;
-      &amp;lt;!-- Google Fonts --&amp;gt;
-      &amp;lt;link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&amp;amp;display=swap"rel="stylesheet"/&amp;gt;
-      &amp;lt;!-- Stylesheet --&amp;gt;
-      &amp;lt;link rel="stylesheet" href="style.css" /&amp;gt;
-    &amp;lt;/head&amp;gt;
-    &amp;lt;body&amp;gt;
-      &amp;lt;div class="container"&amp;gt;
-        &amp;lt;div id="card"&amp;gt;&amp;lt;/div&amp;gt;
-        &amp;lt;button id="btn"&amp;gt;Generate&amp;lt;/button&amp;gt;
-      &amp;lt;/div&amp;gt;
-      &amp;lt;!-- Script --&amp;gt;
-      &amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;
-    &amp;lt;/body&amp;gt;
-  &amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  CSS:
&lt;/h1&gt;

&lt;p&gt;Coming to the CSS Code, we do a CSS reset by removing the unwanted paddings and margins. We also centre the &lt;code&gt;container&lt;/code&gt; using the transforms. We set the dimensions for the container and the &lt;code&gt;card&lt;/code&gt; div.&lt;/p&gt;

&lt;p&gt;For the pokemon image we set the &lt;code&gt;width&lt;/code&gt; to 180px and &lt;code&gt;max-height&lt;/code&gt; to 200px. &lt;br&gt;
We style the &lt;code&gt;span&lt;/code&gt; elements and place them inside the types div using the flex layout. We set the &lt;code&gt;justify-content&lt;/code&gt; to &lt;code&gt;space-around&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, we style the stats div and the button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  background-color: #eff3ff;
}
.container {
  width: 350px;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
}
#card {
  position: relative;
  width: 100%;
  padding: 30px 20px;
  box-shadow: 0 20px 30px rgba(0, 0, 0, 0.15);
  border-radius: 10px;
}
#card img {
  display: block;
  width: 180px;
  max-height: 200px;
  position: relative;
  margin: 20px auto;
}
.hp {
  width: 80px;
  background-color: #ffffff;
  text-align: center;
  padding: 8px 0;
  border-radius: 30px;
  margin-left: auto;
  font-weight: 400;
}
.poke-name {
  text-align: center;
  font-weight: 600;
}
.types {
  display: flex;
  justify-content: space-around;
  margin: 20px 0 40px 0;
}
.hp span,
.types span {
  font-size: 12px;
  letter-spacing: 0.4px;
  font-weight: 600;
}
.types span {
  padding: 5px 20px;
  border-radius: 20px;
  color: #ffffff;
}
.stats {
  display: flex;
  align-items: center;
  justify-content: space-between;
  text-align: center;
}
.stats p {
  color: #404060;
}
#btn {
  display: block;
  padding: 15px 60px;
  font-size: 18px;
  background-color: #101010;
  color: #ffffff;
  position: relative;
  margin: 30px auto;
  border: none;
  border-radius: 5px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Javascript:
&lt;/h1&gt;

&lt;p&gt;Now coming to javascript, we create an array for colours corresponding to each pokemon type. Next, we store the pokeAPI link in a constant called &lt;code&gt;URL&lt;/code&gt;. We also get the &lt;code&gt;card&lt;/code&gt; and &lt;code&gt;btn&lt;/code&gt; elements and assign them to variables.&lt;/p&gt;

&lt;p&gt;In the next step, we create a function called &lt;code&gt;getPokeData&lt;/code&gt;. Within this function, we generate a random number between 1 and 150. This number will be our id.&lt;/p&gt;

&lt;p&gt;We create the &lt;code&gt;finalUrl&lt;/code&gt; by combining the &lt;code&gt;URL&lt;/code&gt; and the &lt;code&gt;id&lt;/code&gt; string. Now we fetch the data for the pokemon corresponding to the id generated. We use this data to create the card.&lt;/p&gt;

&lt;p&gt;For the next step, we create a function called &lt;code&gt;generateCard()&lt;/code&gt;. Now using the data fetched from the API we get necessary data and assign them to variables.&lt;/p&gt;

&lt;p&gt;We now use the pokemon type to get a corresponding color value from the &lt;code&gt;typeColor&lt;/code&gt; array. Now we assign this value to a variable called &lt;code&gt;themeColor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we generate HTML for the card using variables we have created. At this point, we call the function &lt;code&gt;appendTypes&lt;/code&gt; with the parameter &lt;code&gt;data.types&lt;/code&gt;.&lt;br&gt;
The &lt;code&gt;appendTypes&lt;/code&gt; function iterates over the types array and wrap each type inside a &lt;code&gt;span&lt;/code&gt; element. This span element is then appended to the &lt;code&gt;types&lt;/code&gt; div.&lt;/p&gt;

&lt;p&gt;We also call the &lt;code&gt;styleCard&lt;/code&gt; function which applies the &lt;code&gt;themeColor&lt;/code&gt; to the card background as radial-gradient and the span element inside the types div. And that’s it. Our random pokemon Card generator is now ready.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const typeColor = {
  bug: "#26de81",
  dragon: "#ffeaa7",
  electric: "#fed330",
  fairy: "#FF0069",
  fighting: "#30336b",
  fire: "#f0932b",
  flying: "#81ecec",
  grass: "#00b894",
  ground: "#EFB549",
  ghost: "#a55eea",
  ice: "#74b9ff",
  normal: "#95afc0",
  poison: "#6c5ce7",
  psychic: "#a29bfe",
  rock: "#2d3436",
  water: "#0190FF",
};
const url = " https://pokeapi.co/api/v2/pokemon/";
const card = document.getElementById("card");
const btn = document.getElementById("btn");

let getPokeData = () =&amp;gt; {
  // Generate a random number between 1 and 150
  let id = Math.floor(Math.random() * 150) + 1;
  // Combine the pokeapi url with pokemon id
  const finalUrl = url + id;
  // Fetch generated URL
  fetch(finalUrl)
    .then((response) =&amp;gt; response.json())
    .then((data) =&amp;gt; {
      generateCard(data);
    });
};

//Generate Card

let generateCard = (data) =&amp;gt; {
  // Get necessary data and assign it to variables
  console.log(data);
  const hp = data.stats[0].base_stat;
  const imgSrc = data.sprites.other.dream_world.front_default;
  const pokeName = data.name[0].toUpperCase() + data.name.slice(1);
  const statAttack = data.stats[1].base_stat;
  const statDefense = data.stats[2].base_stat;
  const statSpeed = data.stats[5].base_stat;

  // Set themeColor based on pokemon type
  const themeColor = typeColor[data.types[0].type.name];
  console.log(themeColor);
  card.innerHTML = `
        &amp;lt;p class="hp"&amp;gt;
          &amp;lt;span&amp;gt;HP&amp;lt;/span&amp;gt;
            ${hp}
        &amp;lt;/p&amp;gt;
        &amp;lt;img src=${imgSrc} /&amp;gt;
        &amp;lt;h2 class="poke-name"&amp;gt;${pokeName}&amp;lt;/h2&amp;gt;
        &amp;lt;div class="types"&amp;gt;

        &amp;lt;/div&amp;gt;
        &amp;lt;div class="stats"&amp;gt;
          &amp;lt;div&amp;gt;
            &amp;lt;h3&amp;gt;${statAttack}&amp;lt;/h3&amp;gt;
            &amp;lt;p&amp;gt;Attack&amp;lt;/p&amp;gt;
          &amp;lt;/div&amp;gt;
          &amp;lt;div&amp;gt;
            &amp;lt;h3&amp;gt;${statDefense}&amp;lt;/h3&amp;gt;
            &amp;lt;p&amp;gt;Defense&amp;lt;/p&amp;gt;
          &amp;lt;/div&amp;gt;
          &amp;lt;div&amp;gt;
            &amp;lt;h3&amp;gt;${statSpeed}&amp;lt;/h3&amp;gt;
            &amp;lt;p&amp;gt;Speed&amp;lt;/p&amp;gt;
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
  `;
  appendTypes(data.types);
  styleCard(themeColor);
};
let appendTypes = (types) =&amp;gt; {
  types.forEach((item) =&amp;gt; {
    let span = document.createElement("SPAN");
    span.textContent = item.type.name;
    document.querySelector(".types").appendChild(span);
  });
};
let styleCard = (color) =&amp;gt; {
  card.style.background = `radial-gradient(circle at 50% 0%, ${color} 36%, #ffffff 36%)`;
  card.querySelectorAll(".types span").forEach((typeColor) =&amp;gt; {
    typeColor.style.backgroundColor = color;
  });
};

btn.addEventListener("click", getPokeData);
window.addEventListener("load", getPokeData);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://pokecardapinew.netlify.app/"&gt;Click here for the live demo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>api</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
