<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Eesa Zahed - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Eesa Zahed - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 24 May 2026 16:29:53 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/eesazahed/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Simple Bitcoin-to-USD Calculator ]]>
                </title>
                <description>
                    <![CDATA[ Welcome to this fun and hands-on project where we'll build a calculator that converts Bitcoin to USD. You'll learn about API fetching, DOM manipulation, and localStorage, and you'll use some basic math along the way.  By the end of this tutorial, you... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-a-bitcoin-to-usd-calculator/</link>
                <guid isPermaLink="false">66b8dd3147c23b7ae1ad0bb9</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Eesa Zahed ]]>
                </dc:creator>
                <pubDate>Mon, 22 Jul 2024 22:36:23 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/07/pexels-davidmcbee-730547.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Welcome to this fun and hands-on project where we'll build a calculator that converts Bitcoin to USD. You'll learn about API fetching, DOM manipulation, and localStorage, and you'll use some basic math along the way. </p>
<p>By the end of this tutorial, you'll have a functioning Bitcoin price calculator that fetches the current price of Bitcoin, allows you to calculate the value of your Bitcoin holdings, and saves your data locally. </p>
<p>This is a beginner-friendly project suited for those who understand the basics of HTML, CSS, and vanilla JavaScript.</p>
<p>This is what we'll create:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Screenshot-2024-07-17-at-21.39.30.png" alt="Image" width="600" height="400" loading="lazy">
<em>The finished project!</em></p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>Create a new folder and name it something relevant to the project, such as <em>btc-usd-calc</em>. Create a file titled <em>index.html</em> in your project folder.</p>
<p>Start by writing the HTML. The page should essentially contain </p>
<ul>
<li>A heading</li>
<li>The current Bitcoin price</li>
<li>A text field for a user to input their Bitcoin holdings</li>
<li>A button to calculate their Bitcoin holdings</li>
<li>The calculated amount</li>
</ul>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Bitcoin Calculator<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"shortcut icon"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"favicon.ico"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"image/x-icon"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Bitcoin Calculator<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"main"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"currentPrice"</span>&gt;</span>Latest price:<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">b</span>&gt;</span>$<span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"bitcoinPrice"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> USD<span class="hljs-tag">&lt;/<span class="hljs-name">b</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"bitcoinAmount"</span>&gt;</span>How much Bitcoin do you own?<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"number"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"bitcoinAmount"</span> <span class="hljs-attr">step</span>=<span class="hljs-string">"any"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"0.05"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"calculateBtn"</span>&gt;</span>Calculate<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"usdAmount"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"script.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Open it up in a browser and you should see this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Screenshot-2024-07-17-at-21.44.38.png" alt="Image" width="600" height="400" loading="lazy">
<em>Pretty basic at the moment - styling will be done later!</em></p>
<h2 id="heading-fetch-the-api">Fetch the API</h2>
<p>Now, in the same directory as <em>index.html</em>, create a new file titled <em>script.js</em>.</p>
<p>It's important to know what API to use. I highly recommend that you use <a target="_blank" href="https://api.coindesk.com/v1/bpi/currentprice.json">this one</a> while following along with this tutorial. The CoinDesk API is:</p>
<ul>
<li>Free</li>
<li>Fast</li>
<li>No API token needed</li>
<li>Minimal </li>
<li>Contains the necessary information for this project</li>
</ul>
<p>In <em>script.js</em>, define a constant for the API URL:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> API_URL = <span class="hljs-string">"https://api.coindesk.com/v1/bpi/currentprice.json"</span>;
</code></pre>
<p>The next thing to do is to add an event listener to call the API once the page content loads. You can do that by adding the following code directly underneath the API_URL constant:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">"DOMContentLoaded"</span>, <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-comment">// Run an asynchronous function once the DOM content has loaded</span>
  <span class="hljs-keyword">let</span> bitcoinPrice; <span class="hljs-comment">// Initialize the variable</span>

  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> fetch(API_URL);
    <span class="hljs-comment">// Await a response from the HTTP request sent to the API URL</span>

    <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> response.json();
    <span class="hljs-comment">// Await the json content of the response</span>

    bitcoinPrice = data.bpi.USD.rate_float.toFixed(<span class="hljs-number">2</span>);
    <span class="hljs-comment">// Reassign the bitcoinPrice variable to the USD value of Bitcoin, rounded to the nearest 2 decimal places.</span>
  } <span class="hljs-keyword">catch</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"error!"</span>); <span class="hljs-comment">// In case of error</span>
  }

  <span class="hljs-built_in">console</span>.log(bitcoinPrice); <span class="hljs-comment">// Log the price in the console</span>
});
</code></pre>
<p>In case of any errors, the program will let us know in the console. Otherwise, <code>bitcoinPrice</code> will be logged in the console. You can test it out now and see if you get the current Bitcoin price!</p>
<h2 id="heading-how-to-implement-localstorage">How to Implement localStorage</h2>
<p>Fundamentally, <code>localStorage</code> is a feature in most web browsers (see which versions support it <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage#browser_compatibility">here</a>) that saves information so that it is retained in the browser's memory even after the page or browser is closed.</p>
<p>Let's get started by editing some lines:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">"DOMContentLoaded"</span>, <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">let</span> bitcoinPrice = <span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">"lastBitcoinPrice"</span>);
  <span class="hljs-comment">// Retrieve the last stored Bitcoin price from localStorage, if it exists</span>
  <span class="hljs-comment">// Note: It should be null the first time you try running the page</span>

  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> fetch(API_URL);
    <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> response.json();
    bitcoinPrice = data.bpi.USD.rate_float.toFixed(<span class="hljs-number">2</span>);
    <span class="hljs-comment">// bitcoinPrice will be re-written</span>

    <span class="hljs-built_in">localStorage</span>.setItem(<span class="hljs-string">"lastBitcoinPrice"</span>, bitcoinPrice);
    <span class="hljs-comment">// Save most recent price to localStorage</span>
  } <span class="hljs-keyword">catch</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"error!"</span>);
  }

  <span class="hljs-built_in">console</span>.log(bitcoinPrice);
});
</code></pre>
<h2 id="heading-how-to-implement-dom-manipulation">How to Implement DOM Manipulation</h2>
<p>The Document Object Model (DOM) is an interface that allows programming interactions with web documents. Essentially, DOM manipulation with JavaScript allows us to update certain parts or the entirety of a document. </p>
<p>In this project, we'll use it to show the current Bitcoin price and calculated value. We'll also use it to retrieve the user-inputted value in the <code>#bitcoinAmount</code> text field when the calculate button is clicked. </p>
<p>Let's implement DOM manipulation:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">"DOMContentLoaded"</span>, <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">const</span> main = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"main"</span>);
  <span class="hljs-keyword">const</span> bitcoinPriceElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"bitcoinPrice"</span>);
  <span class="hljs-keyword">const</span> bitcoinAmountInput = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"bitcoinAmount"</span>);
  <span class="hljs-keyword">const</span> calculateBtn = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"calculateBtn"</span>);
  <span class="hljs-keyword">const</span> usdAmountElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"usdAmount"</span>);

  <span class="hljs-keyword">let</span> bitcoinPrice = <span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">"lastBitcoinPrice"</span>);

  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> fetch(API_URL);
    <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> response.json();
    bitcoinPrice = data.bpi.USD.rate_float.toFixed(<span class="hljs-number">2</span>);
    <span class="hljs-built_in">localStorage</span>.setItem(<span class="hljs-string">"lastBitcoinPrice"</span>, bitcoinPrice);

    bitcoinPriceElement.textContent = bitcoinPrice;
    <span class="hljs-comment">// Set the text content of bitcoinPriceElement to the current bitcoinPrice</span>
  } <span class="hljs-keyword">catch</span> {
    <span class="hljs-keyword">if</span> (bitcoinPrice) {
      <span class="hljs-comment">// If there's an existing bitcoinPrice in localStorage...</span>
      bitcoinPriceElement.textContent = bitcoinPrice;
      <span class="hljs-comment">// ...display whatever is saved localStorage</span>
    } <span class="hljs-keyword">else</span> {
      main.innerHTML = <span class="hljs-string">"&lt;p&gt;Could not fetch Bitcoin price :(&lt;/p&gt;"</span>;
      <span class="hljs-keyword">return</span>;
    }
  }

  <span class="hljs-built_in">console</span>.log(bitcoinPrice);
});
</code></pre>
<h2 id="heading-how-to-calculate-the-current-wallet-value">How to Calculate the Current Wallet Value</h2>
<p>Now, the point of a Bitcoin calculator is to calculate how much someone's Bitcoin wallet is valued, not necessarily how much the current price is. </p>
<p>For example, the current price of Bitcoin might be $60,000 USD. If you own 2 Bitcoins, your wallet is valued at $120,000 USD. If you own half (0.5) a Bitcoin, your wallet is valued at $30,000 USD.</p>
<p>Let's get the amount of Bitcoin that the user owns (<code>bitcoinAmount</code>) from localStorage.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// continue after console.log(bitcoinPrice);</span>

