<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ayushi Priya on Medium]]></title>
        <description><![CDATA[Stories by Ayushi Priya on Medium]]></description>
        <link>https://medium.com/@ayushipriya_40647?source=rss-da4bc77a971b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*a7mVVVL_nSiJ7X0X</url>
            <title>Stories by Ayushi Priya on Medium</title>
            <link>https://medium.com/@ayushipriya_40647?source=rss-da4bc77a971b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 14:03:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ayushipriya_40647/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Tic tac toe]]></title>
            <link>https://medium.com/@ayushipriya_40647/tic-tac-toe-694a364225ee?source=rss-da4bc77a971b------2</link>
            <guid isPermaLink="false">https://medium.com/p/694a364225ee</guid>
            <dc:creator><![CDATA[Ayushi Priya]]></dc:creator>
            <pubDate>Sun, 25 Dec 2022 16:10:38 GMT</pubDate>
            <atom:updated>2022-12-25T16:10:38.214Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/265/1*uqdh--nA4Z7FZwVpL6W2cQ.jpeg" /></figure><p>Tic tac toe game my favourite childhood game, when I was younger I used to play a lot with my brother. I have made this game in which we can have more than three columns and rows , infact we can make the board of length whatever we want.</p><p>App.js file in which we are rendering FirstBox</p><pre>import &quot;./styles.css&quot;;<br>import FirstBox from &quot;./firstBox&quot;;<br><br>export default function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;FirstBox numRow={15} /&gt;<br>    &lt;/div&gt;<br>  );<br><br>}</pre><p>FirstBox.js file in which we are importig useLudoBoard custom hook in which we have winner, arrayOfTicTacToe,resetLudoboad function, itemClicked function.</p><p>TicTacToe component is getting rendered here in which we are passing arrayOfTicTactoe and itemClicked as parameteres. We also have a reset button in which we are calling resetLudoBoard function.</p><p>At the last line we are showing the winner.</p><pre>import React from &quot;react&quot;;<br>import useLudoBoard from &quot;./useLudoBoard&quot;;<br>import TicTacToe from &quot;./ticTacToe&quot;;<br>import styles from &quot;./ticTacToeStyles&quot;;<br><br>function FirstBox(props) {<br>  const {<br>    winner,<br>    arrayOfTicTactoe,<br>    resetLudoBoard,<br>    itemClicked<br>  } = useLudoBoard(props);<br><br>  return (<br>    &lt;&gt;<br>      &lt;TicTacToe<br>        arrayOfTicTactoe={arrayOfTicTactoe}<br>        itemClicked={itemClicked}<br>      /&gt;<br>      &lt;div style={styles.resetBtnStyle}&gt;<br>        {&quot; &quot;}<br>        &lt;button<br>          onClick={() =&gt; {<br>            resetLudoBoard();<br>          }}<br>        &gt;<br>          Reset<br>        &lt;/button&gt;<br>      &lt;/div&gt;<br>      {winner &amp;&amp; &lt;div&gt;Winner is {winner}&lt;/div&gt;}<br>    &lt;/&gt;<br>  );<br>}<br><br>export default FirstBox;</pre><p>ticTacToe.js file in which are iterating through the array containing tictactoe elements and showing whether it’s “X” or “O”. Whenever any one of the item is clicked itemClicked function is called.</p><pre>import React from &quot;react&quot;;<br>import styles from &quot;./ticTacToeStyles&quot;;<br><br>const TicTacToe = (props) =&gt; {<br>  const { arrayOfTicTactoe, itemClicked } = props;<br>  return (<br>    &lt;&gt;<br>      {arrayOfTicTactoe.length &gt; 0 &amp;&amp;<br>        arrayOfTicTactoe.map((e, index1) =&gt; (<br>          &lt;div style={styles.boxRowStyle}&gt;<br>            {e.map((ele, index2) =&gt; (<br>              &lt;div<br>                style={styles.boxStyle}<br>                onClick={() =&gt; {<br>                  itemClicked(ele, index1, index2);<br>                }}<br>              &gt;<br>                {ele}<br>              &lt;/div&gt;<br>            ))}<br>          &lt;/div&gt;<br>        ))}<br>    &lt;/&gt;<br>  );<br>};<br><br>export default TicTacToe;</pre><p>ticTacToeStyles.js — file for styling all the elements of tic tac toe board.</p><pre>const styles = {<br>  boxRowStyle: {<br>    display: &quot;flex&quot;,<br>    flexDirection: &quot;row&quot;<br>  },<br>  boxStyle: {<br>    border: &quot;1px solid black&quot;,<br>    padding: 30,<br>    width: 20,<br>    height: 20<br>  },<br>  resetBtnStyle: {<br>    marginTop: 40<br>  }<br>};<br><br>export default styles;</pre><p>useLudoBoard.js file in which we are having following states and functions :-</p><ol><li>chanceOFPlayer- This represents a boolean value for knowing which player is having chance .</li><li>winner- This represents winner of the game</li><li>arrayOfTicTactoe — A 2-D array representing the whole board which contains null value initially</li><li>numRow- We are getting this from prop , represnting the size of the board.</li><li>resetLudoBoard- This function is called when users clicks on the reset button and whole 2-D array get filled with null, winner is set to empty string and chanceOfPlayer is set to false.</li><li>itemClicked — This function is called whenever user clicks on the board. At first in the function we are iterating the whole array and then whenever itemClicked indexes matches with the indexes of the ludoboard , if the chanceOfCurrentPlayer is true we update it to “X” otherwise we update it to “O”.</li><li>In this function we then iterate the whole array and check if the whole row contains the same value then we set winner to current player.</li><li>In this function we then iterate the whole array and check if the whole column contains the same value then we set winner to current player.</li><li>In this function we then iterate the whole array and check if the whole diagonal contains the same value then we set winner to current player.</li><li>In this function we then iterate the whole array and check if the whole anti-diagonal contains the same value then we set winner to current player.</li></ol><pre>import React, { useState, useEffect } from &quot;react&quot;;<br><br>function useLudoBoard(props) {<br>  const [chanceOFPlayer, setChanceOfPlayer] = useState(false);<br>  const [winner, setWinner] = useState(&quot;&quot;);<br>  const { numRow } = props;<br>  const [arrayOfTicTactoe, setArrayOfTicTacToe] = useState(<br>    new Array(numRow).fill(null).map((item) =&gt; new Array(numRow).fill(null))<br>  );<br><br>  const resetLudoBoard = () =&gt; {<br>    setArrayOfTicTacToe(<br>      new Array(numRow).fill(null).map((item) =&gt; new Array(numRow).fill(null))<br>    );<br>    setWinner(&quot;&quot;);<br>    setChanceOfPlayer(false);<br>  };<br><br>  const itemClicked = (ele, indexClicked1, indexClicked2) =&gt; {<br>    console.log(&quot;index1&quot;, indexClicked1, &quot;index2&quot;, indexClicked2);<br>    if (ele !== &quot;X&quot; || ele !== &quot;O&quot;) {<br>      const newArrayOfTicTactoe = [...arrayOfTicTactoe];<br>      newArrayOfTicTactoe.map((e, index1) =&gt; {<br>        e.map((element, index2) =&gt; {<br>          if (indexClicked1 === index1 &amp;&amp; indexClicked2 === index2) {<br>            if (!chanceOFPlayer) {<br>              newArrayOfTicTactoe[index1][index2] = &quot;X&quot;;<br>            } else {<br>              newArrayOfTicTactoe[index1][index2] = &quot;O&quot;;<br>            }<br>          }<br>        });<br>      });<br>      setArrayOfTicTacToe(newArrayOfTicTactoe);<br><br>      let countRow = 0;<br>      for (var j = 0; j &lt; numRow; j++) {<br>        countRow = 0;<br>        for (var i = 0; i &lt; numRow - 1; i++) {<br>          if (<br>            arrayOfTicTactoe[j][i] === arrayOfTicTactoe[j][i + 1] &amp;&amp;<br>            arrayOfTicTactoe[j][i] != null<br>          ) {<br>            countRow++;<br>            if (countRow === numRow - 1) {<br>              if (arrayOfTicTactoe[j][i] == &quot;X&quot;) {<br>                setWinner(&quot;X&quot;);<br>              } else {<br>                console.log(&quot;row is matching&quot;);<br>                setWinner(&quot;O&quot;);<br>              }<br>            }<br>          }<br>        }<br>      }<br>      let countColumn = 0;<br>      for (j = 0; j &lt; numRow; j++) {<br>        countColumn = 0;<br>        for (i = 0; i &lt; numRow - 1; i++) {<br>          if (<br>            arrayOfTicTactoe[i][j] === arrayOfTicTactoe[i + 1][j] &amp;&amp;<br>            arrayOfTicTactoe[i][j] != null<br>          ) {<br>            countColumn++;<br>          }<br>          if (countColumn === numRow - 1) {<br>            if (arrayOfTicTactoe[i][j] === &quot;X&quot;) {<br>              setWinner(&quot;X&quot;);<br>            } else {<br>              console.log(&quot;column is matching&quot;);<br>              setWinner(&quot;O&quot;);<br>            }<br>          }<br>        }<br>      }<br><br>      let countDiagonal = 0;<br>      let initialRow = 0,<br>        initialColumn = 0;<br>      while (initialRow &lt; numRow - 1 &amp;&amp; initialColumn &lt; numRow - 1) {<br>        console.log(&quot;initialRow&quot;, initialRow);<br>        console.log(&quot;initialColumn&quot;, initialColumn);<br>        if (<br>          arrayOfTicTactoe[initialRow][initialColumn] ===<br>            arrayOfTicTactoe[initialRow + 1][initialColumn + 1] &amp;&amp;<br>          arrayOfTicTactoe[initialRow][initialColumn] != null<br>        ) {<br>          countDiagonal++;<br>        }<br>        if (countDiagonal === numRow - 1) {<br>          if (arrayOfTicTactoe[initialRow][initialColumn] === &quot;X&quot;) {<br>            setWinner(&quot;X&quot;);<br>          } else {<br>            setWinner(&quot;O&quot;);<br>          }<br>        }<br>        initialRow++;<br>        initialColumn++;<br>      }<br><br>      let countReverseDiagonal = 0;<br>      let reverseRow = 0,<br>        reverseCol = numRow - 1;<br><br>      while (reverseRow &gt;= 0 &amp;&amp; reverseCol &gt; 0) {<br>        console.log(&quot;reverseRow&quot;, reverseRow);<br>        console.log(&quot;reverseCol&quot;, reverseCol);<br>        console.log(<br>          &quot;arrayOfTicTactoe[reverseRow][reverseCol]&quot;,<br>          arrayOfTicTactoe[reverseRow][reverseCol]<br>        );<br><br>        if (<br>          arrayOfTicTactoe[reverseRow][reverseCol] ===<br>            arrayOfTicTactoe[reverseRow + 1][reverseCol - 1] &amp;&amp;<br>          arrayOfTicTactoe[reverseRow][reverseCol] != null<br>        ) {<br>          console.log(&quot;matched&quot;);<br>          countReverseDiagonal++;<br>        }<br>        console.log(countReverseDiagonal);<br>        if (countReverseDiagonal === numRow - 1) {<br>          if (arrayOfTicTactoe[reverseRow][reverseCol] === &quot;X&quot;) {<br>            setWinner(&quot;X&quot;);<br>          } else {<br>            console.log(&quot;reverse diagonal is matching&quot;);<br>            setWinner(&quot;O&quot;);<br>          }<br>        }<br>        reverseRow++;<br>        reverseCol--;<br>      }<br><br>      setChanceOfPlayer(!chanceOFPlayer);<br>    }<br>  };<br>  return {<br>    chanceOFPlayer,<br>    winner,<br>    numRow,<br>    arrayOfTicTactoe,<br>    resetLudoBoard,<br>    itemClicked<br>  };<br>}<br><br>export default useLudoBoard;</pre><p>I hope that you enjoyed reading this code as much as you enjoy playing this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=694a364225ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Snake and ladder game]]></title>
            <link>https://medium.com/@ayushipriya_40647/snake-and-ladder-game-80ab5e3d0676?source=rss-da4bc77a971b------2</link>
            <guid isPermaLink="false">https://medium.com/p/80ab5e3d0676</guid>
            <dc:creator><![CDATA[Ayushi Priya]]></dc:creator>
            <pubDate>Sun, 11 Dec 2022 08:54:51 GMT</pubDate>
            <atom:updated>2022-12-11T08:54:51.374Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wcF46dkyUO2mBkXWUc6xKA.png" /></figure><p>Snake and ladder game all from scartch in react hooks , I hope that you will love making this game with the help of this blog. Here the straight line represents a ladder and the black one represents a snake.</p><p>During my childhood my father had a Nokia mobile phone in which I used to play snake game in which snake gets longer and longer by eating the food and by at the end the snake would die when it becomes very long and eat itself.</p><p>Today I feel amazed that I am able to build such type of games on my own.</p><p>App.js</p><pre>import logo from &#39;./logo.svg&#39;;<br>import &#39;./App.css&#39;;<br>import SnakeAndLadder from &#39;./SnakeAndLadder&#39;;<br><br>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;SnakeAndLadder nextStep={10} /&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>SnakeAndLadder.js</p><pre>import React, { useState, useEffect } from &#39;react&#39;;<br>import useSnakeLadder from &#39;./useSnakeLadder&#39;;<br>import styles from &#39;./snakeAndLadderStyle&#39;;<br>import SnakeAndLadderBoard from &#39;./snakeLadderBoard&#39;;<br><br>function SnakeAndLadder(props) {<br>  const {<br>    playerAIndex1,<br>    playerAIndex2,<br>    playerBIndex1,<br>    playerBIndex2,<br>    diceDotPlayer1,<br>    player,<br>    winner,<br>    showDotInDice,<br>    diceClicked,<br>    snakeAndLadderArray,<br>  } = useSnakeLadder(props);<br><br>  return (<br>    &lt;&gt;<br>      &lt;div style={styles.boardStyle}&gt;<br>        &lt;SnakeAndLadderBoard<br>          snakeAndLadderArray={snakeAndLadderArray}<br>          playerAIndex1={playerAIndex1}<br>          playerAIndex2={playerAIndex2}<br>          playerBIndex1={playerBIndex1}<br>          playerBIndex2={playerBIndex2}<br>        /&gt;<br>        &lt;div style={styles.playerStyle}&gt;Player {player}&lt;/div&gt;<br>        &lt;div<br>          onClick={() =&gt; {<br>            diceClicked();<br>          }}<br>          style={styles.diceStyle}<br>        &gt;<br>          {diceDotPlayer1 &amp;&amp; showDotInDice(diceDotPlayer1)}<br>        &lt;/div&gt;<br>        {winner &amp;&amp; (<br>          &lt;div&gt;<br>            Winner is &lt;b&gt;Player{winner}&lt;/b&gt;<br>          &lt;/div&gt;<br>        )}<br>      &lt;/div&gt;<br>    &lt;/&gt;<br>  );<br>}<br><br>export default SnakeAndLadder;</pre><p>playerAIndex1,playerAIndex2- representing the indexes of first player.</p><p>playerBIndex1, playerBIndex2 — representing the indexes of second player.</p><p>diceDotPlayer1 — showing the number on dice</p><p>player- current player</p><p>winner- current winner</p><p>snakeAndLadderArray- representing array in which we are showing the snake and ladder game</p><p>SnakeLadderBoard.js</p><pre>import React from &#39;react&#39;;<br>import styles from &#39;./snakeAndLadderStyle&#39;;<br><br>const SnakeAndLadderBoard = (props) =&gt; {<br>  const {<br>    snakeAndLadderArray,<br>    playerAIndex1,<br>    playerAIndex2,<br>    playerBIndex1,<br>    playerBIndex2,<br>  } = props;<br>  return (<br>    &lt;&gt;<br>      {snakeAndLadderArray &amp;&amp;<br>        snakeAndLadderArray[0][0] !== &#39;&#39; &amp;&amp;<br>        snakeAndLadderArray.map((ele, index1) =&gt; (<br>          &lt;div style={styles.boxRowStyle}&gt;<br>            {ele.map((element, index2) =&gt; {<br>              if (index1 === playerAIndex1 &amp;&amp; index2 === playerAIndex2) {<br>                return (<br>                  &lt;&gt;<br>                    &lt;div style={styles.elementStyle}&gt;<br>                      &lt;div style={styles.sizeOfDot}&gt;&lt;/div&gt;<br>                    &lt;/div&gt;<br>                  &lt;/&gt;<br>                );<br>              }<br>              if (index1 === playerBIndex1 &amp;&amp; index2 === playerBIndex2) {<br>                return (<br>                  &lt;&gt;<br>                    &lt;div style={styles.elementStyle}&gt;<br>                      &lt;div style={styles.secondDot}&gt;&lt;/div&gt;<br>                    &lt;/div&gt;<br>                  &lt;/&gt;<br>                );<br>              }<br>              return (<br>                &lt;div style={styles.elementStyle}&gt;<br>                  {snakeAndLadderArray[index1][index2]}<br>                &lt;/div&gt;<br>              );<br>            })}<br>          &lt;/div&gt;<br>        ))}<br>      &lt;div style={styles.lineMaintainStyle}&gt;&lt;/div&gt;<br>      &lt;div style={styles.curvedLine}&gt;&lt;/div&gt;<br>      &lt;div style={styles.curvedLine1}&gt;&lt;/div&gt;<br>      &lt;div style={styles.curvedLine2}&gt;&lt;/div&gt;<br>      &lt;div style={styles.curvedLine3}&gt;&lt;/div&gt;<br>      &lt;div style={styles.curvedLine4}&gt;&lt;/div&gt;<br>      &lt;div style={styles.curvedLine5}&gt;&lt;/div&gt;<br>      &lt;div style={styles.curvedLine6}&gt;&lt;/div&gt;<br>      &lt;div style={styles.curvedLine7}&gt;&lt;/div&gt;<br>      &lt;div style={styles.straightLine}&gt;&lt;/div&gt;<br>      &lt;div style={styles.straightLine1}&gt;&lt;/div&gt;<br>      &lt;div style={styles.straightLine2}&gt;&lt;/div&gt;<br>      &lt;div style={styles.straightLine3}&gt;&lt;/div&gt;<br>      &lt;div style={styles.straightLine4}&gt;&lt;/div&gt;<br>      &lt;div style={styles.straightLine5}&gt;&lt;/div&gt;<br>      &lt;div style={styles.straightLine6}&gt;&lt;/div&gt;<br>    &lt;/&gt;<br>  );<br>};<br><br>export default SnakeAndLadderBoard;</pre><ol><li>At first iterating through whole array and checking if the current array index is equivalent to the index of the players.</li><li>If the current player index is equal to the array indexes then displaying the player via &lt;div style={styles.sizeOfDot}&gt;&lt;/div&gt; for first player and &lt;div style={styles.secondDot}&gt;&lt;/div&gt; for second player.</li><li>If the indexes donot match then representing the element through &lt;div style={styles.elementStyle}&gt;<br> {snakeAndLadderArray[index1][index2]}<br> &lt;/div&gt;</li><li>curvedLine, curvedLine1, curvedLine2,…., curvedLine7 displays the eight snakes on the board</li><li>straightLine, straightLine1,… straightLine6 displays the seven straight lines as “ladders” on the board.</li></ol><p>snakeAndLadderStyle.js</p><pre>const styles = {<br>  boxRowStyle: {<br>    display: &#39;flex&#39;,<br>    flexDirection: &#39;row&#39;,<br>  },<br>  elementStyle: {<br>    border: &#39;1px solid black&#39;,<br>    width: 50,<br>    height: 50,<br>    display: &#39;flex&#39;,<br>    justifyContent: &#39;center&#39;,<br>    alignItems: &#39;center&#39;,<br>  },<br>  sizeOfDot: {<br>    height: 25,<br>    width: 25,<br>    backgroundColor: &#39;#bbb&#39;,<br>    borderRadius: &#39;50%&#39;,<br>  },<br>  secondDot: {<br>    backgroundColor: &#39;red&#39;,<br>    height: 25,<br>    width: 25,<br>    borderRadius: &#39;50%&#39;,<br>  },<br>  diceStyle: {<br>    border: &#39;1px solid black&#39;,<br>    padding: 30,<br>    width: 20,<br>    height: 20,<br>    marginTop: 10,<br>    marginLeft: 10,<br>  },<br>  smallDot: {<br>    backgroundColor: &#39;black&#39;,<br>    height: 10,<br>    width: 10,<br>    borderRadius: &#39;50%&#39;,<br>  },<br>  dotsDisplay: {<br>    display: &#39;flex&#39;,<br>    justifyContent: &#39;space-between&#39;,<br>    width: 30,<br>  },<br>  dotsFourDisplay: {<br>    marginTop: 10,<br>  },<br>  playerStyle: {<br>    display: &#39;flex&#39;,<br>  },<br>  lineStyle: {<br>    stroke: &#39;red&#39;,<br>    strokeWidth: 2,<br>  },<br>  boardStyle: {<br>    padding: 20,<br>    margin: 20,<br>  },<br>  redLine: {<br>    stroke: &#39;red&#39;,<br>    strokeWidth: 2,<br>  },<br>  lineMaintainStyle: {<br>    width: &#39;80%&#39;,<br>    margin: &#39;auto&#39;,<br>    marginTop: &#39;5%&#39;,<br>    marginBottom: &#39;5%&#39;,<br>    transform: &#39;rotate(-25deg)&#39;,<br>    borderColor: &#39;#1A85FD&#39;,<br>  },<br>  midDotStyle: {<br>    display: &#39;flex&#39;,<br>    alignSelf: &#39;center&#39;,<br>    backgroundColor: &#39;black&#39;,<br>    height: 10,<br>    width: 10,<br>    borderRadius: &#39;50%&#39;,<br>  },<br>  curvedLine: {<br>    width: 300,<br>    height: 100,<br>    border: &#39;solid 5px #000&#39;,<br>    borderColor: &#39;transparent transparent #000 transparent&#39;,<br>    borderRadius: &#39;0 0 240px 50%/60px&#39;,<br>    position: &#39;absolute&#39;,<br>    transform: &#39;rotate(90deg)&#39;,<br>    top: 300,<br>    bottom: 20,<br>  },<br>  curvedLine1: {<br>    width: 300,<br>    height: 100,<br>    border: &#39;solid 5px #000&#39;,<br>    borderColor: &#39;transparent transparent #000 transparent&#39;,<br>    borderRadius: &#39;0 0 240px 50%/60px&#39;,<br>    position: &#39;absolute&#39;,<br>    transform: &#39;rotate(110deg)&#39;,<br>    top: 130,<br>    bottom: 20,<br>    left: 180,<br>  },<br>  curvedLine2: {<br>    width: 280,<br>    height: 60,<br>    border: &#39;solid 5px #000&#39;,<br>    borderColor: &#39;transparent transparent #000 transparent&#39;,<br>    borderRadius: &#39;0 0 240px 50%/60px&#39;,<br>    position: &#39;absolute&#39;,<br>    transform: &#39;rotate(-10deg)&#39;,<br>    top: 100,<br>    bottom: 20,<br>    left: 120,<br>  },<br>  curvedLine3: {<br>    width: 430,<br>    height: 300,<br>    border: &#39;solid 5px #000&#39;,<br>    borderColor: &#39;transparent transparent #000 transparent&#39;,<br>    borderRadius: &#39;0 0 240px 40%/60px&#39;,<br>    position: &#39;absolute&#39;,<br>    transform: &#39;rotate(120deg)&#39;,<br>    top: 180,<br>    bottom: 20,<br>    left: 210,<br>  },<br>  curvedLine4: {<br>    width: 200,<br>    height: 70,<br>    border: &#39;solid 5px #000&#39;,<br>    borderColor: &#39;transparent transparent #000 transparent&#39;,<br>    borderRadius: &#39;0 0 240px 40%/60px&#39;,<br>    position: &#39;absolute&#39;,<br>    transform: &#39;rotate(-110deg)&#39;,<br>    top: 420,<br>    bottom: 20,<br>    left: 180,<br>  },<br>  curvedLine5: {<br>    width: 200,<br>    height: 70,<br>    border: &#39;solid 5px #000&#39;,<br>    borderColor: &#39;transparent transparent #000 transparent&#39;,<br>    borderRadius: &#39;0 0 240px 40%/60px&#39;,<br>    position: &#39;absolute&#39;,<br>    transform: &#39;rotate(-110deg)&#39;,<br>    top: 420,<br>    bottom: 20,<br>    left: 180,<br>  },<br>  curvedLine6: {<br>    width: 200,<br>    height: 100,<br>    border: &#39;solid 5px #000&#39;,<br>    borderColor: &#39;transparent transparent #000 transparent&#39;,<br>    borderRadius: &#39;0 0 240px 40%/60px&#39;,<br>    position: &#39;absolute&#39;,<br>    transform: &#39;rotate(130deg)&#39;,<br>    top: 350,<br>    bottom: 0,<br>    left: 300,<br>  },<br>  curvedLine7: {<br>    width: 200,<br>    height: 100,<br>    border: &#39;solid 5px #000&#39;,<br>    borderColor: &#39;transparent transparent #000 transparent&#39;,<br>    borderRadius: &#39;0 0 250px 60%/90px&#39;,<br>    position: &#39;absolute&#39;,<br>    transform: &#39;rotate(80deg)&#39;,<br>    top: 400,<br>    bottom: 0,<br>    left: 440,<br>  },<br>  straightLine: {<br>    borderLeft: &#39;6px solid green&#39;,<br>    height: 150,<br>    position: &#39;absolute&#39;,<br>    left: 100,<br>    top: 43,<br>    transform: &#39;rotate(200deg)&#39;,<br>  },<br>  straightLine1: {<br>    borderLeft: &#39;6px solid green&#39;,<br>    height: 150,<br>    position: &#39;absolute&#39;,<br>    left: 100,<br>    top: 300,<br>    transform: &#39;rotate(200deg)&#39;,<br>  },<br>  straightLine2: {<br>    borderLeft: &#39;6px solid green&#39;,<br>    height: 200,<br>    position: &#39;absolute&#39;,<br>    left: 150,<br>    top: 350,<br>    transform: &#39;rotate(20deg)&#39;,<br>  },<br>  straightLine3: {<br>    borderLeft: &#39;6px solid green&#39;,<br>    height: 150,<br>    position: &#39;absolute&#39;,<br>    left: 300,<br>    top: 430,<br>    transform: &#39;rotate(250deg)&#39;,<br>  },<br>  straightLine4: {<br>    borderLeft: &#39;6px solid green&#39;,<br>    height: 150,<br>    position: &#39;absolute&#39;,<br>    left: 480,<br>    top: 400,<br>    transform: &#39;rotate(230deg)&#39;,<br>  },<br>  straightLine5: {<br>    borderLeft: &#39;6px solid green&#39;,<br>    height: 200,<br>    position: &#39;absolute&#39;,<br>    left: 450,<br>    top: 180,<br>    transform: &#39;rotate(-240deg)&#39;,<br>  },<br>  straightLine6: {<br>    borderLeft: &#39;6px solid green&#39;,<br>    height: 150,<br>    position: &#39;absolute&#39;,<br>    left: 500,<br>    top: 43,<br>    transform: &#39;rotate(-210deg)&#39;,<br>  },<br>};<br><br>export default styles;</pre><p>useSnakeAndLadder.js</p><pre>import React, { useEffect, useState } from &#39;react&#39;;<br>import styles from &#39;./snakeAndLadderStyle&#39;;<br><br>function useSnakeLadder(props) {<br>  const [snakeAndLadderArray, setSnakeAndLadderArray] = useState(<br>    new Array(10).fill(null).map((item) =&gt; new Array(10).fill(&#39;&#39;))<br>  );<br>  const [jumpTo, setJumpTo] = useState(0);<br>  const [playerAIndex1, setPlayerAIndex1] = useState(9);<br>  const [playerAIndex2, setPlayerAIndex2] = useState(0);<br>  const [playerBIndex1, setPlayerBIndex1] = useState(9);<br>  const [playerBIndex2, setPlayerBIndex2] = useState(0);<br>  const [diceDotPlayer1, setDiceDotPlayer2] = useState(5);<br>  const [player, setPlayer] = useState(1);<br>  const [playerAValue, setPlayerAValue] = useState(1);<br>  const [playerBValue, setPlayerBValue] = useState(1);<br>  const [winner, setWinner] = useState(null);<br>  const [snakeVal, setSnakeVal] = useState([63, 95, 87, 88, 36, 48, 32]);<br><br>  const lineBetweenTwo = (start, end) =&gt; {};<br><br>  useEffect(() =&gt; {<br>    const timer = setTimeout(() =&gt; {<br>      console.log(&#39;playerAValue&#39;, playerAValue);<br>      console.log(&#39;playerBValue&#39;, playerBValue);<br><br>      if (playerBValue === 8) {<br>        console.log(&#39;hello I am 8&#39;);<br>      }<br><br>      if (playerAValue === 8 || playerBValue === 8) {<br>        console.log(&#39;player value &#39;, playerBValue);<br>        updateJumpTo(30);<br>      }<br><br>      if (playerAValue === 63 || playerBValue === 63) {<br>        updateJumpTo(18);<br>      } else if (playerAValue === 95 || playerBValue === 95) {<br>        updateJumpTo(56);<br>      } else if (playerAValue === 87 || playerBValue === 87) {<br>        updateJumpTo(78);<br>      } else if (playerAValue === 88 || playerBValue === 88) {<br>        updateJumpTo(24);<br>      } else if (playerAValue === 36 || playerBValue === 36) {<br>        updateJumpTo(6);<br>      } else if (playerAValue === 48 || playerBValue === 48) {<br>        updateJumpTo(26);<br>      } else if (playerAValue === 32 || playerBValue === 32) {<br>        updateJumpTo(10);<br>      } else if (playerAValue === 80 || playerBValue === 80) {<br>        updateJumpTo(99);<br>      } else if (playerAValue === 21 || playerBValue === 21) {<br>        updateJumpTo(42);<br>      } else if (playerAValue === 2 || playerBValue === 2) {<br>        updateJumpTo(38);<br>      } else if (playerAValue === 4 || playerBValue === 4) {<br>        updateJumpTo(14);<br>      } else if (playerAValue === 8 || playerBValue === 8) {<br>        updateJumpTo(30);<br>      } else if (playerAValue === 50 || playerBValue === 50) {<br>        updateJumpTo(67);<br>      } else if (playerAValue === 71 || playerBValue === 71) {<br>        updateJumpTo(92);<br>      }<br>    }, 1000);<br>    return () =&gt; clearTimeout(timer);<br>  }, [playerBValue, playerAValue]);<br><br>  useEffect(() =&gt; {<br>    function settingArray() {<br>      var numP = 100;<br>      var numQ = 81;<br>      const newSnakeAndLadder = [...snakeAndLadderArray];<br>      newSnakeAndLadder &amp;&amp;<br>        newSnakeAndLadder.map((ele, index1) =&gt; {<br>          {<br>            ele.map((element, index2) =&gt; {<br>              if (index1 % 2 === 0) {<br>                newSnakeAndLadder[index1][index2] = numP--;<br>              } else {<br>                newSnakeAndLadder[index1][index2] = numQ++;<br>                if (index2 === 9) {<br>                  numQ = numQ - 30;<br>                }<br>                numP--;<br>              }<br>            });<br>          }<br>        });<br>      // setPlayerAIndex(0);<br>      // lineBetweenTwo(2, 23);<br>      setSnakeAndLadderArray(newSnakeAndLadder);<br>    }<br><br>    settingArray();<br>  }, []);<br><br>  const updateJumpTo = (index) =&gt; {<br>    if (index &gt; 100) {<br>      if (player === 1) {<br>        setPlayer(2);<br>      } else {<br>        setPlayer(1);<br>      }<br>      return;<br>    }<br>    if (player === 1) {<br>      setPlayerAValue(index);<br>      if (index === 100) {<br>        setWinner(1);<br>      }<br>    } else {<br>      setPlayerBValue(index);<br>      if (index === 100) {<br>        setWinner(2);<br>      }<br>    }<br>    {<br>      snakeAndLadderArray &amp;&amp;<br>        snakeAndLadderArray.map((ele, index1) =&gt; {<br>          ele.map((element, index2) =&gt; {<br>            if (snakeAndLadderArray[index1][index2] === index) {<br>              if (player == 1) {<br>                setPlayerAIndex1(index1);<br>                setPlayerAIndex2(index2);<br>                if (<br>                  index !== 63 &amp;&amp;<br>                  index !== 95 &amp;&amp;<br>                  index !== 87 &amp;&amp;<br>                  index !== 88 &amp;&amp;<br>                  index !== 36 &amp;&amp;<br>                  index !== 48 &amp;&amp;<br>                  index !== 32 &amp;&amp;<br>                  index !== 80 &amp;&amp;<br>                  index !== 21 &amp;&amp;<br>                  index !== 2 &amp;&amp;<br>                  index !== 4 &amp;&amp;<br>                  index !== 8 &amp;&amp;<br>                  index !== 50 &amp;&amp;<br>                  index !== 71<br>                ) {<br>                  setPlayer(2);<br>                }<br>              } else {<br>                setPlayerBIndex1(index1);<br>                setPlayerBIndex2(index2);<br>                if (<br>                  index !== 63 &amp;&amp;<br>                  index !== 95 &amp;&amp;<br>                  index !== 87 &amp;&amp;<br>                  index !== 88 &amp;&amp;<br>                  index !== 36 &amp;&amp;<br>                  index !== 48 &amp;&amp;<br>                  index !== 32 &amp;&amp;<br>                  index !== 80 &amp;&amp;<br>                  index !== 21 &amp;&amp;<br>                  index !== 2 &amp;&amp;<br>                  index !== 4 &amp;&amp;<br>                  index !== 8 &amp;&amp;<br>                  index !== 50 &amp;&amp;<br>                  index !== 71<br>                ) {<br>                  setPlayer(1);<br>                }<br>              }<br><br>              return;<br>            }<br>          });<br>        });<br>    }<br>  };<br><br>  const showDotInDice = (dotNum) =&gt; {<br>    let jsxEle;<br>    console.log(dotNum);<br>    switch (dotNum) {<br>      case 1:<br>        jsxEle = &lt;div style={styles.smallDot}&gt;&lt;/div&gt;;<br>        break;<br>      case 2:<br>        jsxEle = (<br>          &lt;div style={styles.dotsDisplay}&gt;<br>            {&#39; &#39;}<br>            &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>            &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>          &lt;/div&gt;<br>        );<br>        break;<br>      case 3:<br>        jsxEle = (<br>          &lt;div style={styles.dotsDisplay}&gt;<br>            {&#39; &#39;}<br>            &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>            &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>            &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>          &lt;/div&gt;<br>        );<br>        break;<br>      case 4:<br>        jsxEle = (<br>          &lt;div style={styles.dotsDisplay}&gt;<br>            {&#39; &#39;}<br>            &lt;div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>            &lt;/div&gt;<br>            &lt;div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>            &lt;/div&gt;<br>          &lt;/div&gt;<br>        );<br>        break;<br>      case 5:<br>        jsxEle = (<br>          &lt;div style={styles.dotsDisplay}&gt;<br>            &lt;div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>            &lt;/div&gt;<br>            &lt;div style={styles.midDotStyle}&gt;&lt;/div&gt;<br>            &lt;div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>            &lt;/div&gt;<br>          &lt;/div&gt;<br>        );<br>        break;<br>      case 6:<br>        jsxEle = (<br>          &lt;div style={styles.dotsDisplay}&gt;<br>            {&#39; &#39;}<br>            &lt;div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>            &lt;/div&gt;<br>            &lt;div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>              &lt;div style={styles.smallDot}&gt;&lt;/div&gt;<br>            &lt;/div&gt;<br>          &lt;/div&gt;<br>        );<br>        break;<br><br>      default:<br>        jsxEle = &lt;div style={styles.smallDot}&gt;&lt;/div&gt;;<br>        break;<br>    }<br><br>    return jsxEle;<br>  };<br><br>  const diceClicked = () =&gt; {<br>    var randNum = Math.floor(Math.random() * 6) + 1;<br>    let playerVal;<br>    setDiceDotPlayer2(randNum);<br>    if (player === 1) {<br>      playerVal = playerAValue;<br>    } else {<br>      playerVal = playerBValue;<br>    }<br>    let nextElement = playerVal + randNum;<br><br>    let elementTimer = setTimeout(() =&gt; {<br>      updateJumpTo(nextElement);<br>    }, 2000);<br>  };<br><br>  return {<br>    jumpTo,<br>    setJumpTo,<br>    playerAIndex1,<br>    setPlayerAIndex1,<br>    playerAIndex2,<br>    setPlayerAIndex2,<br>    playerBIndex1,<br>    setPlayerBIndex1,<br>    playerBIndex2,<br>    setPlayerBIndex2,<br>    diceDotPlayer1,<br>    setDiceDotPlayer2,<br>    player,<br>    setPlayer,<br>    playerAValue,<br>    setPlayerAValue,<br>    playerBValue,<br>    setPlayerBValue,<br>    winner,<br>    setWinner,<br>    snakeVal,<br>    setSnakeVal,<br>    updateJumpTo,<br>    showDotInDice,<br>    diceClicked,<br>    snakeAndLadderArray,<br>  };<br>}<br><br>export default useSnakeLadder;</pre><p>useSnakeAndLadder.js is a custom hook with the help of which we will handle all the logics behind snake and ladder game</p><ol><li>snakeAndLadderArray — This represents a 2-D array 10*10 which has initial value of empty string.</li><li>playerAIndex1- This represents the what row number player1 is present which is (9) initially.</li><li>playerAIndex2- This represents the what column number player1 is present which is (0) initially.</li><li>playerBIndex1- This represents the what row number player2 is present which is (9) initially.</li><li>playerBIndex2- This represents the what column number player2 is present which is (0) initially.</li><li>diceDotPlayer1- This represents the initial number in the dice.(5)</li><li>player- This represents which player will go for it’s turn.</li><li>playerAValue- This represents at which box the first player is present and what is the value of that box</li><li>playerBValue- This represents at which box the second player is present and what is the value of that box.</li><li>winner- This represents the winner</li><li>First useEffect this helps us handling all the cases where we have to handle the case whenever players encounter a ladder or a snake. updateJumpTo function helps in updating the current value of the player. The if block checks if the current Player value needs to be updated.</li><li>setTimeOut is used in this usEffect so that we can delay the process of changing the player’s value from one index to another and actually see how it is getting updated.</li><li>Second useEffect set the value of our whole snakeLadderBoard.</li><li>Here we have two variables numP and numQ . numP is used for setting the values of the row which is at even index. When we will look at the board closely the even index row are having values which are decreasing by one. (100,99,98,….91).</li><li>numQ is used for setting the values of row which is at odd index. Which is starting at 81 (that’s why initializing numQ by 81) and increasing one by one at a time (81, 82, 83,…, 90) and when the next odd index will start it will again be decreased by 30 . As we can see in the board at index 3 the values are (61,62,…,70).</li><li>updateJumpTo function This function updates the indexes of the current player and set the next player.</li><li>If (index&gt;100) indexes of the current player will not be updated . It will remain on the place where it was present earlier.</li><li>If index is 100 set the current player as winner 🏆</li><li>Loop through snakeAndLadderArray and set the index of the current player to the index of the final Value where player has to move .</li><li>Check whether the finalValue where player has to move doesnot concide with the final value when the player encounter snake or ladder then only toggle the player.</li><li>showDotInDice is used to represent the dots on the dice</li><li>diceClicked- The function takes the random number and set that value to the dice.</li><li>Calculate the nextElement using the currentPlayerValuer and the diceValue(randNum) and update that using updateJumpTo function.</li><li>setTimeout is used here to make the process of changing the player’s position more clear.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80ab5e3d0676" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Comment Box Implementation]]></title>
            <link>https://medium.com/@ayushipriya_40647/comment-box-implementation-9bf1bdd87ca3?source=rss-da4bc77a971b------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bf1bdd87ca3</guid>
            <dc:creator><![CDATA[Ayushi Priya]]></dc:creator>
            <pubDate>Wed, 02 Nov 2022 05:36:39 GMT</pubDate>
            <atom:updated>2022-11-02T06:34:09.777Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*BbN2tKV_8X8G2-VJ06fx2Q.jpeg" /></figure><p>I am here with my code to in which user can add as much as possible comments and replies to each and every comments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*60_gi1xDIfJHWEKxLrNwBA.png" /></figure><p>We can replies to any of the comments and then again add replies where ever we want to.</p><p>I have used Context for maintaining the state of the comments, replies throughout the project. It helps passing data throughout the component tree.</p><p>The JSON which I have used for storing the values is as follows</p><pre>[{<br>id:&#39;&#39;,<br>messages:,<br>replies:[<br>{<br>id:&#39;&#39;,<br>messages:,<br>},<br>{<br>id:&#39;&#39;,<br>messages:,<br>replies:[]<br>}<br>}]</pre><p>App.js</p><pre>import logo from &#39;./logo.svg&#39;;</pre><pre>import &#39;./App.css&#39;;</pre><pre>import Comment from &#39;./commentSection&#39;;</pre><pre>import { CommentProvider } from &#39;./commentContext&#39;;</pre><pre>function App() {</pre><pre>return (</pre><pre>&lt;div className=&quot;App&quot;&gt;</pre><pre>&lt;CommentProvider&gt;</pre><pre>&lt;Comment /&gt;</pre><pre>&lt;/CommentProvider&gt;</pre><pre>&lt;/div&gt;</pre><pre>);</pre><pre>}</pre><pre>export default App;</pre><p>I will be showing how I have used context for storing the above mentioned data.</p><p>A function CommentProvider in which we are having CommentContext for storing values and any component can subscribe to these values via closest matching Provider above it in the tree.</p><p>I have used useReducer for retrieving the values in the localState so that the data persists in the webpage even if user refreshes the page .</p><p>The data will be stored in local Storage and we can retrieve that data through it.</p><p>commentContext.js</p><pre>import React, { useEffect, useReducer } from &quot;react&quot;;</pre><pre>let reducer = (commentValue, newCommentValue) =&gt; {</pre><pre>return newCommentValue;</pre><pre>};</pre><pre>const initialComment = [];</pre><pre>const localState = JSON.parse(localStorage.getItem(&quot;initialComment&quot;));</pre><pre>const CommentContext = React.createContext();</pre><pre>function CommentProvider(props) {</pre><pre>const [commentValue, setCommentValue] = useReducer(</pre><pre>reducer,</pre><pre>localState || initialComment</pre><pre>);</pre><pre>useEffect(() =&gt; {</pre><pre>localStorage.setItem(&quot;initialComment&quot;, JSON.stringify(commentValue));</pre><pre>}, [commentValue]);</pre><pre>return (</pre><pre>&lt;CommentContext.Provider value={{ commentValue, setCommentValue }}&gt;</pre><pre>{props.children}</pre><pre>&lt;/CommentContext.Provider&gt;</pre><pre>);</pre><pre>}</pre><pre>export { CommentContext, CommentProvider };</pre><pre>CommentSection.js</pre><p>Comment Function which initially renders Comment value and when user clicks on the Comment value they will get an input box and submit btn .</p><p>Upon clicking submit button “handleCommentSubmit” function is called in which we are storing the values in an object and then updating the commentValue state.</p><p>ReplyComment function is rendered if we commentValue and we are passing handleReplySubmit function in it so that user can enter their replies in it .</p><p>updateTheMessages function checks each and every object of the commentValue if it matches with id of the element which was clicked then we are adding that reply to that element ,if the message element contains more replies we are going deeper into it for checking the id.</p><pre>import React, { useState, useEffect, useContext } from &#39;react&#39;;</pre><pre>import { CommentContext } from &#39;./commentContext&#39;;</pre><pre>import ReplyComment from &#39;./replyComment&#39;;</pre><pre>export default function Comment() {</pre><pre>const [comment, setComment] = useState(false);</pre><pre>const [commentByUser, setCommentByUser] = useState(&#39;&#39;);</pre><pre>const { commentValue, setCommentValue } = useContext(CommentContext);</pre><pre>function updateTheMessages(newCommentValue, id, obj) {</pre><pre>if (newCommentValue.length === 0) {</pre><pre>return &#39;&#39;;</pre><pre>} else {</pre><pre>return newCommentValue.map((ele) =&gt; {</pre><pre>var temp = Object.assign({}, ele);</pre><pre>if (temp.id === id) {</pre><pre>console.log(&#39;id matched&#39;);</pre><pre>const arr = [];</pre><pre>arr.push(obj);</pre><pre>if (temp.replies) {</pre><pre>var newReplies = temp.replies;</pre><pre>newReplies.unshift(obj);</pre><pre>temp.replies = newReplies;</pre><pre>} else {</pre><pre>temp.replies = [obj];</pre><pre>}</pre><pre>} else if (temp.replies) {</pre><pre>temp.replies = updateTheMessages(ele.replies, id, obj);</pre><pre>}</pre><pre>return temp;</pre><pre>});</pre><pre>}</pre><pre>}</pre><pre>function handleCommentSubmit() {</pre><pre>var obj = {};</pre><pre>var arr = [];</pre><pre>obj.id = &#39;_&#39; + Math.random.toString(36).substr(2, 9);</pre><pre>obj.messages = commentByUser;</pre><pre>arr.push(obj);</pre><pre>setCommentValue([].concat(commentValue, arr));</pre><pre>setCommentByUser(&#39;&#39;);</pre><pre>}</pre><pre>function handleReplySubmit(e, reply, obj) {</pre><pre>const id = e.target.getAttribute(&#39;data-id&#39;);</pre><pre>let newCommentValue = [...commentValue];</pre><pre>newCommentValue = updateTheMessages(newCommentValue, id, obj);</pre><pre>setCommentValue(newCommentValue);</pre><pre>}</pre><pre>return (</pre><pre>&lt;div&gt;</pre><pre>&lt;div</pre><pre>onClick={() =&gt; {</pre><pre>setComment(true);</pre><pre>}}</pre><pre>&gt;</pre><pre>Comment</pre><pre>&lt;/div&gt;</pre><pre>{commentValue &amp;&amp; comment &amp;&amp; (</pre><pre>&lt;ReplyComment</pre><pre>commentValue={commentValue}</pre><pre>handleReplySubmit={handleReplySubmit}</pre><pre>/&gt;</pre><pre>)}</pre><pre>{comment &amp;&amp; (</pre><pre>&lt;div&gt;</pre><pre>&lt;input</pre><pre>value={commentByUser}</pre><pre>onChange={(e) =&gt; {</pre><pre>setCommentByUser(e.target.value);</pre><pre>}}</pre><pre>/&gt;</pre><pre>&lt;button onClick={handleCommentSubmit}&gt; Submit&lt;/button&gt;</pre><pre>&lt;/div&gt;</pre><pre>)}</pre><pre>&lt;/div&gt;</pre><pre>);</pre><pre>}</pre><p>messageElement.js</p><p>A message element to show an input box and a Submit button and here reply section is also there, whenever user clicks on this reply section an input box and a submit btn appears where user can enter their reply and submit it.</p><p>function handleSubmit where an object is getting created in which we are adding id and messages.</p><pre>import React, { useState } from &#39;react&#39;;</pre><pre>export default function Message(props) {</pre><pre>const [inputBoxReply, showInputBpx] = useState(false);</pre><pre>const [reply, setReply] = useState(&#39;&#39;);</pre><pre>const ele = props.element;</pre><pre>function handleSubmit(e) {</pre><pre>showInputBpx(false);</pre><pre>const obj = {};</pre><pre>obj.messages = reply;</pre><pre>obj.id = &#39;_&#39; + Math.random().toString(36).substr(2, 9);</pre><pre>props.handleReplySubmit(e, reply, obj);</pre><pre>setReply(&#39;&#39;);</pre><pre>}</pre><pre>return (</pre><pre>&lt;div key={ele.id}&gt;</pre><pre>&lt;div&gt;{ele.messages}&lt;/div&gt;</pre><pre>&lt;div</pre><pre>onClick={() =&gt; {</pre><pre>showInputBpx(true);</pre><pre>}}</pre><pre>&gt;</pre><pre>REPLY</pre><pre>&lt;/div&gt;</pre><pre>{inputBoxReply &amp;&amp; (</pre><pre>&lt;div&gt;</pre><pre>&lt;input</pre><pre>value={reply}</pre><pre>onChange={(e) =&gt; {</pre><pre>setReply(e.target.value);</pre><pre>}}</pre><pre>/&gt;</pre><pre>&lt;button</pre><pre>data-id={ele.id}</pre><pre>onClick={(e) =&gt; {</pre><pre>handleSubmit(e);</pre><pre>}}</pre><pre>&gt;</pre><pre>SUBMIT</pre><pre>&lt;/button&gt;</pre><pre>&lt;/div&gt;</pre><pre>)}</pre><pre>&lt;/div&gt;</pre><pre>);</pre><pre>}</pre><p>replyComment.js</p><p>ReplyComment function which initially renders Message element in which the message is shown to the user via above mentioned component and if the current element contains replies then it ReplyComment function will be recursively called and the message will be shown to the user.</p><pre>import React from &#39;react&#39;;</pre><pre>import Message from &#39;./messageElement&#39;;</pre><pre>export default function ReplyComment(props) {</pre><pre>const commentValue = props.commentValue;</pre><pre>console.log(commentValue);</pre><pre>return (</pre><pre>&lt;div style={{ marginLeft: &#39;20px&#39; }}&gt;</pre><pre>{Array.isArray(commentValue) &amp;&amp;</pre><pre>commentValue.map((ele, index) =&gt; (</pre><pre>&lt;div&gt;</pre><pre>&lt;Message</pre><pre>handleReplySubmit={props.handleReplySubmit}</pre><pre>element={ele}</pre><pre>commentValue={commentValue}</pre><pre>/&gt;</pre><pre>{ele.replies &amp;&amp; Array.isArray(ele.replies) &amp;&amp; (</pre><pre>&lt;div&gt;</pre><pre>&lt;ReplyComment</pre><pre>commentValue={ele.replies}</pre><pre>handleReplySubmit={props.handleReplySubmit}</pre><pre>/&gt;</pre><pre>&lt;/div&gt;</pre><pre>)}</pre><pre>&lt;/div&gt;</pre><pre>))}</pre><pre>&lt;/div&gt;</pre><pre>);</pre><pre>}</pre><p>Thank you!! Hope you liked the code of adding and comment and replies.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bf1bdd87ca3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Meditation and Software Development]]></title>
            <link>https://medium.com/@ayushipriya_40647/meditation-and-software-development-a087c323d95e?source=rss-da4bc77a971b------2</link>
            <guid isPermaLink="false">https://medium.com/p/a087c323d95e</guid>
            <dc:creator><![CDATA[Ayushi Priya]]></dc:creator>
            <pubDate>Mon, 19 Sep 2022 13:35:50 GMT</pubDate>
            <atom:updated>2022-09-19T14:10:15.732Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P_fviZ6djyyrcsXdNMkEMg.jpeg" /></figure><p>Meditation is an important part of life, let me correct an important part of leading a peaceful, successful and fulfilling life.</p><p>Software Developer’s life is all about learning and implementing various new technologies we cannot event count on our fingers and at the same time maintaining a balance between personal and professional life .</p><p>Work from home culture has kind of depleted the boundaries between personal life and professional life.</p><p>So let me tell you my coder friends it’s very easy to grab some moment of your life and just give your soul some time to relax and know yourself.</p><p>Every time some important feature , code and tech related information is going in our mind .</p><p>Sometime our mind needs an escape from all the external world and going deep dive into our soul and understand who we really are and what we are capable of.</p><p>Doing mediation has countless benefits , I personally feel like it has changed my life .</p><p>Meditation keeps us in clearing our mind and helps in increasing our concentration and in focusing our task in more efficient way we can imagine.</p><p>So please give it a try , focusing on yourself even for 10 minutes will have tremendous value on your career as well as personal life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a087c323d95e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CORS]]></title>
            <link>https://medium.com/@ayushipriya_40647/cors-e80fb8b71175?source=rss-da4bc77a971b------2</link>
            <guid isPermaLink="false">https://medium.com/p/e80fb8b71175</guid>
            <dc:creator><![CDATA[Ayushi Priya]]></dc:creator>
            <pubDate>Mon, 22 Jun 2020 12:21:47 GMT</pubDate>
            <atom:updated>2020-07-09T09:28:16.706Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>How to handle CORS errors?</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LXfvifkXeHBoUFrboBVncA.jpeg" /></figure><p>It was the first month of my internship at OYO that I encountered Cross Origin Resource Sharing(CORS). I was assigned a simple task of storing, updating and deleting “To-Do List Items”. Though the task looks simple on the outside but it was a real challenge for me as I had never worked on Cross Origin Resource Sharing and on the top of it the gigantic name itself created horrors in my mind. However, with a little guidance and too much exploration around the topic, I was able to successfully complete my task which was to call api’s from client side corresponding to GET, POST, PUT, DELETE request for getting, saving, updating , deleting the todo-list items. Initially when I was unable to perform any of the above operations ,I looked in console and saw that there is a CORS error, but later on I was able to perform all the above operations by resolving these errors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*dbAql_mXkPgoX_u1P71NXw.png" /><figcaption>CORS error in the console</figcaption></figure><h3><strong>What is CORS?</strong></h3><p>For those of you wondering what exactly CORS is, let us move forward in layman terms. When any site wants to make requests to origin that differs from the one it’s accessing then it is not allowed by web browsers . When we try to get restricted resources on a web page to be requested from another domain outside the domain from which the resources were served then we somewhat fail in our task. The prime reason for this issue has been identified as “security concerns”. The “security concerns” means that browsers restrict cross-origin HTTP requests initiated from scripts. However, the restrictions constraints our freedom to act (as a user) and this is where Cross Origin Resource Sharing <strong>(CORS)</strong> comes to play.CORS supports secure data transfer and cross origin requests between clients and servers.It works by adding new HTTP headers which allow servers to decide which origins are permitted to read information from them.</p><p>Let us consider an example to further illustrate its application. Whenever we travel to another country, we need a passport/visa as a proof of our identity, without which we won’t be allowed to travel.Similarly if we need to get some data from any api we need to pass some headers which will be the proof of identity.</p><h3><strong>The CORS has been further classified into the following types:-</strong></h3><p><strong>i. Simple Requests</strong>: A browser is said to have simple request if it meets the following conditions:-</p><p>1. GET, POST or HEAD method is used.</p><p>2. Except the headers set by the user agent and header only <a href="https://fetch.spec.whatwg.org/#cors-safelisted-request-header">CORS safe-listed header</a> are allowed.</p><p>3. The values used for Content-Type headers are</p><p>3.1 application/x-www-form-url-encoded</p><p>3.2 multipart/form-data</p><p>3.3 text/plain</p><p>4. No event listeners are registered on any XMLHttpRequestUpload object</p><p>5. No ReadableStream object is used in the request</p><p>When we make a request from web content <a href="https://abc.com/">https://abc.com</a> to invoke content from domain <a href="https://xyz.com/">https://xyz.com</a> and if the server sends header Access-Control-Allow-Origin:* in response then this will indicate that resource will be accessed by any domain but if it sends Access-Control-Allow-Origin:<a href="https://xyz.com/">https://xyz.com</a> then no origin other than <a href="https://xyz.com/">https://xyz.com</a> can access the resource in cross-site manner.</p><p><strong>ii. Preflighted Requests</strong>: If the request doesn’t meet the above criteria then it is said to be a preflighted request Options method. It is an OPTIONS request, using three HTTP request headers: Access-Control-Request-Method, Access-Control-Request-Headers and the Origin Header which is send to resource on other domain to determine if actual request is safe to send.</p><p>To further simplify this let us consider an example.</p><p>To solve the CORS issue we can seek help from Google Chrome Extension mod-headers.</p><p>When we want to allow a website, say https://<a href="http://www.abc.com/">abc.com</a> to access the information from a server of different origin then we have to add Access-Control-Allow-Origin: <a href="https://abc.com/">https://abc.</a>com. on the response headers.</p><p>Access-Control-Allow-Origin: * facilitates accessing the resource from <strong>any</strong> domain</p><p>The following are the new HTTP headers added by the CORS standard:</p><ol><li>Access-Control-Allow-Origin</li><li>Access-Control-Allow-Credentials</li><li>Access-Control-Allow-Headers</li><li>Access-Control-Allow-Methods</li><li>Access-Control-Expose-Headers</li><li>Access-Control-Max-Age</li><li>Access-Control-Request-Headers</li><li>Access-Control-Request-Method</li><li>Origin</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e80fb8b71175" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>