<?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: Hamza Ihsan</title>
    <description>The latest articles on DEV Community by Hamza Ihsan (@hamzaihsan).</description>
    <link>https://dev.to/hamzaihsan</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%2F1186786%2F5d258f07-a305-49a0-9f9a-fab6af3b92e0.jpg</url>
      <title>DEV Community: Hamza Ihsan</title>
      <link>https://dev.to/hamzaihsan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hamzaihsan"/>
    <language>en</language>
    <item>
      <title>JavaScript for Babies</title>
      <dc:creator>Hamza Ihsan</dc:creator>
      <pubDate>Fri, 26 Jul 2024 20:12:28 +0000</pubDate>
      <link>https://dev.to/hamzaihsan/javascript-for-babies-3bl2</link>
      <guid>https://dev.to/hamzaihsan/javascript-for-babies-3bl2</guid>
      <description>&lt;p&gt;Hi there, this article is for people who know HTML and CSS, but JavaScript looks too complex to learn. This is the easiest javascript tutorial on the internet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-requisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You Should have a little bit of knowledge about HTML and CSS.&lt;/li&gt;
&lt;li&gt;VSCODE&lt;/li&gt;
&lt;li&gt;Know how to create an index.html file.&lt;/li&gt;
&lt;li&gt;Already know a C/C++ based language (a little bit).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What is JavaScript?&lt;/strong&gt;&lt;br&gt;
It's a programming language for the web. It's as simple as that.&lt;/p&gt;

&lt;p&gt;So let's get started without wasting time. &lt;/p&gt;
&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;Start by opening a folder in VScode and making a file as &lt;code&gt;index.html&lt;/code&gt;. Press &lt;code&gt;!&lt;/code&gt; and Press Enter. You should be able to generate the HTML boilerplate.&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%2Fgfo696h7ok4wnlkrru3q.gif" 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%2Fgfo696h7ok4wnlkrru3q.gif" alt="GIF showing using ! to generate HTML boilerplate" width="692" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then in the &lt;code&gt;head&lt;/code&gt; tag add the &lt;code&gt;script&lt;/code&gt; tag.&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;head&amp;gt;
 &amp;lt;script src="./script.js" defer&amp;gt; &amp;lt;script&amp;gt;
&amp;lt;head/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Create a new file right beside the index.html file named &lt;code&gt;script.js&lt;/code&gt;&lt;br&gt;
Add this to your script file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alert("hello");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should be able to see this popup when you open the html file in the browser.&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%2Fxo2y9qqxc4hq1be5xoyu.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%2Fxo2y9qqxc4hq1be5xoyu.png" alt=" " width="585" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means you have successfully linked JavaScript to your HTML.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function in JavaScript
&lt;/h2&gt;

&lt;p&gt;You can make a function in JS 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;function foo(){
    console.log('foo');
}

foo();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here foo is the name of the Function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Console in JavaScript
&lt;/h2&gt;

&lt;p&gt;Press &lt;code&gt;Ctrl+Shift+I&lt;/code&gt; in your browser where you have the &lt;code&gt;Console&lt;/code&gt; Tab. Here you can find error messages and messages from &lt;code&gt;console.log()&lt;/code&gt; calls. &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%2F0bq3kovt5nzgiygguefz.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%2F0bq3kovt5nzgiygguefz.png" alt="console.log(foo)" width="533" height="236"&gt;&lt;/a&gt;&lt;br&gt;
The &lt;code&gt;console.log("foo")&lt;/code&gt; from the above function is also displayed here.&lt;/p&gt;
&lt;h2&gt;
  
  
  Buttons and OnClick
&lt;/h2&gt;

&lt;p&gt;Make an HTML button with an &lt;code&gt;onClick&lt;/code&gt; prop. 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;button onclick="foo()"&amp;gt;Hello&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anything inside the &lt;code&gt;onClick&lt;/code&gt;prop will be executed every time you click on this button. In our case every time we click the button the function &lt;code&gt;foo()&lt;/code&gt; will be called.&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%2Fxll0dsmiynl709g984fy.gif" 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%2Fxll0dsmiynl709g984fy.gif" alt=" " width="1920" height="887"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Variables
&lt;/h2&gt;