<span class="hljs-keyword">let</span> bitcoinAmount = <span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">"bitcoinAmount"</span>);
</code></pre>
<p>Calculate the amount in USD with this function:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> calculateUSDAmount = <span class="hljs-function">() =&gt;</span> {
  bitcoinAmount = bitcoinAmountInput.value || <span class="hljs-number">0</span>;
  <span class="hljs-comment">// bitcoinAmount will be reassigned to whatever is in the input on the front end, otherwise it's default value will be zero</span>

  <span class="hljs-keyword">const</span> usdAmount = bitcoinAmount * bitcoinPrice;
  <span class="hljs-comment">// Say you have 2 Bitcoins and the price is 60000.</span>
  <span class="hljs-comment">// 2 * 60000 = 120000</span>

  usdAmountElement.innerHTML = <span class="hljs-string">`&lt;b&gt;$<span class="hljs-subst">${usdAmount.toFixed(
    <span class="hljs-number">2</span>
  )}</span> USD&lt;/b&gt; worth of Bitcoin.`</span>;
  <span class="hljs-comment">// Round it to the nearest 2 decimals and display it</span>
};
</code></pre>
<p>Remember when we got <code>bitcoinAmount</code> from localStorage? Now, when the page loads, let's set the value of the input on the front-end to <code>bitcoinAmount</code>.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">if</span> (bitcoinAmount) {
    bitcoinAmountInput.value = bitcoinAmount;
    <span class="hljs-comment">// Set the input's value to bitcoinAmount</span>

    calculateUSDAmount();
    <span class="hljs-comment">// Calculate and update the front-end</span>
  }
</code></pre>
<p>For the user to update their <code>bitcoinAmount</code>, let's add an event listener to when the <code>calculateBtn</code> is clicked:</p>
<pre><code class="lang-javascript">  calculateBtn.addEventListener(<span class="hljs-string">"click"</span>, <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">localStorage</span>.setItem(<span class="hljs-string">"bitcoinAmount"</span>, bitcoinAmountInput.value);
    <span class="hljs-comment">// Save the input value to localStorage</span>

    calculateUSDAmount();
    <span class="hljs-comment">// Calculate and update the front-end</span>
  });
</code></pre>
<p>All of the JavaScript should now be complete.</p>
<h2 id="heading-full-javascript-code">Full JavaScript Code</h2>
<p>You entire code should be similar to this (aside from comments and formatting):</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> API_URL = <span class="hljs-string">"https://api.coindesk.com/v1/bpi/currentprice.json"</span>;

<span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">"DOMContentLoaded"</span>, <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">const</span> main = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"main"</span>);
  <span class="hljs-keyword">const</span> bitcoinPriceElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"bitcoinPrice"</span>);
  <span class="hljs-keyword">const</span> bitcoinAmountInput = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"bitcoinAmount"</span>);
  <span class="hljs-keyword">const</span> calculateBtn = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"calculateBtn"</span>);
  <span class="hljs-keyword">const</span> usdAmountElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"usdAmount"</span>);

  <span class="hljs-keyword">let</span> bitcoinPrice = <span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">"lastBitcoinPrice"</span>);

  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> fetch(API_URL);
    <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> response.json();
    bitcoinPrice = data.bpi.USD.rate_float.toFixed(<span class="hljs-number">2</span>);
    <span class="hljs-built_in">localStorage</span>.setItem(<span class="hljs-string">"lastBitcoinPrice"</span>, bitcoinPrice);

    bitcoinPriceElement.textContent = bitcoinPrice;
    <span class="hljs-comment">// Set the text content of bitcoinPriceElement to the current bitcoinPrice</span>
  } <span class="hljs-keyword">catch</span> {
    <span class="hljs-keyword">if</span> (bitcoinPrice) {
      <span class="hljs-comment">// If there's an existing bitcoinPrice in localStorage...</span>
      bitcoinPriceElement.textContent = bitcoinPrice;
      <span class="hljs-comment">// ...display whatever is saved localStorage</span>
    } <span class="hljs-keyword">else</span> {
      main.innerHTML = <span class="hljs-string">"&lt;p&gt;Could not fetch Bitcoin price :(&lt;/p&gt;"</span>;
      <span class="hljs-keyword">return</span>;
    }
  }

  <span class="hljs-built_in">console</span>.log(bitcoinPrice);

  <span class="hljs-keyword">let</span> bitcoinAmount = <span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">"bitcoinAmount"</span>);

  <span class="hljs-keyword">const</span> calculateUSDAmount = <span class="hljs-function">() =&gt;</span> {
    bitcoinAmount = bitcoinAmountInput.value || <span class="hljs-number">0</span>;
    <span class="hljs-comment">// bitcoinAmount will be reassigned to whatever is in the input on the front end, otherwise it's default value will be zero</span>

    <span class="hljs-keyword">const</span> usdAmount = bitcoinAmount * bitcoinPrice;
    <span class="hljs-comment">// Say you have 2 Bitcoins and the price is 60000.</span>
    <span class="hljs-comment">// 2 * 60000 = 120000</span>

    usdAmountElement.innerHTML = <span class="hljs-string">`&lt;b&gt;$<span class="hljs-subst">${usdAmount.toFixed(
      <span class="hljs-number">2</span>
    )}</span> USD&lt;/b&gt; worth of Bitcoin.`</span>;
    <span class="hljs-comment">// Round it to the nearest 2 decimals and display it</span>
  };

  <span class="hljs-keyword">if</span> (bitcoinAmount) {
    bitcoinAmountInput.value = bitcoinAmount;
    <span class="hljs-comment">// Set the input's value to bitcoinAmount</span>

    calculateUSDAmount();
    <span class="hljs-comment">// Calculate and update the front-end</span>
  }

  calculateBtn.addEventListener(<span class="hljs-string">"click"</span>, <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">localStorage</span>.setItem(<span class="hljs-string">"bitcoinAmount"</span>, bitcoinAmountInput.value);
    <span class="hljs-comment">// Save the input value to localStorage</span>

    calculateUSDAmount();
    <span class="hljs-comment">// Calculate and update the front-end</span>
  });
});
</code></pre>
<p>Test it out in the browser and play around with it!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Screenshot-2024-07-18-at-13.11.56.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-add-some-styling">How to Add Some Styling</h2>
<p>Right now, there isn't any CSS so it looks pretty basic. You're more than welcome to customize the styling to your own preferences.</p>
<p>If you'd like to style it how I did, then follow along by adding this line at the bottom of <code>&lt;head&gt;</code>:</p>
<pre><code class="lang-html">   <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"style.css"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
</code></pre>
<p>Then, create a new file titled <em>style.css</em> within the same directory as <em>index.html</em>.</p>
<p>Write the following code in <em>style.css</em>:</p>
<pre><code class="lang-css"><span class="hljs-keyword">@import</span> url(<span class="hljs-string">"https://fonts.googleapis.com/css2?family=Inter:wght@400;700&amp;display=swap"</span>);

<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"Inter"</span>, sans-serif;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#f5f5f5</span>;
}

<span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">50rem</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">5rem</span> auto;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#fff</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">2rem</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">1rem</span>;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">1rem</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.1</span>);
}

<span class="hljs-selector-id">#main</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
  <span class="hljs-attribute">margin</span>: auto;
}