&lt;p&gt;Let's add a variable &lt;code&gt;let count = 0;&lt;/code&gt; Here count is the name of the variable and it is initialized to zero. Let's change our function a little bit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let count = 0;

function increamentcount(){
    count++;
    console.log(count);
}

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

&lt;/div&gt;



&lt;p&gt;Here every time the function is called the count is incremented and displayed in the console.&lt;br&gt;
We will change the button to something like this &lt;br&gt;
&lt;code&gt;&amp;lt;button onclick="increamentcount()"&amp;gt;count++&amp;lt;/button&amp;gt;&lt;/code&gt;&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%2F1tz87cgmjy1toluuxpgf.gif" 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%2F1tz87cgmjy1toluuxpgf.gif" alt=" " width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Conditional Statement in JavaScript
&lt;/h2&gt;

&lt;p&gt;Here is a simple if statement in Javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  if(count == 10){
        count = 0;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is similar to C++ style conditional statements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Document Manipulation (Basics)
&lt;/h2&gt;

&lt;p&gt;Your whole HTML file is a document in itself. Manipulating this document via Javascript is called Document Manipulation.&lt;br&gt;
Let's make a paragraph tag with id = "count". Note that for Document Manipulation we use id's on the objects.&lt;br&gt;
*&lt;em&gt;What's an object? *&lt;/em&gt;&lt;br&gt;
Anything inside the HTML document is an object.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;body&amp;gt;&lt;br&gt;
    &amp;lt;p id="count"&amp;gt;&amp;lt;/p&amp;gt;&lt;br&gt;
    &amp;lt;button onclick="increamentcount()"&amp;gt;count++&amp;lt;/button&amp;gt;&lt;br&gt;
&amp;lt;/body&amp;gt;&lt;/code&gt;&lt;br&gt;
Now we will declare another variable that will target this paragraph tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let paragraph = document.getElementById('count');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are getting an element/object by its id.&lt;br&gt;
Then instead of printing in the console, we will make the innerText of the paragraph the count variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function increamentcount(){
    count++;
    paragraph.innerText = count;
}

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

&lt;/div&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%2Fbfrbeaecqdpteur87nv4.gif" 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%2Fbfrbeaecqdpteur87nv4.gif" alt=" " width="1091" height="887"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Socials
&lt;/h2&gt;

&lt;p&gt;Twitter (X): &lt;a href="https://x.com/ihsanhaamza" rel="noopener noreferrer"&gt;https://x.com/ihsanhaamza&lt;/a&gt;&lt;br&gt;
LinkedIn: &lt;a href="https://www.linkedin.com/in/hamza-ihsan/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/hamza-ihsan/&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/thehamzaihsan" rel="noopener noreferrer"&gt;https://github.com/thehamzaihsan&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Part 1: Making a tic-tac-toe game in React JS.</title>
      <dc:creator>Hamza Ihsan</dc:creator>
      <pubDate>Tue, 17 Oct 2023 01:57:59 +0000</pubDate>
      <link>https://dev.to/hamzaihsan/part-1-making-a-tic-tac-toe-game-in-react-js-515c</link>
      <guid>https://dev.to/hamzaihsan/part-1-making-a-tic-tac-toe-game-in-react-js-515c</guid>
      <description>&lt;p&gt;This post was originally a part of my &lt;a href="https://medium.com/@haamzaihsan" rel="noopener noreferrer"&gt;blog&lt;/a&gt;.&lt;br&gt;
So, in the last few months, I started learning React, a JavaScript Library for making web applications. My goal was to learn the basics and make some fun projects with it. Here is my journey making a tic-tac-toe in React JS (TypeScript).&lt;/p&gt;

&lt;p&gt;Note: This part focuses on UI and making a simple 2-player game. Still developing the version in which you can play with a bot. This is not a stepwise guide, instead, it is an over-the-top idea about how I made the game.&lt;/p&gt;

&lt;p&gt;Get the code here and don’t forget to follow me. → &lt;a href="https://github.com/thehamzaihsan/tictactoe-react-ts" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The UI:&lt;/strong&gt; The User Interface is made in Tailwind CSS which is a remarkable CSS library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Components&lt;/strong&gt; React uses components to give flexibility and ease of use, you don’t have to write a piece of code repeatedly. Here each tic-tac block uses a separate component as a button.&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;button
      title="t1"
      onClick={() =&amp;gt; {
        Click();
      }}
      className="text-center w-28 h-28 border-8 border-black box-border p-3"
    &amp;gt;
      {props.value == 1 ? (
        &amp;lt;svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"&amp;gt;
          &amp;lt;path
            d="M0 14.545 1.455 16 8 9.455 14.545 16 16 14.545 9.455 8 16 1.455 14.545 0 8 6.545 1.455 0 0 1.455 6.545 8z"
            fill-rule="evenodd"
            className="cross"
          /&amp;gt;
        &amp;lt;/svg&amp;gt;
      ) : null}
      {props.value == 2 ? (
        &amp;lt;svg viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg"&amp;gt;
          &amp;lt;circle cx="24" cy="24" r="21.5" className="circle" /&amp;gt;
        &amp;lt;/svg&amp;gt;
      ) : null}
    &amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Mechanics&lt;/strong&gt; The Game treats the board as a 2D array. Player 1 places a 1 and Player 2 places a 2 in the array. The player will only allowed to click if there is a 0 (empty) on that place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  var initTable: number[][] = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
  ];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**Inputs **The click () function is called when a button is pressed. The function calls another function outside the component that changes the Array and places a tick/circle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Click() {
    if (props.value == 0 &amp;amp;&amp;amp; props.gamerunning == true) {
      props.onfunctionClick(props.row, props.col);
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Checking&lt;/strong&gt; After every turn the game checks if the board is solved if yes then it makes the player from the last turn the “winner” else the game continues. It’s that simple…. uhm not that much, to check you have if else which check for diagonals, rows, and columns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function checkArray(arr: number[][]) {
    if (
      (arr[0][0] == 1 &amp;amp;&amp;amp; arr[1][1] == 1 &amp;amp;&amp;amp; arr[2][2] == 1) ||
      (arr[0][0] == 2 &amp;amp;&amp;amp; arr[1][1] == 2 &amp;amp;&amp;amp; arr[2][2] == 2)
    ) {
      return false;
      setwinner(turn);
    }

    for (let i = 0; i &amp;lt; 3; i++) {
      if (
        (arr[i][0] == 1 &amp;amp;&amp;amp; arr[i][1] == 1 &amp;amp;&amp;amp; arr[i][2] == 1) ||
        (arr[i][0] == 2 &amp;amp;&amp;amp; arr[i][1] == 2 &amp;amp;&amp;amp; arr[i][2] == 2)
      ) {
        return false;
        setwinner(turn);
      } else if (
        (arr[0][i] == 1 &amp;amp;&amp;amp; arr[1][i] == 1 &amp;amp;&amp;amp; arr[2][i] == 1) ||
        (arr[0][i] == 2 &amp;amp;&amp;amp; arr[1][i] == 2 &amp;amp;&amp;amp; arr[2][i] == 2)
      ) {
        return false;
        setwinner(turn);
      }
    }
    if (
      (arr[0][2] == 1 &amp;amp;&amp;amp; arr[1][1] == 1 &amp;amp;&amp;amp; arr[2][0] == 1) ||
      (arr[0][2] == 2 &amp;amp;&amp;amp; arr[1][1] == 2 &amp;amp;&amp;amp; arr[2][0] == 2)
    ) {
      return false;
      setwinner(turn);
    } else {
      return true;
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What if it is a draw?&lt;/strong&gt; So, it’s not straightforward how to check if it's a draw or not, but we can calculate the number of turns if they are equal to 9 and there is no winner so that means it’s a draw.&lt;/p&gt;

&lt;p&gt;What’s next? This is a simple fun project that I made to test my skills. It’s not perfect, but I will add some features like playing with a bot, etc.&lt;/p&gt;

&lt;p&gt;That’s it for now, See you later. Be sure to follow me on &lt;a href="https://medium.com/@haamzaihsan" rel="noopener noreferrer"&gt;Medium&lt;/a&gt; and &lt;a href="https://twitter.com/ihsanhaamza" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

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