<span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">text-align</span>: center;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">3rem</span>;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">2rem</span>;
  <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">linear-gradient</span>(to right, #<span class="hljs-number">4</span>b0bff, #<span class="hljs-number">68</span>b2ff);
  <span class="hljs-attribute">-webkit-background-clip</span>: text;
  <span class="hljs-attribute">background-clip</span>: text;
  <span class="hljs-attribute">color</span>: transparent;
}

<span class="hljs-selector-tag">h3</span> {
  <span class="hljs-attribute">font-weight</span>: normal;
}

<span class="hljs-selector-tag">button</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0.5rem</span> <span class="hljs-number">0.75rem</span>;
  <span class="hljs-attribute">border</span>: none;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#007bff</span>;
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#fff</span>;
  <span class="hljs-attribute">cursor</span>: pointer;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">0.5rem</span>;
  <span class="hljs-attribute">transition</span>: <span class="hljs-number">0.3s</span>;
  <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">0.25rem</span>;
}

<span class="hljs-selector-tag">button</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0056b3</span>;
}

<span class="hljs-selector-tag">label</span> {
  <span class="hljs-attribute">display</span>: block;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">1rem</span>;
}

<span class="hljs-selector-tag">input</span><span class="hljs-selector-attr">[type=<span class="hljs-string">"number"</span>]</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0.5rem</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1rem</span>;
  <span class="hljs-attribute">border</span>: none;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#e3e3e3</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">0.5rem</span>;
}

<span class="hljs-keyword">@media</span> (<span class="hljs-attribute">prefers-color-scheme:</span> dark) {
  <span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">color-scheme</span>: dark;
    <span class="hljs-attribute">color</span>: white;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#2b0057</span>;
  }

  <span class="hljs-selector-tag">h1</span> {
    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">linear-gradient</span>(to right, #<span class="hljs-number">4</span>facfe, #<span class="hljs-number">00</span>f2fe);
  }

  <span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#16022c</span>;
  }

  <span class="hljs-selector-tag">input</span><span class="hljs-selector-attr">[type=<span class="hljs-string">"number"</span>]</span> {
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#33194d</span>;
  }
}
</code></pre>
<p>You should see this if you're in dark mode:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Screenshot-2024-07-18-at-13.17.06.png" alt="Image" width="600" height="400" loading="lazy">
<em>Dark mode</em></p>
<p>And this if you're in light mode:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Screenshot-2024-07-18-at-13.16.52.png" alt="Image" width="600" height="400" loading="lazy">
<em>Light mode</em></p>
<p>And that's everything! </p>
<p>You can see it on GitHub here: <a target="_blank" href="https://github.com/eesazahed/bitcoin-calc/">https://github.com/eesazahed/bitcoin-calc/</a></p>
<p>Here's the live preview: <a target="_blank" href="https://eesazahed.github.io/bitcoin-calc/">https://eesazahed.github.io/bitcoin-calc/</a></p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>Feel free to check out my <a target="_blank" href="https://www.linkedin.com/in/eszhd/">LinkedIn</a> or <a target="_blank" href="https://github.com/eesazahed">GitHub</a>.</p>
<p>If you'd like to reach out, my email address is eszhd1 (at) gmail.com</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Write More Efficient JavaScript Conditions ]]>
                </title>
                <description>
                    <![CDATA[ When you're coding in JavaScript, conditions are important for developing a functional application.  Conditional statements are important because you use them as "validators" which can either return truth or false. You can then use them to trigger fu... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-write-more-efficient-javascript-conditions/</link>
                <guid isPermaLink="false">66b8dd3847c23b7ae1ad0bbd</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Eesa Zahed ]]>
                </dc:creator>
                <pubDate>Wed, 03 May 2023 17:44:11 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/image3-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When you're coding in JavaScript, conditions are important for developing a functional application. </p>
<p>Conditional statements are important because you use them as "validators" which can either return truth or false. You can then use them to trigger further actions within the program.</p>
<p>But with many long, messy <code>if</code> statements in the program, it can cause confusion for developers and heavily reduce readability. This is why it's important for developers to implement more efficient conditions.</p>
<h2 id="heading-conditional-statement-example">Conditional Statement Example</h2>
<p>Imagine you are creating a polling app. Each user has to choose their favorite color, but the only options are red, green, and blue.</p>
<p>Clearly there aren't many choices for users, but let's stick to a simple solution for now.</p>
<p>The function for voting may look something like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> vote = <span class="hljs-function">(<span class="hljs-params">color</span>) =&gt;</span> {
  <span class="hljs-comment">// code goes here</span>
}
</code></pre>
<p>But what if a user votes for a color that isn't a valid option? They might have used a dropdown you forgot to remove some colors from, or the user could have manipulated the client-side JavaScript.</p>
<p>Try to imagine if a user voted for the color orange. You would need to use some JavaScript logic to prevent this.</p>
<p>How would you do this? You could create a simple condition where a user can only choose red:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> vote = <span class="hljs-function">(<span class="hljs-params">color</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (color !== <span class="hljs-string">"red"</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"invalid color"</span>;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-string">"valid color"</span>;
}
</code></pre>
<p>This is great! Now, the first condition the function checks is: "Is the color NOT red? Well, that’s not allowed", and returns accordingly.</p>
<pre><code class="lang-js">vote(<span class="hljs-string">"orange"</span>); <span class="hljs-comment">// "invalid color"</span>
</code></pre>
<p>Now, how can you make it so that the color blue is also accepted? All you need to do is add another condition:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> vote = <span class="hljs-function">(<span class="hljs-params">color</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (color !== <span class="hljs-string">"red"</span> &amp;&amp; color !== <span class="hljs-string">"blue"</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"invalid color"</span>;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-string">"valid color"</span>;
}
</code></pre>
<p>This works, too. But what if you want to verify that the color is red, orange, yellow, green, blue, or purple? Then the code would look like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> vote = <span class="hljs-function">(<span class="hljs-params">color</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (color !== <span class="hljs-string">"red"</span> &amp;&amp; color !== <span class="hljs-string">"orange"</span>  &amp;&amp; color !== <span class="hljs-string">"yellow"</span>  &amp;&amp; color !== <span class="hljs-string">"green"</span>  &amp;&amp; color !== <span class="hljs-string">"blue"</span>  &amp;&amp; color !== <span class="hljs-string">"purple"</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"invalid color"</span>;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-string">"valid color"</span>;
}

vote(<span class="hljs-string">"cyan"</span>); <span class="hljs-comment">// "invalid color"</span>
</code></pre>
<p>As you can see, this definitely works. But the code is long, messy, and is less readable now. To implement this efficiently, you need to use an array.</p>
<h3 id="heading-how-to-do-this-using-an-array">How to Do this Using an Array</h3>
<p>First, define the array. A good variable name is <code>validColors</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> validColors = [<span class="hljs-string">"red"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"yellow"</span>, <span class="hljs-string">"green"</span>, <span class="hljs-string">"blue"</span>, <span class="hljs-string">"purple"</span>];
</code></pre>
<p>You can use the <code>array.includes()</code> method here. The syntax for this is:</p>
<pre><code class="lang-js">array.includes(item) <span class="hljs-comment">// returns a boolean</span>
</code></pre>
<p>To implement this in the code, the syntax looks like this:</p>
<pre><code class="lang-js">!validColors.includes(color)
</code></pre>
<p>The exclamation point (!) at the beginning is there because you want to know if the color is NOT in the <code>validColors</code> array.</p>
<p>The full code for this is:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> validColors = [<span class="hljs-string">"red"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"yellow"</span>, <span class="hljs-string">"green"</span>, <span class="hljs-string">"blue"</span>, <span class="hljs-string">"purple"</span>];

<span class="hljs-keyword">const</span> vote = <span class="hljs-function">(<span class="hljs-params">color</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (!validColors.includes(color)) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"invalid color"</span>;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-string">"valid color"</span>;
}
</code></pre>
<p>This code is far easier to read and to edit. And it will be much easier to add new colors to the <code>validColors</code> array.</p>
<p>And you can test the code to make sure it works:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> validColors = [<span class="hljs-string">"red"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"yellow"</span>, <span class="hljs-string">"green"</span>, <span class="hljs-string">"blue"</span>, <span class="hljs-string">"purple"</span>];

<span class="hljs-keyword">const</span> vote = <span class="hljs-function">(<span class="hljs-params">color</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (!validColors.includes(color)) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"invalid color"</span> ;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-string">"valid color"</span>;
}

<span class="hljs-built_in">console</span>.log(vote(<span class="hljs-string">"red"</span>)); <span class="hljs-comment">// "valid color"</span>
<span class="hljs-built_in">console</span>.log(vote(<span class="hljs-string">"orange"</span>)); <span class="hljs-comment">// "valid color"</span>
<span class="hljs-built_in">console</span>.log(vote(<span class="hljs-string">"yellow"</span>)); <span class="hljs-comment">// "valid color"</span>
<span class="hljs-built_in">console</span>.log(vote(<span class="hljs-string">"green"</span>)); <span class="hljs-comment">// "valid color"</span>
<span class="hljs-built_in">console</span>.log(vote(<span class="hljs-string">"blue"</span>)); <span class="hljs-comment">// "valid color"</span>
<span class="hljs-built_in">console</span>.log(vote(<span class="hljs-string">"purple"</span>)); <span class="hljs-comment">// "valid color"</span>

<span class="hljs-built_in">console</span>.log(vote(<span class="hljs-string">"cyan"</span>)); <span class="hljs-comment">// "invalid color"</span>
<span class="hljs-built_in">console</span>.log(vote(<span class="hljs-string">"black"</span>)); <span class="hljs-comment">// "invalid color"</span>
</code></pre>
<p>Now, this implementation uses much less code and is more readable. </p>
<h3 id="heading-why-arrays-are-more-efficient-than-switch-cases">Why Arrays are More Efficient Than Switch Cases</h3>
<p>This is much easier than using a switch case, which would look like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> validColors = [<span class="hljs-string">"red"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"yellow"</span>, <span class="hljs-string">"green"</span>, <span class="hljs-string">"blue"</span>, <span class="hljs-string">"purple"</span>];

<span class="hljs-keyword">const</span> vote = <span class="hljs-function">(<span class="hljs-params">color</span>) =&gt;</span> {
  <span class="hljs-keyword">switch</span> (color) {
    <span class="hljs-keyword">case</span> <span class="hljs-string">"red"</span>:
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"you voted red"</span>);
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-string">"orange"</span>:
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"you voted orange"</span>);
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-string">"yellow"</span>:
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"you voted yellow"</span>);
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-string">"green"</span>:
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"you voted green"</span>);
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-string">"blue"</span>:
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"you voted blue"</span>);
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-string">"purple"</span>:
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"you voted purple"</span>);
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">default</span>:
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"invalid color"</span>);
    }
}
</code></pre>
<p>As you can see, this uses a lot more code. Another issue would arise if you needed to add another color as a valid option. Instead of just updating an array, you would need to carefully edit a switch case, which has a higher potential to break something in the code.</p>
<h3 id="heading-the-issue-with-nested-conditional-statements">The Issue with Nested Conditional Statements</h3>
<p>For another example, let's try to add the user's name when voting. The name must be less than or equal to 15 characters.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> validColors = [<span class="hljs-string">"red"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"yellow"</span>, <span class="hljs-string">"green"</span>, <span class="hljs-string">"blue"</span>, <span class="hljs-string">"purple"</span>];

<span class="hljs-keyword">const</span> vote = <span class="hljs-function">(<span class="hljs-params">name, color</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (!validColors.includes(color)) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"invalid color"</span> ;
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">if</span> (name.length &gt; <span class="hljs-number">15</span>) {
      <span class="hljs-keyword">return</span> <span class="hljs-string">"name is too long"</span>;
    }
  }

  <span class="hljs-keyword">return</span> <span class="hljs-string">"valid name and color"</span>;
}

<span class="hljs-built_in">console</span>.log(vote(<span class="hljs-string">"bob"</span>, <span class="hljs-string">"red"</span>)); <span class="hljs-comment">// "valid name and color"</span>
</code></pre>
<p>The code works correctly, but is difficult to read, and can cause confusion if more code is added. You can clean this up by not nesting conditions.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> validColors = [<span class="hljs-string">"red"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"yellow"</span>, <span class="hljs-string">"green"</span>, <span class="hljs-string">"blue"</span>, <span class="hljs-string">"purple"</span>];

<span class="hljs-keyword">const</span> vote = <span class="hljs-function">(<span class="hljs-params">name, color</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (!validColors.includes(color)) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"invalid color"</span> ;
  }

  <span class="hljs-keyword">if</span> (name.length &gt; <span class="hljs-number">15</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"name is too long"</span>;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-string">"valid name and color"</span>;
}

<span class="hljs-built_in">console</span>.log(vote(<span class="hljs-string">"bob"</span>, <span class="hljs-string">"red"</span>)); <span class="hljs-comment">// "valid name and color"</span>
</code></pre>
<p>This works well, as the code first checks if the color is valid, and then it checks if the name is valid. The code is also much easier to read now.</p>
<p>It’s looking good! I hope this has helped you understand how to write more efficient conditions in JavaScript.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Feel free to check out my <a target="_blank" href="https://github.com/eesazahed">GitHub</a> and <a target="_blank" href="https://replit.com/@eesazahed">Replit</a> to view my projects.</p>
<p>If you'd like to reach out, my email address is eszhd1 (at) gmail.com</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use SQLite with Python ]]>
                </title>
                <description>
                    <![CDATA[ Databases are a crucial component in software development. After all, we need to collect data in a location where we can digitally access it for reading, writing, updating, and deleting. In this tutorial, you'll learn how to use SQLite with Python. L... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/sqlite-python-beginners-tutorial/</link>
                <guid isPermaLink="false">66b8dd3b19f41bae75f28f4a</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SQLite ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Eesa Zahed ]]>
                </dc:creator>
                <pubDate>Tue, 21 Feb 2023 21:41:41 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/218187404-b5da9bc5-a6aa-446f-a8d5-5805344d091e.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Databases are a crucial component in software development. After all, we need to collect data in a location where we can digitally access it for reading, writing, updating, and deleting.</p>
<p>In this tutorial, you'll learn how to use SQLite with Python. Learning SQLite is a great way to learn how databases operate and how to perform basic CRUD (create, read, update, delete) operations. </p>
<p>Many software developer positions involve working with databases, and if you ever consider creating a full-scale application (such as a social media app or an online game), you'll definitely need a database too. </p>
<p>This tutorial will explain many basic concepts and simple operations, so that you can understand how to work with databases better.</p>
<h2 id="heading-what-is-sqlite">What is SQLite?</h2>
<p>SQLite is an embedded SQL (Structured Query Language) database engine library that works with many languages. </p>
<p>According to the <a target="_blank" href="https://www.sqlite.org/arch.html">official website</a>, SQL text is compiled into bytecode, which is then run by a virtual machine. Therefore, it is extremely fast and can efficiently handle complex queries.</p>
<p>A SQLite database is stored as a disk file, similar to a CSV (comma-separated values) file. But SQLite has many advantages over using a CSV file:</p>
<ul>
<li>It is written using C. C is a statically-typed, compiled language which is much faster than most languages, including Python.</li>
<li>It’s lightweight, so it performs better and faster than reading from a CSV file.</li>
<li>It’s easy to set up</li>
<li>It can handle more complex queries.</li>
<li>It’s more useful to learn, in case you are ever tasked with using SQL or MySQL in the future.</li>
</ul>
<h2 id="heading-how-to-setup-sqlite">How to Setup SQLite</h2>
<p>Here is an example of using SQLite with Python. I’m using <a target="_blank" href="https://replit.com">Replit’s online IDE</a>, but you are welcome to follow along on any IDE you like. </p>
<p>First, I’ll create a Python project with a <code>main.py</code> file. I’ll be using CS50’s SQL library, which you can install by running <code>pip3 install cs50</code>.</p>
<p>The first step is to create a database.db file in the root directory, which you can do by entering the following command in the terminal:</p>
<pre><code>touch database.db
</code></pre><p>At this point, the following code should be added to main.py:</p>
<pre><code><span class="hljs-keyword">from</span> cs50 <span class="hljs-keyword">import</span> SQL

db = SQL(<span class="hljs-string">"sqlite:///database.db"</span>)
</code></pre><h3 id="heading-how-to-create-a-database-table">How to create a database table</h3>
<p>The next step is to create a table in the database. SQL stores data in tables, which are similar to tables found in Excel or Google Sheets. The code for this is:</p>
<pre><code>db.execute(<span class="hljs-string">"CREATE TABLE IF NOT EXISTS users (name TEXT, age NUMBER, fav_food STRING)"</span>)
</code></pre><p>To break this down, db is the database that the data is written to. Next, a command gets executed. If the table <code>users</code> doesn’t exist, create a table with the name users, with the column names <code>name</code>, <code>age</code>, and <code>fav_food</code>, with the data types for each value specified.</p>
<h3 id="heading-how-to-write-to-the-database">How to write to the database</h3>
<p>You can use the INSERT operation to add a user.</p>
<pre><code>db.execute(<span class="hljs-string">"INSERT INTO users (name, age, fav_food) VALUES(?, ?, ?)"</span>, <span class="hljs-string">"eesa"</span>, <span class="hljs-number">14</span>, <span class="hljs-string">"pizza"</span>)
</code></pre><p>The value “eesa” gets inserted into the name column, the value 14 is inserted into the age column, and the value “pizza” is inserted into the fav_food column.</p>
<p>The code for adding another user (in this case, Bob), would be this:</p>
<pre><code>db.execute(<span class="hljs-string">"INSERT INTO users (name, age, fav_food) VALUES(?, ?, ?)"</span>, <span class="hljs-string">"bob"</span>, <span class="hljs-number">20</span>, <span class="hljs-string">"burgers"</span>)
</code></pre><h3 id="heading-how-to-read-from-the-database">How to read from the database</h3>
<p>After this, we can attempt to read all the users from the database. You can do this by running the following code.</p>
<pre><code>people = db.execute(<span class="hljs-string">"SELECT * FROM users"</span>)
print(people) # [{<span class="hljs-string">'name'</span>: <span class="hljs-string">'eesa'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">14</span>, <span class="hljs-string">'fav_food'</span>: <span class="hljs-string">'pizza'</span>}]
</code></pre><p>The code above is fairly straightforward. The * in the SELECT statement selects everything that’s in the database.</p>
<p>To only select specific values, you can use the DISTINCT statement. Say for example, you only want the favorite food of each user. You can do this by running the following code:</p>
<pre><code>people = db.execute(<span class="hljs-string">"SELECT DISTINCT fav_food FROM users"</span>)
print(people)
</code></pre><p>You can also separate values using commas in a SELECT DISTINCT query:</p>
<pre><code>people = db.execute(<span class="hljs-string">"SELECT DISTINCT age, fav_food FROM users"</span>)
print(people)
</code></pre><p>What if we wanted to just read the data for Bob, and ignore everyone else? You can do this by using the SQL WHERE Clause:</p>
<pre><code>people = db.execute(<span class="hljs-string">"SELECT * FROM users WHERE name='bob'"</span>)
print(people)
</code></pre><p>How about for more complex queries? You can do this using the AND, OR and NOT syntax. You can separate WHERE clauses with these keywords for more complex queries.</p>
<pre><code>people = db.execute(<span class="hljs-string">"SELECT * FROM users WHERE name='bob' AND age=20"</span>)
print(people)
</code></pre><p>This will print out the data for Bob, because Bob is 20. </p>
<h3 id="heading-how-to-update-a-row-in-the-database">How to update a row in the database</h3>
<p>To update a row, you can use the UPDATE statement like this:</p>
<pre><code>db.execute(<span class="hljs-string">"UPDATE users SET fav_food='shawarma' WHERE name='eesa'"</span>)
</code></pre><h3 id="heading-how-to-delete-a-row-in-the-database">How to delete a row in the database</h3>
<p>To delete a row, use the DELETE Syntax (as you might’ve guessed). It looks like this:</p>
<pre><code>db.execute(<span class="hljs-string">"DELETE FROM users WHERE name='bob'"</span>) # goodbye bob

people = db.execute(<span class="hljs-string">"SELECT * FROM users"</span>)
print(people) # [{<span class="hljs-string">'name'</span>: <span class="hljs-string">'eesa'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">14</span>, <span class="hljs-string">'fav_food'</span>: <span class="hljs-string">'shawarma'</span>}]
</code></pre><p>To delete all the rows in the table, just remove the WHERE clause:</p>
<pre><code>db.execute(<span class="hljs-string">"DELETE FROM users"</span>) # :(

people = db.execute(<span class="hljs-string">"SELECT * FROM users"</span>)
print(people) # []
</code></pre><h2 id="heading-conclusion">Conclusion</h2>
<p>And that’s it for now. For more information on SQLite, I'd recommend checking out the <a target="_blank" href="https://docs.python.org/3/library/sqlite3.html">official documentation</a>. I wish you the best in creating amazing things!</p>
<p>Feel free to check out my <a target="_blank" href="https://github.com/eesazahed">GitHub</a> and <a target="_blank" href="https://replit.com/@eesazahed">Replit</a> to view my projects.</p>
<p>If you'd like to reach out, my email address is eszhd1 (at) gmail.com</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Get GitHub Pro and Replit Hacker for Free as a Student ]]>
                </title>
                <description>
                    <![CDATA[ If you are in high school or college/university and are interested in coding, you should consider signing up for GitHub Education.  This allows you to use GitHub Pro, as well as having many other benefits.  In this quick guide, I'll show you the step... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-get-github-pro-and-replit-hacker-for-free-as-a-student/</link>
                <guid isPermaLink="false">66b8dd3547c23b7ae1ad0bbb</guid>
                
                    <category>
                        <![CDATA[ GitHub ]]>
                    </category>
                
                    <category>
                        <![CDATA[ repl.it ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Students ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Eesa Zahed ]]>
                </dc:creator>
                <pubDate>Fri, 10 Feb 2023 17:47:17 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/coderr.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you are in high school or college/university and are interested in coding, you should consider signing up for GitHub Education. </p>
<p>This allows you to use GitHub Pro, as well as having many other benefits. </p>
<p>In this quick guide, I'll show you the steps to sign up for GitHub Education and I'll discuss its benefits. Then I'll show you how you can get free access to Replit Hacker for a year. </p>
<h2 id="heading-how-to-get-a-free-github-pro-account-as-a-student">How to Get a Free GitHub Pro Account as a Student</h2>
<p>If you do not already have a <a target="_blank" href="https://github.com/signup">GitHub</a> account, you will need to create one. Sign in with your personal email address (so you can use your account even after your school email address gets deactivated when you graduate).</p>
<p>Next, go to settings and click Emails. Here is where you will add your school email to your GitHub account.</p>
<p><img src="https://user-images.githubusercontent.com/97064249/215526808-7e2aeb8a-c32c-4f33-9ef7-ad0f6b88d287.png" alt="image1" width="255" height="405" loading="lazy">
<em>Add your school email in your GitHub account</em></p>
<p>Add your school email address and verify it. Once it is verified, go to <a target="_blank" href="https://education.github.com/">https://education.github.com/</a>. This will allow you to be eligible for GitHub Pro. Sign in with your GitHub account.</p>
<p>Then, head to Benefits. Scroll down and click <strong>Get student benefits</strong> (just make sure you are <a target="_blank" href="https://education.github.com/discount_requests/pack_application#student-info">eligible</a> first).</p>
<p><img src="https://user-images.githubusercontent.com/97064249/215526941-b1a1e2fc-bcd9-4edd-bf8e-61a4e1cd82ac.png" alt="image2" width="437" height="329" loading="lazy">
<em>Student benefits</em></p>
<p>Select <strong>Student</strong> as your academic status. Scroll down and use your school email address. After that, you have to select your school name as your school. Using a few sentences, explain why you are using GitHub.</p>
<p>Once you have completed those steps, press continue.</p>
<p>You’ll need to verify you’re a student at your school. To do this, you can upload a screenshot of an email from your school board or another official school document. It’ll take a few days to verify, so you’ll have to wait until then.</p>
<p>After that has been completed, you should see your dashboard looking something like this:</p>
<p><img src="https://user-images.githubusercontent.com/97064249/215528452-d7f4de46-77ac-4df2-8352-f06329fdeb5f.png" alt="image3" width="1287" height="734" loading="lazy"></p>
<p>You'll now have access to many student benefits, including a $200 Digital Ocean credit, a free domain from Namecheap, and a free subscription to the JetBrains IDE, which you can renew annually. </p>
<p>Next in this tutorial, you will be learning how to get the Replit Hacker plan for free (which is not mentioned as a student benefit offer).</p>
<h2 id="heading-how-to-get-a-free-replit-hacker-plan">How to Get a Free Replit Hacker Plan</h2>
<p>Once you have GitHub Pro, you’ll have access to many other benefits as well. One of the most notable benefits is a free Replit Hacker Plan for one year.</p>
<p>If you don't know what Replit is, it's a web-based IDE which allows users to create amazing projects (called Repls) that are stored on the cloud. </p>
<p>I have found Replit incredibly useful for coding in Rust, as it makes installing packages and setting up the dev environment really simple and easy. </p>
<p>You can also import GitHub repositories directly into Replit, so you can work on your code from any device, as long as you have an internet connection.</p>
<p>Replit is free to sign up, but having the <strong>Hacker plan</strong> gives you certain benefits that are extremely useful for development. </p>
<p>One notable benefit is having unlimited private Repls. Some more advantages are having 10x the account storage, and double the Repl storage. This plan starts at $7.00 a month, but luckily for students, there is a way to get the plan for free (<a target="_blank" href="https://replit.com/site/github-students">Source</a>).</p>
<p>If you don’t have a Replit account, create one with your personal email address (so you can use your account even after you school email address gets deactivated when you graduate).</p>
<p>Next, head to Account in the home menu.</p>
<p><img src="https://user-images.githubusercontent.com/97064249/215528245-87cf3cae-db1f-4382-b75a-599c269cc95e.png" alt="image4" width="184" height="106" loading="lazy"></p>
<p>Scroll down to Connected services and connect your GitHub account.</p>
<p><img src="https://user-images.githubusercontent.com/97064249/215528283-8895bb3e-fb70-4d7c-87b3-3d3852e8c6c7.png" alt="image5" width="355" height="161" loading="lazy"></p>
<p>You should now have GitHub Pro (until you graduate) and Replit Hacker (for one year).</p>
<p>Enjoy!</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Now with your upgraded accounts, I hope you enjoy creating amazing things! Feel free to check out my <a target="_blank" href="https://github.com/eesazahed">GitHub</a> and <a target="_blank" href="https://replit.com/@eesazahed">Replit</a> to view my projects.</p>
<p>If you'd like to reach out, my email address is eszhd1 (at) gmail.com</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
