<?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[ toothbrush - 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[ toothbrush - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 08 May 2026 16:50:30 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/toothbrush/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Clear Input Values of Dynamic Form in React ]]>
                </title>
                <description>
                    <![CDATA[ There's a lot to consider when working on a React application, especially when they involve forms. Even if you're able to create a submit button and update your app's state the way you want, clearing the forms can be difficult. Say your application h... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-clear-input-values-of-dynamic-form-in-react/</link>
                <guid isPermaLink="false">66c35098d73001a6c0054bde</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:19:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a92740569d1a4ca2665.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>There's a lot to consider when working on a React application, especially when they involve forms. Even if you're able to create a submit button and update your app's state the way you want, clearing the forms can be difficult.</p>
<p>Say your application has dynamic forms like this:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom"</span>;
<span class="hljs-keyword">import</span> Cart <span class="hljs-keyword">from</span> <span class="hljs-string">"./Cart"</span>;

<span class="hljs-keyword">import</span> <span class="hljs-string">"./styles.css"</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">Items</span>: [
        {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"item1"</span>,
          <span class="hljs-attr">description</span>: <span class="hljs-string">"item1"</span>,
          <span class="hljs-attr">group</span>: <span class="hljs-string">"groupA"</span>,
          <span class="hljs-attr">dtype</span>: <span class="hljs-string">"str"</span>
        },
        {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"item2"</span>,
          <span class="hljs-attr">description</span>: <span class="hljs-string">"item2"</span>,
          <span class="hljs-attr">group</span>: <span class="hljs-string">"groupA"</span>,
          <span class="hljs-attr">dtype</span>: <span class="hljs-string">"str"</span>
        },
        {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"item3"</span>,
          <span class="hljs-attr">description</span>: <span class="hljs-string">"item3"</span>,
          <span class="hljs-attr">group</span>: <span class="hljs-string">"groupB"</span>,
          <span class="hljs-attr">dtype</span>: <span class="hljs-string">"str"</span>
        },
        {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"item4"</span>,
          <span class="hljs-attr">description</span>: <span class="hljs-string">"item4"</span>,
          <span class="hljs-attr">group</span>: <span class="hljs-string">"groupB"</span>,
          <span class="hljs-attr">dtype</span>: <span class="hljs-string">"str"</span>
        }
      ],
      <span class="hljs-attr">itemvalues</span>: [{}]
    };
    <span class="hljs-built_in">this</span>.onChangeText = <span class="hljs-built_in">this</span>.onChangeText.bind(<span class="hljs-built_in">this</span>);
    <span class="hljs-built_in">this</span>.handleReset = <span class="hljs-built_in">this</span>.handleReset.bind(<span class="hljs-built_in">this</span>);
    <span class="hljs-built_in">this</span>.handleSubmit = <span class="hljs-built_in">this</span>.handleSubmit.bind(<span class="hljs-built_in">this</span>);
    <span class="hljs-built_in">this</span>.findFieldIndex = <span class="hljs-built_in">this</span>.findFieldIndex.bind(<span class="hljs-built_in">this</span>);
    <span class="hljs-built_in">this</span>.trimText = <span class="hljs-built_in">this</span>.trimText.bind(<span class="hljs-built_in">this</span>);
  }

  onChangeText = <span class="hljs-function"><span class="hljs-params">e</span> =&gt;</span> {
    <span class="hljs-keyword">const</span> valuesCopy = [...this.state.itemvalues];
    <span class="hljs-comment">//debugger;</span>

    <span class="hljs-comment">// get data-group value</span>
    <span class="hljs-keyword">const</span> itemvalue = e.target.dataset.group;

    <span class="hljs-keyword">if</span> (!valuesCopy[<span class="hljs-number">0</span>][itemvalue]) {
      valuesCopy[<span class="hljs-number">0</span>][itemvalue] = [];
    }

    <span class="hljs-keyword">const</span> itemvalues = valuesCopy[<span class="hljs-number">0</span>][itemvalue];
    <span class="hljs-keyword">const</span> index = <span class="hljs-built_in">this</span>.findFieldIndex(itemvalues, e.target.name);

    <span class="hljs-keyword">if</span> (index &lt; <span class="hljs-number">0</span>) {
      valuesCopy[<span class="hljs-number">0</span>][itemvalue] = [
        ...itemvalues,
        { [e.target.name]: e.target.value.split(<span class="hljs-string">","</span>).map(<span class="hljs-built_in">this</span>.trimText) }
      ];
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-comment">// update the value</span>
      valuesCopy[<span class="hljs-number">0</span>][itemvalue][index][e.target.name] = e.target.value
        .split(<span class="hljs-string">","</span>)
        .map(<span class="hljs-built_in">this</span>.trimText);
    }

    <span class="hljs-comment">// console.log(itemsCopy);</span>

    <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">itemvalues</span>: valuesCopy });
  };
  findFieldIndex = <span class="hljs-function">(<span class="hljs-params">array, name</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> array.findIndex(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> item[name] !== <span class="hljs-literal">undefined</span>);
  };
  trimText(str) {
    <span class="hljs-keyword">return</span> str.trim();
  }

  handleReset = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">this</span>.setState({
      <span class="hljs-attr">itemvalues</span>: [{}]
    });
  };

  handleSubmit = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.state.itemvalues);
  };

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Cart</span>
        <span class="hljs-attr">Items</span>=<span class="hljs-string">{this.state.Items}</span>
        <span class="hljs-attr">handleSubmit</span>=<span class="hljs-string">{this.handleSubmit}</span>
        <span class="hljs-attr">handleReset</span>=<span class="hljs-string">{this.handleReset}</span>
        <span class="hljs-attr">onChangeText</span>=<span class="hljs-string">{this.onChangeText}</span>
      /&gt;</span></span>
    );
  }
}

ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>));
</code></pre>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> Form <span class="hljs-keyword">from</span> <span class="hljs-string">"./Form"</span>;

<span class="hljs-keyword">const</span> Cart = <span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Form</span> <span class="hljs-attr">Items</span>=<span class="hljs-string">{props.Items}</span> <span class="hljs-attr">onChangeText</span>=<span class="hljs-string">{props.onChangeText}</span> /&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{props.handleSubmit}</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{props.handleReset}</span>&gt;</span>Reset<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Cart;
</code></pre>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">const</span> Form = <span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {props.Items.map((item, index) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
          <span class="hljs-attr">name</span>=<span class="hljs-string">{item.name}</span>
          <span class="hljs-attr">placeholder</span>=<span class="hljs-string">{item.description}</span>
          <span class="hljs-attr">data-type</span>=<span class="hljs-string">{item.dtype}</span>
          <span class="hljs-attr">data-group</span>=<span class="hljs-string">{item.group}</span>
          <span class="hljs-attr">onChange</span>=<span class="hljs-string">{e</span> =&gt;</span> props.onChangeText(e)}
          key={index}
        /&gt;
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Form;
</code></pre>
<p>And simple input boxes are rendered to the page:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/image-56.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>When a user enters text into one of the input boxes, it's saved to the application state in groups like this:</p>
<pre><code>Itemvalues:
  <span class="hljs-number">0</span>:
    groupA: 
            item1: itemvalue1
            <span class="hljs-attr">item2</span>: itemvalue2
    <span class="hljs-attr">groupB</span>: 
            item3: itemvalue3
            <span class="hljs-attr">item4</span>: itemvalue4
</code></pre><p>It's pretty complicated, but you managed to get that working.</p>
<p>In <code>handleReset</code>, you're able to set <code>itemvalues</code> back to a null state when the "Reset" button is pressed:</p>
<pre><code class="lang-js">handleReset = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">this</span>.setState({
    <span class="hljs-attr">itemvalues</span>: [{}]
  });
};
</code></pre>
<p>But the problem is that the text is not cleared from all of the input boxes:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/Peek-2020-06-16-21-32.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You've already handled storing the actual text in the state, so here's a simple way to clear the text from all input boxes.</p>
<h2 id="heading-how-to-clear-the-values-all-inputs">How to clear the values all inputs</h2>
<p>At the top of <code>handleReset</code>, use <code>document.querySelectorAll('input')</code> to select all the input elements on the page:</p>
<pre><code class="lang-js">handleReset = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">document</span>.querySelectorAll(<span class="hljs-string">'input'</span>);
  <span class="hljs-built_in">this</span>.setState({
    <span class="hljs-attr">itemvalues</span>: [{}]
  });
};
</code></pre>
<p><code>document.querySelectorAll('input')</code> returns a <code>NodeList</code>, which is a bit different than an array, so you can't use any useful array methods on it.</p>
<p>To turn it into an array, pass <code>document.querySelectorAll('input')</code> to <code>Array.from()</code>:</p>
<pre><code class="lang-js">handleReset = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">Array</span>.from(<span class="hljs-built_in">document</span>.querySelectorAll(<span class="hljs-string">'input'</span>));
  <span class="hljs-built_in">this</span>.setState({
    <span class="hljs-attr">itemvalues</span>: [{}]
  });
};
</code></pre>
<p>Now all you have to do is iterate through each of the inputs and set its <code>value</code> attribute to an empty string. The <code>forEach</code> method is a good candidate for this:</p>
<pre><code class="lang-js">handleReset = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">Array</span>.from(<span class="hljs-built_in">document</span>.querySelectorAll(<span class="hljs-string">"input"</span>)).forEach(
    <span class="hljs-function"><span class="hljs-params">input</span> =&gt;</span> (input.value = <span class="hljs-string">""</span>)
  );
  <span class="hljs-built_in">this</span>.setState({
    <span class="hljs-attr">itemvalues</span>: [{}]
  });
};
</code></pre>
<p>Now when a user presses the "Reset" button, the value of every input is cleared, too:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/Peek-2020-06-16-21-42.gif" alt="Image" width="600" height="400" loading="lazy"></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Nesting For Loops in JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ If you're having trouble understanding freeCodeCamp's Nesting For Loops challenge, don't worry. We got your back. In this problem you have to complete the multiplyAll() function, and takes a multi-dimensional array as an argument. Remember that a mul... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/nesting-for-loops-in-javascript/</link>
                <guid isPermaLink="false">66c35be4f83dfae169b2c046</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Loops ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Tutorial ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:19:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a97740569d1a4ca2681.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you're having trouble understanding freeCodeCamp's <a target="_blank" href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops">Nesting For Loops</a> challenge, don't worry. We got your back.</p>
<p>In this problem you have to complete the <code>multiplyAll()</code> function, and takes a multi-dimensional array as an argument. Remember that a multi-dimensional array, sometimes called a 2D array, is just an array of arrays, for example, <code>[[1,2], [3,4], [5,6]]</code>.</p>
<p>In the editor on the right, <code>multiplyAll()</code> is defined as follows:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">var</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>

  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre>
<p>You need to complete the function so it multiplies the <code>product</code> variable by each number in the sub-arrays of the parameter <code>arr</code>, which is a multi-dimensional array.</p>
<p>There are a lot of different ways to solve this problem, but we'll focus on the simplest method using <code>for</code> loops.</p>
<h2 id="heading-set-up-your-for-loops">Set up your <code>for</code> loops</h2>
<p>Because <code>arr</code> is a multi-dimensional array, you'll need two <code>for</code> loops: one to loop through each of the sub-arrays arrays, and another to loop through the elements in each sub-array.</p>
<h3 id="heading-loop-through-the-inner-arrays">Loop through the inner arrays</h3>
<p>To do this, set up a <code>for</code> loop like you've done in previous challenges:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">let</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {

  }
  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre>
<p>Note that we're using <code>let</code> instead of <code>var</code> for the loop and to declare <code>product</code>. In this challenge you won't notice a difference between the two, but generally it's good practice to use ES6's <code>const</code> and <code>let</code> whenever you can. You can read more about why <a target="_blank" href="https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/">in this article</a>.</p>
<p>Now log each of the sub-arrays to the console:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">let</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
    <span class="hljs-built_in">console</span>.log(arr[i]);
  }
  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre>
<p>Because you're calling <code>multiplyAll()</code> with <code>[[1,2],[3,4],[5,6,7]]</code> at the bottom, you should see the following:</p>
<pre><code>[ <span class="hljs-number">1</span>, <span class="hljs-number">2</span> ]
[ <span class="hljs-number">3</span>, <span class="hljs-number">4</span> ]
[ <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span> ]
</code></pre><h3 id="heading-loop-through-the-elements-in-each-sub-array">Loop through the elements in each sub-array</h3>
<p>Now you need to loop through each number in the sub-arrays you just logged to the console.</p>
<p>Remove <code>console.log(arr[i]);</code> and create another <code>for</code> loop inside of the one you just wrote:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">let</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">0</span>; j &lt; arr[i].length; j++) {

    }
  }
  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre>
<p>Remember that, for the inner loop, we need to check the <code>.length</code> of <code>arr[i]</code> since <code>arr[i]</code> is one of the sub-arrays we looked at earlier.</p>
<p>Now log <code>arr[i][j]</code> to the console to see each of the individual elements:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">let</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">0</span>; j &lt; arr[i].length; j++) {
      <span class="hljs-built_in">console</span>.log(arr[i][j]);
    }
  }
  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre>
<pre><code><span class="hljs-number">1</span>
<span class="hljs-number">2</span>
<span class="hljs-number">3</span>
<span class="hljs-number">4</span>
<span class="hljs-number">5</span>
<span class="hljs-number">6</span>
<span class="hljs-number">7</span>
</code></pre><p>Finally, multiply <code>product</code> by every element in each of the sub-arrays:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">let</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">0</span>; j &lt; arr[i].length; j++) {
      product *= arr[i][j];
    }
  }
  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre>
<p>If you log <code>product</code> to the console, you'll see the correct answer for each test case:</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">let</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">0</span>; j &lt; arr[i].length; j++) {
      product *= arr[i][j];
    }
  }
  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-built_in">console</span>.log(product);
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre><pre><code><span class="hljs-number">6</span>  <span class="hljs-comment">// [[1], [2], [3]]</span>
<span class="hljs-number">5040</span>  <span class="hljs-comment">// [[1, 2], [3, 4], [5, 6, 7]]</span>
<span class="hljs-number">54</span>  <span class="hljs-comment">// [[5, 1], [0.2, 4, 0.5], [3, 9]]</span>
</code></pre><h2 id="heading-a-closer-look">A closer look</h2>
<p>If you're still not sure why the code above works, don't worry – you're not alone. Working with nested loops is complicated, and even experienced developers can get confused.</p>
<p>In cases like this, it can be helpful to log something more detailed to the console. Go back to your code and log <code>Sub-array ${i}: ${arr[i]}</code> to the console just before the inner <code>for</code> loop:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">let</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Sub-array <span class="hljs-subst">${i}</span>: <span class="hljs-subst">${arr[i]}</span>`</span>);
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">0</span>; j &lt; arr[i].length; j++) {
      product *= arr[i][j];
    }
  }
  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre>
<p>In the outer <code>for</code> loop, each iteration goes through the sub-arrays in <code>arr</code>. You should see this in the console:</p>
<pre><code>Sub-array <span class="hljs-number">0</span>: <span class="hljs-number">1</span>,<span class="hljs-number">2</span>
Sub-array <span class="hljs-number">1</span>: <span class="hljs-number">3</span>,<span class="hljs-number">4</span>
Sub-array <span class="hljs-number">2</span>: <span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>
</code></pre><p>Note that we're using <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">template literals</a> above. <code>Sub-array ${i}: ${arr[i]}</code> is the same as <code>'Sub-array ' + i + ': ' + arr[i]</code>, just much easier to write.</p>
<p>Now in the inner <code>for</code> loop, log <code>Element ${j}: ${arr[i][j]}</code> to the console:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">let</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Sub-array <span class="hljs-subst">${i}</span>: <span class="hljs-subst">${arr[i]}</span>`</span>);
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">0</span>; j &lt; arr[i].length; j++) {
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Element <span class="hljs-subst">${j}</span>: <span class="hljs-subst">${arr[i][j]}</span>`</span>);
      product *= arr[i][j];
    }
  }
  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre>
<p>The inner <code>for</code> loop goes through each element in each sub-array (<code>arr[i]</code>), so you should see this in the console:</p>
<pre><code>Sub-array <span class="hljs-number">0</span>: <span class="hljs-number">1</span>,<span class="hljs-number">2</span>
Element <span class="hljs-number">0</span>: <span class="hljs-number">1</span>
Element <span class="hljs-number">1</span>: <span class="hljs-number">2</span>
Sub-array <span class="hljs-number">1</span>: <span class="hljs-number">3</span>,<span class="hljs-number">4</span>
Element <span class="hljs-number">0</span>: <span class="hljs-number">3</span>
Element <span class="hljs-number">1</span>: <span class="hljs-number">4</span>
Sub-array <span class="hljs-number">2</span>: <span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>
Element <span class="hljs-number">0</span>: <span class="hljs-number">5</span>
Element <span class="hljs-number">1</span>: <span class="hljs-number">6</span>
Element <span class="hljs-number">2</span>: <span class="hljs-number">7</span>
</code></pre><p>The first iteration of <code>i</code> grabs the first sub-array, <code>[1, 2]</code>. Then the first iteration of <code>j</code> goes through each element in that sub-array:</p>
<pre><code><span class="hljs-comment">// i is 0</span>
arr[<span class="hljs-number">0</span>] <span class="hljs-comment">// [1, 2];</span>

<span class="hljs-comment">// j is 0</span>
arr[<span class="hljs-number">0</span>][<span class="hljs-number">0</span>] <span class="hljs-comment">// 1</span>
<span class="hljs-comment">// j is 1</span>
arr[<span class="hljs-number">0</span>][<span class="hljs-number">1</span>] <span class="hljs-comment">// 2</span>

-----

<span class="hljs-comment">// i is 1</span>
arr[<span class="hljs-number">1</span>] <span class="hljs-comment">// [3, 4]</span>

<span class="hljs-comment">// j is 0</span>
arr[<span class="hljs-number">1</span>][<span class="hljs-number">0</span>] <span class="hljs-comment">// 3</span>
<span class="hljs-comment">// j is 1</span>
arr[<span class="hljs-number">1</span>][<span class="hljs-number">1</span>] <span class="hljs-comment">// 4</span>

...
</code></pre><p>This example is pretty simple, but <code>arr[i][j]</code> can still be difficult to understand without logging multiple things to the console.</p>
<p>One quick improvement we can make is declaring a <code>subArray</code> variable in the outer <code>for</code> loop and setting it equal to <code>arr[i]</code>:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">let</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
    <span class="hljs-keyword">const</span> subArray = arr[i];
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">0</span>; j &lt; arr[i].length; j++) {
      product *= arr[i][j];
    }
  }
  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre>
<p>Then just make a few tweaks to the code to use the new <code>subArray</code> variable instead of <code>arr[i]</code>:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyAll</span>(<span class="hljs-params">arr</span>) </span>{
  <span class="hljs-keyword">let</span> product = <span class="hljs-number">1</span>;
  <span class="hljs-comment">// Only change code below this line</span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
    <span class="hljs-keyword">const</span> subArray = arr[i];
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">0</span>; j &lt; subArray.length; j++) {
      product *= subArray[j];
    }
  }
  <span class="hljs-comment">// Only change code above this line</span>
  <span class="hljs-keyword">return</span> product;
}

multiplyAll([[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>],[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>]]);
</code></pre>
<p>That should be everything you need to know about multi-dimensional arrays and nested <code>for</code> loops. Now get out there and iterate with the best of 'em!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ React - Change Inline CSS Conditionally Based on Component State ]]>
                </title>
                <description>
                    <![CDATA[ If you're having trouble with freeCodeCamp's Change Inline CSS Conditionally Based on Component State challenge, you're probably not alone. In this challenge, you need to add code to change some inline CSS conditionally based on the state of a React ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-change-inline-css-conditionally-based-on-component-state/</link>
                <guid isPermaLink="false">66c35d5a39357f9446976607</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:19:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a95740569d1a4ca267a.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you're having trouble with freeCodeCamp's <a target="_blank" href="https://www.freecodecamp.org/learn/front-end-libraries/react/change-inline-css-conditionally-based-on-component-state">Change Inline CSS Conditionally Based on Component State</a> challenge, you're probably not alone.</p>
<p>In this challenge, you need to add code to change some inline CSS conditionally based on the state of a React component.</p>
<p>When you first go to the challenge, here's the code you'll see:</p>
<pre><code class="lang-jsx"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GateKeeper</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">input</span>: <span class="hljs-string">''</span>
    };
    <span class="hljs-built_in">this</span>.handleChange = <span class="hljs-built_in">this</span>.handleChange.bind(<span class="hljs-built_in">this</span>);
  }
  handleChange(event) {
    <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">input</span>: event.target.value })
  }
  render() {
    <span class="hljs-keyword">let</span> inputStyle = {
      <span class="hljs-attr">border</span>: <span class="hljs-string">'1px solid black'</span>
    };
    <span class="hljs-comment">// change code below this line</span>

    <span class="hljs-comment">// change code above this line</span>
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Don't Type Too Much:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
          <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>
          <span class="hljs-attr">style</span>=<span class="hljs-string">{inputStyle}</span>
          <span class="hljs-attr">value</span>=<span class="hljs-string">{this.state.input}</span>
          <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this.handleChange}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
};
</code></pre>
<p>Notice that an inline style object, <code>inputStyle</code>, has already been declared with some default styling.</p>
<p>Your goal in this challenge is to update <code>inputStyle</code> so the border of the input is <code>3px solid red</code> when there are more than 15 characters in the input. Note that the text in the input box is saved in the component's state as <code>input</code>:</p>
<pre><code class="lang-jsx">...
this.state = {
  <span class="hljs-attr">input</span>: <span class="hljs-string">''</span>
};
...
</code></pre>
<h2 id="heading-close-but-not-quite">Close, but not quite</h2>
<p>Imagine that, after reading the description and instructions, you come up with this:</p>
<pre><code class="lang-jsx"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GateKeeper</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">input</span>: <span class="hljs-string">''</span>
    };
    <span class="hljs-built_in">this</span>.handleChange = <span class="hljs-built_in">this</span>.handleChange.bind(<span class="hljs-built_in">this</span>);
  }
  handleChange(event) {
    <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">input</span>: event.target.value })
  }
  render() {
    <span class="hljs-keyword">let</span> inputStyle = {
      <span class="hljs-attr">border</span>: <span class="hljs-string">'1px solid black'</span>
    };
    <span class="hljs-comment">// change code below this line</span>
    <span class="hljs-keyword">const</span> char = <span class="hljs-number">15</span>;
    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.state.input &gt; char) {
      inputStyle = {
        <span class="hljs-attr">border</span>:<span class="hljs-string">'3px solid red'</span>
      }
    }  
    <span class="hljs-comment">// change code above this line</span>
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Don't Type Too Much:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
          <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>
          <span class="hljs-attr">style</span>=<span class="hljs-string">{inputStyle}</span>
          <span class="hljs-attr">value</span>=<span class="hljs-string">{this.state.input}</span>
          <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this.handleChange}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
};
</code></pre>
<p>But when you try to submit this, it doesn't pass all the tests. Let's take a closer look at what's going on.</p>
<h2 id="heading-solutions">Solutions</h2>
<h3 id="heading-using-an-if-statement">Using an <code>if</code> statement</h3>
<p>Declaring <code>char</code> is fine, but take a closer look at the <code>if</code> condition:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.state.input &gt; char) {
  inputStyle = {
    <span class="hljs-attr">border</span>:<span class="hljs-string">'3px solid red'</span>
  }
}
</code></pre>
<p>Remember that <code>this.state.input</code> is the value of the input box and is a string. For example, it could be "testing testing 1, 2, 3".</p>
<p>If you enter "testing testing 1, 2, 3" into the text box and lot <code>this.state.input</code> to the console:</p>
<pre><code class="lang-jsx"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GateKeeper</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">input</span>: <span class="hljs-string">''</span>
    };
    <span class="hljs-built_in">this</span>.handleChange = <span class="hljs-built_in">this</span>.handleChange.bind(<span class="hljs-built_in">this</span>);
  }
  handleChange(event) {
    <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">input</span>: event.target.value })
  }
  render() {
    <span class="hljs-keyword">let</span> inputStyle = {
      <span class="hljs-attr">border</span>: <span class="hljs-string">'1px solid black'</span>
    };
    <span class="hljs-comment">// change code below this line</span>
    <span class="hljs-keyword">const</span> char = <span class="hljs-number">15</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.state.input);
    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.state.input &gt; char) {
      inputStyle = {
        <span class="hljs-attr">border</span>:<span class="hljs-string">'3px solid red'</span>
      }
    }  
    <span class="hljs-comment">// change code above this line</span>
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Don't Type Too Much:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
          <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>
          <span class="hljs-attr">style</span>=<span class="hljs-string">{inputStyle}</span>
          <span class="hljs-attr">value</span>=<span class="hljs-string">{this.state.input}</span>
          <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this.handleChange}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
};
</code></pre>
<p>You'll see <code>testing testing 1, 2, 3</code> in the console.</p>
<p>Further, if you log <code>this.state.input &gt; char</code> to the console, you'll see that it evaluates to <code>false</code>:</p>
<pre><code class="lang-jsx"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GateKeeper</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">input</span>: <span class="hljs-string">''</span>
    };
    <span class="hljs-built_in">this</span>.handleChange = <span class="hljs-built_in">this</span>.handleChange.bind(<span class="hljs-built_in">this</span>);
  }
  handleChange(event) {
    <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">input</span>: event.target.value })
  }
  render() {
    <span class="hljs-keyword">let</span> inputStyle = {
      <span class="hljs-attr">border</span>: <span class="hljs-string">'1px solid black'</span>
    };
    <span class="hljs-comment">// change code below this line</span>
    <span class="hljs-keyword">const</span> char = <span class="hljs-number">15</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.state.input &gt; char);
    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.state.input &gt; char) {
      inputStyle = {
        <span class="hljs-attr">border</span>:<span class="hljs-string">'3px solid red'</span>
      }
    }  
    <span class="hljs-comment">// change code above this line</span>
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Don't Type Too Much:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
          <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>
          <span class="hljs-attr">style</span>=<span class="hljs-string">{inputStyle}</span>
          <span class="hljs-attr">value</span>=<span class="hljs-string">{this.state.input}</span>
          <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this.handleChange}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
};
</code></pre>
<p>Simply put, you can't compare a string (<code>this.state.input</code>) directly to <code>char</code>, which is a number.</p>
<p>Instead, call the <code>.length</code> on <code>this.state.input</code> to get the length of the string and compare that to <code>count</code>:</p>
<pre><code class="lang-jsx"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GateKeeper</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">input</span>: <span class="hljs-string">''</span>
    };
    <span class="hljs-built_in">this</span>.handleChange = <span class="hljs-built_in">this</span>.handleChange.bind(<span class="hljs-built_in">this</span>);
  }
  handleChange(event) {
    <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">input</span>: event.target.value })
  }
  render() {
    <span class="hljs-keyword">let</span> inputStyle = {
      <span class="hljs-attr">border</span>: <span class="hljs-string">'1px solid black'</span>
    };
    <span class="hljs-comment">// change code below this line</span>
    <span class="hljs-keyword">const</span> char = <span class="hljs-number">15</span>;
    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.state.input.length &gt; char) {
      inputStyle = {
        <span class="hljs-attr">border</span>:<span class="hljs-string">'3px solid red'</span>
      }
    }  
    <span class="hljs-comment">// change code above this line</span>
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Don't Type Too Much:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
          <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>
          <span class="hljs-attr">style</span>=<span class="hljs-string">{inputStyle}</span>
          <span class="hljs-attr">value</span>=<span class="hljs-string">{this.state.input}</span>
          <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this.handleChange}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
};
</code></pre>
<p>Since the length of the string "testing testing 1, 2, 3" is 23 characters long (including spaces and commas), the border of the input box will turn red:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/image-53.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-using-a-ternary-operator">Using a ternary operator</h3>
<p>A <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator">ternary or conditional operator</a> is like a one line <code>if...else</code> statement, and can help shorten your code significantly.</p>
<p>Go back to your solution and remove everything except the <code>char</code> variable:</p>
<pre><code class="lang-jsx"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GateKeeper</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">input</span>: <span class="hljs-string">''</span>
    };
    <span class="hljs-built_in">this</span>.handleChange = <span class="hljs-built_in">this</span>.handleChange.bind(<span class="hljs-built_in">this</span>);
  }
  handleChange(event) {
    <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">input</span>: event.target.value })
  }
  render() {
    <span class="hljs-keyword">let</span> inputStyle = {
      <span class="hljs-attr">border</span>: <span class="hljs-string">'1px solid black'</span>
    };
    <span class="hljs-comment">// change code below this line</span>

    <span class="hljs-comment">// change code above this line</span>
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Don't Type Too Much:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
          <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>
          <span class="hljs-attr">style</span>=<span class="hljs-string">{inputStyle}</span>
          <span class="hljs-attr">value</span>=<span class="hljs-string">{this.state.input}</span>
          <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this.handleChange}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
};
</code></pre>
<p>Now take the condition you used in your earlier <code>if</code> statement and use it as the first part of the ternary condition: <code>this.state.input.length &gt; char ?  :  ;</code></p>
<p>Everything between <code>?</code> and <code>:</code> indicates what happens if the earlier statement is true. You can just copy the code that was inside your <code>if</code> statement before: <code>this.state.input.length &gt; char ? inputStyle = { border:'3px solid red' } :  ;</code></p>
<p>Now you need to handle the <code>else</code> portion of the ternary operator, which is everything between <code>:</code> and <code>;</code>. </p>
<p>While you didn't use an <code>else</code> statement in your first solution, you effectively used <code>inputStyle</code> as-is. So just use <code>inputStyle</code> the way it's declared earlier in your code: <code>this.state.input.length &gt; char ? inputStyle = { border:'3px solid red' } : inputStyle;</code></p>
<p>Your whole solution should look like this:</p>
<pre><code class="lang-jsx"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GateKeeper</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">input</span>: <span class="hljs-string">''</span>
    };
    <span class="hljs-built_in">this</span>.handleChange = <span class="hljs-built_in">this</span>.handleChange.bind(<span class="hljs-built_in">this</span>);
  }
  handleChange(event) {
    <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">input</span>: event.target.value })
  }
  render() {
    <span class="hljs-keyword">let</span> inputStyle = {
      <span class="hljs-attr">border</span>: <span class="hljs-string">'1px solid black'</span>
    };
    <span class="hljs-comment">// change code below this line</span>
    <span class="hljs-keyword">const</span> char = <span class="hljs-number">15</span>;
    <span class="hljs-built_in">this</span>.state.input.length &gt; char ? inputStyle = { <span class="hljs-attr">border</span>:<span class="hljs-string">'3px solid red'</span> } : inputStyle;
    <span class="hljs-comment">// change code above this line</span>
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Don't Type Too Much:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
          <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>
          <span class="hljs-attr">style</span>=<span class="hljs-string">{inputStyle}</span>
          <span class="hljs-attr">value</span>=<span class="hljs-string">{this.state.input}</span>
          <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this.handleChange}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
};
</code></pre>
<p>And that's it – you should be able to pass the challenge! Now go forth and conditionally style React components to your heart's content.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Targeting Click of “Clear” Button (X) on Input Field ]]>
                </title>
                <description>
                    <![CDATA[ jQuery makes it easy to get your project up and running. Though it's fallen out of favor in recent years, it's still worth learning the basics, especially if you want quick access to its powerful methods. But while jQuery is a powerful library, it ca... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/targeting-click-of-clear-button-x-on-input-field/</link>
                <guid isPermaLink="false">66c36042ef766eb77cd787c7</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ jQuery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:19:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a98740569d1a4ca2689.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>jQuery makes it easy to get your project up and running. Though it's fallen out of favor in recent years, it's still worth learning the basics, especially if you want quick access to its powerful methods.</p>
<p>But while jQuery is a powerful library, it can't do everything. That's where having solid understanding of vanilla JavaScript comes in handy.</p>
<p>Say you have a <a target="_blank" href="https://www.freecodecamp.org/learn/coding-interview-prep/take-home-projects/build-a-wikipedia-viewer">Wikipedia Viewer</a> project like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"search"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"text"</span>&gt;</span>Search on Wikipedia<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"searchbox"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"search"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">input</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"searchbutton"</span>&gt;</span>Search<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://en.wikipedia.org/wiki/Special:Random"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"searchbutton"</span>&gt;</span>Random Article<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"resultingarticles"</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">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-js">$(<span class="hljs-string">"#searchbox"</span>).keyup(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>) </span>{
  <span class="hljs-keyword">if</span>(event.keyCode === <span class="hljs-number">13</span>) {
    $(<span class="hljs-string">"#searchbutton"</span>).click();
  };
});

$(<span class="hljs-string">"#searchbutton"</span>).click(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">var</span> searchInput = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"searchbox"</span>).value;
  searchInput = searchInput.toLowerCase();

  <span class="hljs-keyword">if</span>(searchInput !== <span class="hljs-string">""</span>) {

    <span class="hljs-keyword">var</span> myRequest = <span class="hljs-keyword">new</span> XMLHttpRequest();
    myRequest.open(<span class="hljs-string">'GET'</span>,<span class="hljs-string">'https://en.wikipedia.org/w/api.php?action=query&amp;list=search&amp;srsearch='</span>+ searchInput + <span class="hljs-string">'&amp;utf8=&amp;format=json&amp;origin=*'</span>);

      myRequest.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
      <span class="hljs-keyword">var</span> searchResults = <span class="hljs-built_in">JSON</span>.parse(myRequest.responseText);

      $(<span class="hljs-string">".resultingarticles"</span>).empty();  

      <span class="hljs-keyword">for</span>(i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">10</span>; i++) {
        <span class="hljs-keyword">var</span> articleTitle = searchResults.query.search[i].title;
        <span class="hljs-keyword">var</span> articleSnippet = searchResults.query.search[i].snippet;
        <span class="hljs-keyword">var</span> articleId = searchResults.query.search[i].pageid;
        <span class="hljs-keyword">var</span> articleLink = <span class="hljs-string">"https://en.wikipedia.org/?curid="</span> + articleId;
        $(<span class="hljs-string">".resultingarticles"</span>).append(<span class="hljs-string">"&lt;a href='"</span> + articleLink + <span class="hljs-string">"' target='_blank'&gt;"</span> + <span class="hljs-string">"&lt;div class='article'&gt;"</span> + <span class="hljs-string">"&lt;p&gt;"</span>+articleTitle+<span class="hljs-string">"&lt;/p&gt;"</span> + <span class="hljs-string">"&lt;p&gt;"</span> + articleSnippet + <span class="hljs-string">"&lt;/p&gt;"</span> + <span class="hljs-string">"&lt;/div&gt;"</span> + <span class="hljs-string">"&lt;/a&gt;"</span>);
      };

      };

    myRequest.send();

  };
});
</code></pre>
<p>Everything is working as you expect – you can enter text into the search box, hit enter or the "Search" button, and see a list of Wikipedia articles.</p>
<p>Because you're using <code>type="search"</code> on your <code>input</code> element, the Chrome browser will automatically add an "X" to the end of the input if there's text and you hover over the input. Note that other browsers might handle <code>type="search"</code> differently.</p>
<p>When you click on the "X", the text disappears.</p>
<p>But say you already have a list of articles, and when you clear the text, you also want to clear the populated articles:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/Peek-2020-06-13-19-24.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>It turns out that clicking the "X" in the search box fires a "search" event. jQuery doesn't support the "search" event, so you'll have to write an event listener in vanilla JavaScript:</p>
<pre><code class="lang-js"><span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"searchbox"</span>).addEventListener(<span class="hljs-string">"search"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>) </span>{
  $(<span class="hljs-string">".resultingarticles"</span>).empty();  
});
</code></pre>
<p>Now when a search event is fired, you can use jQuery to clear the <code>div</code> element with the articles:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/Peek-2020-06-13-19-29.gif" alt="Image" width="600" height="400" loading="lazy"></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Unable to Find Images Based on URL in React ]]>
                </title>
                <description>
                    <![CDATA[ If you're new to React and are having trouble accessing images stored locally, you're not alone. Imagine you have your images stored in a directory next to a component like this: /src   /components     - component1     - component2 /img   ]]>
                </description>
                <link>https://www.freecodecamp.org/news/unable-to-find-images-based-on-url-in-react/</link>
                <guid isPermaLink="false">66c363d940438b5931fe09a8</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                    <category>
                        <![CDATA[ url ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:19:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a93740569d1a4ca266c.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you're new to React and are having trouble accessing images stored locally, you're not alone.</p>
<p>Imagine you have your images stored in a directory next to a component like this:</p>
<pre><code>/src
  /components
    - component1
    - component2
/img
  - img1
  - img2
</code></pre><p>And you're trying to access the images in the <code>/img</code> directory from <code>component2</code>:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Component, useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { render } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom'</span>
<span class="hljs-keyword">import</span> { useTransition, animated, config } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-spring"</span>;
<span class="hljs-keyword">import</span> imgArr <span class="hljs-keyword">from</span> <span class="hljs-string">'./images'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'../App.css'</span>;

<span class="hljs-keyword">const</span> Slideshow = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> [index, set] = useState(<span class="hljs-number">0</span>)
  <span class="hljs-keyword">const</span> transitions = useTransition(imgArr[index], <span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> item.id, {
    <span class="hljs-attr">from</span>: { <span class="hljs-attr">opacity</span>: <span class="hljs-number">0</span> },
    <span class="hljs-attr">enter</span>: {<span class="hljs-attr">opacity</span>: <span class="hljs-number">1</span> },
    <span class="hljs-attr">leave</span>: { <span class="hljs-attr">opacity</span>: <span class="hljs-number">0</span> },
    <span class="hljs-attr">config</span>: config.molasses,
  })
  useEffect(<span class="hljs-function">() =&gt;</span> <span class="hljs-keyword">void</span> <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =&gt;</span> set(<span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> (state + <span class="hljs-number">1</span>) % <span class="hljs-number">4</span>), <span class="hljs-number">2000</span>), [])
  <span class="hljs-keyword">return</span> transitions.map(<span class="hljs-function">(<span class="hljs-params">{ item, props, key }</span>) =&gt;</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">animated.div</span>
      <span class="hljs-attr">key</span>=<span class="hljs-string">{key}</span>
      <span class="hljs-attr">className</span>=<span class="hljs-string">"bg"</span>
      <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">...props</span>, <span class="hljs-attr">slideshowContainerStyle</span>}}
    &gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"img"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{require(item.url)}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>/&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">animated.div</span>&gt;</span></span>
  ))
}

<span class="hljs-keyword">const</span> slideshowContainerStyle = {
 <span class="hljs-attr">width</span>: <span class="hljs-string">'80%'</span>,
 <span class="hljs-attr">height</span>: <span class="hljs-string">'300px'</span>
}


<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Slideshow;
</code></pre>
<p>You've tried the paths <code>../img/img1.jpg</code> and <code>..img/img1.jpg</code>, but get <code>Error: Cannot find module '&lt;path&gt;'</code> .</p>
<p>So what's going on here?</p>
<h2 id="heading-a-little-about-create-react-app">A little about <code>create-react-app</code></h2>
<p>Like most people, you probably used <code>create-react-app</code> to bootstrap your project.</p>
<p>In that case, using relative paths can be a bit tricky because <code>create-react-app</code> builds the HTML, CSS, and JavaScript files to an output folder:</p>
<pre><code>/public
  - index.html
  - bundle.js
  - style.css
</code></pre><p>There are a number of ways to use images with <code>create-react-app</code>, but one of the easiest is to include your images into <code>/public</code>. When your project is built, <code>/public</code> serves as the root directory.</p>
<p>You can read more about adding images or other assets in the <a target="_blank" href="https://create-react-app.dev/docs/adding-images-fonts-and-files/">Create React App docs</a>.</p>
<h2 id="heading-importing-images">Importing images</h2>
<p>If you took a look at the docs, you'll notice that the code includes <code>import</code> statements to load assets like images and fonts.</p>
<p>Importing is useful when your asset, in this case an image, is in the same directory or near the component that uses it, and won't be used anywhere else. For example, if you have an input component with buttons that use SVGs for thumbs up and thumbs down icons.</p>
<p>A bit advantage of using <code>import</code> is that it will throw an error during build time if there's a typo. This sort of checking helps ensure users won't see a broken image that slipped by.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Updating State From Child Component Onclick ]]>
                </title>
                <description>
                    <![CDATA[ How to update the state of a parent component from a child component is one of the most commonly asked React questions. Imagine you're trying to write a simple recipe box application, and this is your code so far: import React from "react"; import Re... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/updating-state-from-child-component-onclick/</link>
                <guid isPermaLink="false">66c36423c00e5b110b380495</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:19:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a94740569d1a4ca2673.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>How to update the state of a parent component from a child component is one of the most commonly asked React questions.</p>
<p>Imagine you're trying to write a simple recipe box application, and this is your code so far:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom"</span>;

<span class="hljs-keyword">import</span> RecipeBox <span class="hljs-keyword">from</span> <span class="hljs-string">"./RecipeBox"</span>;

<span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"root"</span>);
ReactDOM.render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RecipeBox</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
  rootElement
);
</code></pre>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RecipeBox</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">input</span>: <span class="hljs-string">""</span>,
      <span class="hljs-attr">recipeList</span>: [
        {
          <span class="hljs-attr">recipe</span>: <span class="hljs-string">"Tacos"</span>,
          <span class="hljs-attr">directions</span>: <span class="hljs-string">"make tacos"</span>,
          <span class="hljs-attr">ingredients</span>: [<span class="hljs-string">"meat"</span>]
        },
        {
          <span class="hljs-attr">recipe</span>: <span class="hljs-string">"pizza"</span>,
          <span class="hljs-attr">directions</span>: <span class="hljs-string">"bake"</span>,
          <span class="hljs-attr">ingredients</span>: [<span class="hljs-string">"dough"</span>]
        }
      ]
    };
    <span class="hljs-built_in">this</span>.handleChange = <span class="hljs-built_in">this</span>.handleChange.bind(<span class="hljs-built_in">this</span>);
    <span class="hljs-built_in">this</span>.handleSubmit = <span class="hljs-built_in">this</span>.handleSubmit.bind(<span class="hljs-built_in">this</span>);
  }

  handleChange(event) {
    <span class="hljs-built_in">this</span>.setState({
      <span class="hljs-attr">input</span>: event.target.value
    });
  }

  handleSubmit() {
    <span class="hljs-keyword">const</span> newRecipe = <span class="hljs-built_in">this</span>.state.recipelist[<span class="hljs-number">0</span>].recipe;
    setState({
      recipeList[<span class="hljs-number">0</span>].recipe: newRecipe
    });
  }

  render() {
    <span class="hljs-keyword">const</span> ITEMS = <span class="hljs-built_in">this</span>.state.recipeList.map(<span class="hljs-function">(<span class="hljs-params">{ directions }</span>) =&gt;</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>{directions}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span>
    ));
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">EditList</span>
          <span class="hljs-attr">input</span>=<span class="hljs-string">{this.state.input}</span>
          <span class="hljs-attr">handleChange</span>=<span class="hljs-string">{this.handleChange}</span>
          <span class="hljs-attr">onSubmit</span>=<span class="hljs-string">{this.handleSubmit}</span>
        /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>{ITEMS}<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EditList</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
          <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span>
          <span class="hljs-attr">value</span>=<span class="hljs-string">{this.props.input}</span>
          <span class="hljs-attr">onChange</span>=<span class="hljs-string">{this.props.handleChange}</span>
        /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.props.onSubmit}</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}
</code></pre>
<p>Eventually you want <code>handleChange</code> to capture what the user enters and update specific recipes.</p>
<p>But when you try to run your app, you see a lot of errors in the terminal and dev console. Let's take a closer look at what's going on.</p>
<h2 id="heading-fixing-errors">Fixing errors</h2>
<p>First in <code>handleSubmit</code>, <code>setState</code> should be <code>this.setState</code>:</p>
<pre><code class="lang-jsx">handleSubmit() {
  <span class="hljs-keyword">const</span> newRecipe = <span class="hljs-built_in">this</span>.state.recipelist[<span class="hljs-number">0</span>].recipe;
  <span class="hljs-built_in">this</span>.setState({
    recipeList[<span class="hljs-number">0</span>].recipe: newRecipe
  });
}
</code></pre>
<p>You're already prop drilling, or passing things from the parent <code>RecipeBox</code> component to its child <code>EditList</code> properly. But when someone enters text into the input box and clicks "Submit", the state isn't updated the way you expect.</p>
<h2 id="heading-how-to-update-the-state-from-a-child-component">How to update the state from a child component</h2>
<p>Here you're running into issues because you're trying to update the state of a nested array (<code>recipeList[0].recipe: newRecipe</code>). That won't work in React.</p>
<p>Instead, you need to create a copy of the full <code>recipeList</code> array, modify the value of <code>recipe</code> for the element you want to update in the copied array, and assign the modified array back to <code>this.state.recipeList</code>.</p>
<p>First, use the spread syntax to create a copy of <code>this.state.recipeList</code>:</p>
<pre><code class="lang-jsx">handleSubmit() {
  <span class="hljs-keyword">const</span> recipeList = [...this.state.recipeList];
  <span class="hljs-built_in">this</span>.setState({
    recipeList[<span class="hljs-number">0</span>].recipe: newRecipe
  });
}
</code></pre>
<p>Then update the recipe for the element you want to update. Let's do the first element as a proof of concept:</p>
<pre><code class="lang-jsx">handleSubmit() {
  <span class="hljs-keyword">const</span> recipeList = [...this.state.recipeList];
  recipeList[<span class="hljs-number">0</span>].recipe = <span class="hljs-built_in">this</span>.state.input;
  <span class="hljs-built_in">this</span>.setState({
    recipeList[<span class="hljs-number">0</span>].recipe: newRecipe
  });
}
</code></pre>
<p>Finally, update the current <code>recipeList</code> with your new copy. Also, set <code>input</code> to an empty string so the textbox is empty after users click "Submit":</p>
<pre><code class="lang-jsx">handleSubmit() {
  <span class="hljs-keyword">const</span> recipeList = [...this.state.recipeList];
  recipeList[<span class="hljs-number">0</span>].recipe = <span class="hljs-built_in">this</span>.state.input;
  <span class="hljs-built_in">this</span>.setState({
    recipeList,
    <span class="hljs-attr">input</span>: <span class="hljs-string">""</span>
  });
}
</code></pre>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Coding With a Chromebook ]]>
                </title>
                <description>
                    <![CDATA[ Chromebooks are awesome. They're relatively simple and inexpensive devices that run Chrome OS, a stripped down Linux based operating system developed by Google. While they're perfect for people who just need a web browser to perform basic tasks, if y... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/coding-with-a-chromebook/</link>
                <guid isPermaLink="false">66c34787fe2a6da72cef3376</guid>
                
                    <category>
                        <![CDATA[ chromebook ]]>
                    </category>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:18:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a9c740569d1a4ca26a5.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Chromebooks are awesome. They're relatively simple and inexpensive devices that run <a target="_blank" href="https://en.wikipedia.org/wiki/Chrome_OS">Chrome OS</a>, a stripped down Linux based operating system developed by Google.</p>
<p>While they're perfect for people who just need a web browser to perform basic tasks, if you're getting into development with just a Chromebook, you might be wondering if it's better to invest in a PC.</p>
<p>But with all the cloud-based technologies and recent updates to Chrome OS, you've got a lot of options. We'll go over a few of the popular ones here.</p>
<h2 id="heading-cloud-based-solutions">Cloud-based solutions</h2>
<p>If you're completely new to web development, there's of course <a target="_blank" href="https://www.freecodecamp.org/">freeCodeCamp.org</a>. The entire curriculum can be completed entirely in the browser and by leveraging tools like <a target="_blank" href="https://codepen.io/">CodePen</a>, <a target="_blank" href="https://codesandbox.io/">CodeSandbox</a>, <a target="_blank" href="https://www.freecodecamp.org/news/p/633c27ad-f2a2-4b7a-a56b-e85620d957dc/glitch.me">Glitch</a>, and <a target="_blank" href="https://repl.it/">Repl.it</a> for more complex projects.</p>
<p>Even experienced developers use one or more of the sites listed above for quick prototyping and for easily sharing their projects with others, all for free. While they might be slower than a native dev environment, the fact that you can access them from any internet connected device and that everything is saved to their servers is a big plus.</p>
<h2 id="heading-linux-for-chromebooks">Linux for Chromebooks</h2>
<p>As of <a target="_blank" href="https://9to5google.com/2018/09/18/google-chrome-os-69-stable-release/">Chrome OS v.69</a>, you can enable Linux for Chromebooks and install a beta version of the Linux shell on <a target="_blank" href="https://www.chromium.org/chromium-os/chrome-os-systems-supporting-linux">select Chromebooks</a>. Though the list of supported devices is short, most if not all future Chromebooks are expected to <a target="_blank" href="https://www.zdnet.com/article/all-chromebooks-will-also-be-linux-laptops-going-forward/">support this feature</a>.</p>
<p>What basically happens is that Chrome OS runs a version of Debian in a virtual machine. Because Debian is what Ubuntu, a popular Linux distribution/operating system, is based on, you should be able to install anything on your Chromebook like you would on a Debian/Ubuntu machine.</p>
<p>For example, if you want to install Firefox, all you need to do is open up the terminal and enter <code>sudo apt install firefox</code>.</p>
<p>There are some disadvantages, though. Currently this feature is in beta, and hardware acceleration is still not supported in non-Chrome OS applications. </p>
<p>Things like Firefox or VSCode will run slower than on other machines running a Linux distribution like Ubuntu natively. This also means that video decoding is a bit slower, too, and playback may suffer as a result. Also, devices like microphones and webcams are not supported yet.</p>
<p>Check out <a target="_blank" href="https://support.google.com/chromebook/answer/9145439?hl=en">this page</a> for more information about what's not supported yet.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Count Objects in an Array ]]>
                </title>
                <description>
                    <![CDATA[ Knowing how to quickly iterate through an array and count objects is deceptively simple. The length() method will tell you the total number of values in the array, but what if you only want to count those values based on certain conditions? For examp... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-count-objects-in-an-array/</link>
                <guid isPermaLink="false">66c350ddf41767c3c96bad05</guid>
                
                    <category>
                        <![CDATA[ arrays ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ object ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:18:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a9a740569d1a4ca2696.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Knowing how to quickly iterate through an array and count objects is deceptively simple. The <code>length()</code> method will tell you the total number of values in the array, but what if you only want to count those values based on certain conditions?</p>
<p>For example, imagine you have an array like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> storage = [
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'1'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'2'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'3'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'4'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'5'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'6'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'7'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'1'</span> },
];
</code></pre>
<p>And you only want to count the number of objects with <code>status</code> set to <code>'0'</code>.</p>
<p>Like with just about everything in programming, there are a number of ways to do this. We'll go through a few of the common methods below.</p>
<h2 id="heading-use-a-for-loop">Use a <code>for</code> loop</h2>
<p>Probably the easiest way would be to declare a <code>counter</code> variable, loop through the array, and iterate <code>counter</code> only if <code>status</code> is equal to <code>'0'</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> storage = [
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'1'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'2'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'3'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'4'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'5'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'6'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'7'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'1'</span> },
];

<span class="hljs-keyword">let</span> counter = <span class="hljs-number">0</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; storage.length; i++) {
  <span class="hljs-keyword">if</span> (storage[i].status === <span class="hljs-string">'0'</span>) counter++;
}

<span class="hljs-built_in">console</span>.log(counter); <span class="hljs-comment">// 6</span>
</code></pre>
<p>You could simplify this a bit by using a <code>for...of</code> loop:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> storage = [
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'1'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'2'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'3'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'4'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'5'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'6'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'7'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'1'</span> },
];

<span class="hljs-keyword">let</span> counter = <span class="hljs-number">0</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> obj <span class="hljs-keyword">of</span> storage) {
  <span class="hljs-keyword">if</span> (obj.status === <span class="hljs-string">'0'</span>) counter++;
}

<span class="hljs-built_in">console</span>.log(counter); <span class="hljs-comment">// 6</span>
</code></pre>
<p>Also, you could create a function to do the same thing if you have other arrays of objects to count conditionally:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> storage = [
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'1'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'2'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'3'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'4'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'5'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'6'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'7'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'1'</span> },
];

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">statusCounter</span>(<span class="hljs-params">inputs</span>) </span>{
  <span class="hljs-keyword">let</span> counter = <span class="hljs-number">0</span>;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> input <span class="hljs-keyword">of</span> inputs) {
    <span class="hljs-keyword">if</span> (input.status === <span class="hljs-string">'0'</span>) counter += <span class="hljs-number">1</span>;
  }
  <span class="hljs-keyword">return</span> counter;
}

statusCounter(storage); <span class="hljs-comment">// 6</span>
</code></pre>
<h2 id="heading-use-array-methods">Use array methods</h2>
<p>JavaScript includes a bunch of <a target="_blank" href="https://www.freecodecamp.org/news/javascript-standard-objects-arrays/">helpful methods</a> when working with arrays. Each one can be chained to an array and passed different parameters to work with while iterating through the elements in the array.</p>
<p>The two we'll look at are <code>filter()</code> and <code>reduce()</code>.</p>
<h3 id="heading-filter"><code>filter()</code></h3>
<p>The filter method does just that – it iterates through each element in the array and filters out all elements that don't meet the condition(s) you provide. It then returns a new array with all the elements that returned true based on your condition(s).</p>
<p>For example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> storage = [
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'1'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'2'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'3'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'4'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'5'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'6'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'7'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'1'</span> },
];

<span class="hljs-keyword">const</span> count = storage.filter(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">item</span>)</span>{
  <span class="hljs-keyword">if</span> (item.status === <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
  }
});

<span class="hljs-comment">/*
[
  { data: '1', status: '0' },
  { data: '2', status: '0' },
  { data: '3', status: '0' },
  { data: '4', status: '0' },
  { data: '5', status: '0' },
  { data: '6', status: '0' }
] 
*/</span>
</code></pre>
<p>Now that you've filtered out the object with <code>status: '1'</code>, just call the <code>length()</code> method on the new array to get the total count of objects with <code>status: '1'</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> storage = [
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'1'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'2'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'3'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'4'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'5'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'6'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'7'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'1'</span> },
];

<span class="hljs-keyword">const</span> count = storage.filter(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">item</span>)</span>{
  <span class="hljs-keyword">if</span> (item.status === <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
  }
}).length; <span class="hljs-comment">// 6</span>
</code></pre>
<p>But this can be shortened a lot with ES6 syntax:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> storage = [
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'1'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'2'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'3'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'4'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'5'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'6'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'7'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'1'</span> },
];

<span class="hljs-keyword">const</span> count = storage.filter(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> item.status === <span class="hljs-string">'0'</span>).length; <span class="hljs-comment">// 6</span>
</code></pre>
<h3 id="heading-reduce"><code>reduce()</code></h3>
<p>Think of the <code>reduce()</code> method like a Swiss army knife – it's extremely flexible, and lets you take an array as input and transform it into just about anything. Even better, like <code>filter()</code>, this method returns a new array, leaving the original unchanged.</p>
<p>You can read more about <code>reduce()</code> in <a target="_blank" href="https://www.freecodecamp.org/news/the-ultimate-guide-to-javascript-array-methods-reduce/">this article</a>.</p>
<p>For our purposes, we want to take an array, examine its contents, and produce a number. Here's a simple way to do that:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> storage = [
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'1'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'2'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'3'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'4'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'5'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'6'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'7'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'1'</span> },
];

<span class="hljs-keyword">const</span> count = storage.reduce(<span class="hljs-function">(<span class="hljs-params">counter, obj</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (obj.status === <span class="hljs-string">'0'</span>) counter += <span class="hljs-number">1</span>
  <span class="hljs-keyword">return</span> counter;
}, <span class="hljs-number">0</span>); <span class="hljs-comment">// 6</span>
</code></pre>
<p>You could simplify further by using ES6 syntax and a ternary operator:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> storage = [
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'1'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'2'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'3'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'4'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'5'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'6'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'7'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'1'</span> },
];

<span class="hljs-keyword">const</span> count = storage.reduce(<span class="hljs-function">(<span class="hljs-params">counter, obj</span>) =&gt;</span> obj.status === <span class="hljs-string">'0'</span> ? counter += <span class="hljs-number">1</span> : counter, <span class="hljs-number">0</span>); <span class="hljs-comment">// 6</span>
</code></pre>
<p>And even a bit more by using <a target="_blank" href="https://www.freecodecamp.org/news/array-and-object-destructuring-in-javascript/">object destructuring</a>: </p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> storage = [
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'1'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'2'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'3'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'4'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'5'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'6'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'0'</span> },
  { <span class="hljs-attr">data</span>: <span class="hljs-string">'7'</span>, <span class="hljs-attr">status</span>: <span class="hljs-string">'1'</span> },
];

<span class="hljs-keyword">const</span> count = storage.reduce(<span class="hljs-function">(<span class="hljs-params">counter, { status }</span>) =&gt;</span> status === <span class="hljs-string">'0'</span> ? counter += <span class="hljs-number">1</span> : counter, <span class="hljs-number">0</span>); <span class="hljs-comment">// 6</span>
</code></pre>
<p>So those are a few ways to go through the elements of an array and count them conditionally. Now get out there and count with confidence!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ REACT – Simple Intro Component Not Rendering? ]]>
                </title>
                <description>
                    <![CDATA[ One of the great things about React is its flexible component system. Once you get a hang of it, you can break up your application into reusable components and include them all over your project. The problem is that there are a few gotchas that make ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-simple-intro-component-not-rendering/</link>
                <guid isPermaLink="false">66c35d7e56d1719c395f31aa</guid>
                
                    <category>
                        <![CDATA[ components ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:18:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a9b740569d1a4ca269e.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>One of the great things about React is its flexible component system. Once you get a hang of it, you can break up your application into reusable components and include them all over your project.</p>
<p>The problem is that there are a few gotchas that make working with components difficult for those new to React.</p>
<p>For example, say you have the following component, <code>mainIntro.js</code>:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> mainIntro = <span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"quote-box"</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> Hunter x Hunter Quotes <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"text"</span>&gt;</span>
        "When I say it doesn't hurt me, that means I can bear it."
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"author"</span>&gt;</span>
        - Killua Zoldyck
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"new-quote"</span>&gt;</span> Next Quote <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"tweet-quote"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span>&gt;</span> Tweet this quote <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> mainIntro;
</code></pre>
<p>And want to import it into <code>App.js</code>:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> mainIntro <span class="hljs-keyword">from</span> <span class="hljs-string">'./components'</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span></span>{
    render(){
        <span class="hljs-keyword">return</span>(
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">mainIntro</span> /&gt;</span></span>
        );
    }
}


<span class="hljs-keyword">const</span> mainNode = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"quoter"</span>);
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>,mainNode);
</code></pre>
<p>But <code>mainIntro</code> isn't loading for some reason. Let's take a closer look at what's happening.</p>
<h2 id="heading-naming-your-components">Naming your components</h2>
<p>For anyone familiar with Object Oriented Programming, it's common convention to name each class with an uppercase letter. For example, a class to describe a person would be called <code>Person</code> to indicate that it's a class.</p>
<p>In React, which uses JSX rather than plain JavaScript, the first letter of a tag indicates what kind of element it is. Uppercase first characters are used to specify React components, so <code>mainIntro</code> should instead be called <code>MainIntro</code>:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> MainIntro = <span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"quote-box"</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> Hunter x Hunter Quotes <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"text"</span>&gt;</span>
        "When I say it doesn't hurt me, that means I can bear it."
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"author"</span>&gt;</span>
        - Killua Zoldyck
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"new-quote"</span>&gt;</span> Next Quote <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"tweet-quote"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span>&gt;</span> Tweet this quote <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> MainIntro;
</code></pre>
<p>While the filename can still be <code>mainIntro.js</code>, it's a good idea to capitalize the first character, too. Later when you scan the contents of the directory, you'll quickly be able to pick out that <code>MainIntro.js</code> contains a component.</p>
<p>Now <code>App.js</code> should look like this:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> MainIntro <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/MainIntro.js'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span></span>{
    render(){
        <span class="hljs-keyword">return</span>(
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">MainIntro</span> /&gt;</span></span>
        );
    }
}


<span class="hljs-keyword">const</span> mainNode = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"quoter"</span>);
ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>,mainNode);
</code></pre>
<h2 id="heading-how-is-react-installed">How is React Installed?</h2>
<p>There are two main ways to use React. First, install and set it up locally, probably through <code>create-react-app</code>. Second, through a CDN.</p>
<p>You might have noticed above that the code snippets don't actually include React in the project with <code>import React from'react';</code>. This will throw an error if you're working with React locally.</p>
<p>However, if you're using a CDN to load React, it's available globally and you don't need to import it like above.</p>
<h2 id="heading-arrow-functions">Arrow Functions</h2>
<p>Before diving into React, it's important to have a solid understanding of JavaScript, particularly ES6 syntax.</p>
<p>Take another look at the <code>MainIntro</code> component:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> MainIntro = <span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"quote-box"</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> Hunter x Hunter Quotes <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"text"</span>&gt;</span>
        "When I say it doesn't hurt me, that means I can bear it."
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"author"</span>&gt;</span>
        - Killua Zoldyck
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"new-quote"</span>&gt;</span> Next Quote <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"tweet-quote"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span>&gt;</span> Tweet this quote <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> MainIntro;
</code></pre>
<p>If you look closely at the first line, you'll notice a syntax error:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> MainIntro = <span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> (
</code></pre>
<p>You're writing a functional component, which are typically simple JavaScript functions that can accept props as an argument and return valid JSX. Of course, the syntax needs to be correct for it to return properly.</p>
<p><a target="_blank" href="https://www.freecodecamp.org/news/arrow-function-javascript-tutorial-how-to-declare-a-js-function-with-the-new-es6-syntax/">Arrow functions</a> can be written in a lot of ways, but for this example, you'll need to add the curly braces (<code>{}</code>) and make sure to return JSX from the component itself:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> MainIntro = <span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"quote-box"</span>&gt;</span>
     //... rest of the code   
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>After implementing all the changes mentioned above, your component should now render properly.</p>
<p>Though the main distinction between functional and class components in React used to be that the former was "stateless" while the latter was "statefull", React Hooks have blurred the lines between them. Read more about both components in this <a target="_blank" href="https://www.freecodecamp.org/news/functional-components-vs-class-components-in-react/">overview</a> and this deeper dive into <a target="_blank" href="https://www.freecodecamp.org/news/a-few-questions-on-functional-components/">functional components with React Hooks</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Bootstrap 4: How to Make Top Fixed Navbar Stay in Container and Not Stretch? ]]>
                </title>
                <description>
                    <![CDATA[ There are many ways to make a fixed navbar stay inside a parent's div container. We'll go over the most straightforward one here. Imagine you have the following code, modified slightly from the Bootstrap docs: <div class="container">   <nav class="na... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/bootstrap-4-how-to-make-top-fixed-navbar-stay-in-container-and-not-stretch/</link>
                <guid isPermaLink="false">66c34631d48c8b932b406b16</guid>
                
                    <category>
                        <![CDATA[ Bootstrap ]]>
                    </category>
                
                    <category>
                        <![CDATA[ bootstrap 4 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:17:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a9e740569d1a4ca26b6.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>There are many ways to make a fixed navbar stay inside a parent's <code>div</code> container. We'll go over the most straightforward one here.</p>
<p>Imagine you have the following code, modified slightly from the <a target="_blank" href="https://v4-alpha.getbootstrap.com/components/navbar/#collapsible-content">Bootstrap docs</a>:</p>
<pre><code class="lang-html"><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">nav</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar navbar-fixed-top navbar-inverse bg-inverse"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar-toggler hidden-lg-up"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">data-toggle</span>=<span class="hljs-string">"collapse"</span> <span class="hljs-attr">data-target</span>=<span class="hljs-string">"#navbarResponsive"</span> <span class="hljs-attr">aria-controls</span>=<span class="hljs-string">"navbarResponsive"</span> <span class="hljs-attr">aria-expanded</span>=<span class="hljs-string">"false"</span> <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"Toggle navigation"</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"collapse navbar-toggleable-md"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"navbarResponsive"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar-brand"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">""</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"d-inline-block align-top"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>&gt;</span>Navbar
      <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav navbar-nav float-md-right"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-item active"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-link"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Home
            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"sr-only"</span>&gt;</span>(current)<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-item"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-link"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Link<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-item"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-link"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Link<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">ul</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">nav</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"next"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  hello
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span><span class="hljs-selector-class">.next</span> {
  <span class="hljs-attribute">background-color</span>: lightblue;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">60rem</span>;
}
</code></pre>
<p>And your page looks like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-solutions">Solutions</h2>
<p>While the docs read "Navbars and their contents are fluid by default. Use optional containers to limit their horizontal width," the easiest solution is to use CSS to set the width of the navbar directly:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span><span class="hljs-selector-class">.next</span> {
  <span class="hljs-attribute">background-color</span>: lightblue;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">60rem</span>;
}

<span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0px</span>;
}

<span class="hljs-selector-tag">nav</span><span class="hljs-selector-class">.navbar</span> {
  <span class="hljs-attribute">width</span>: inherit;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">0%</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">50%</span>;
  <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(-<span class="hljs-number">50%</span>);
}
</code></pre>
<p>By adding rules targeting <code>.container</code> and <code>nav.navbar</code>, your navbar is now the same width as the parent's container:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/image-46.png" alt="Image" width="600" height="400" loading="lazy"></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Iterate Through Nested Object in React.js ]]>
                </title>
                <description>
                    <![CDATA[ If you've ever worked with APIs, you'll know that the structure of the data they return can get complicated quickly. Imagine you call an API from your React project and the response looks something like this: Object1 {      Object2 {      ]]>
                </description>
                <link>https://www.freecodecamp.org/news/iterate-through-nested-object-in-react-js/</link>
                <guid isPermaLink="false">66c358a27ef110ecbf367b27</guid>
                
                    <category>
                        <![CDATA[ object ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:17:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9aa2740569d1a4ca26c5.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you've ever worked with APIs, you'll know that the structure of the data they return can get complicated quickly.</p>
<p>Imagine you call an API from your React project and the response looks something like this:</p>
<pre><code class="lang-json">Object1 {
     Object2 {
           propertyIWantToAcess:
           anotherpropertyIWantToAcess:
      }
}
</code></pre>
<p>You've stored the data within your component's state as <code>this.state.myPosts</code>, and can access the elements of the outer object with the following:</p>
<pre><code class="lang-jsx">render() {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.state.myPosts);

    <span class="hljs-keyword">const</span> data = <span class="hljs-built_in">this</span>.state.myPosts;

    <span class="hljs-keyword">const</span> display = <span class="hljs-built_in">Object</span>.keys(data).map(<span class="hljs-function">(<span class="hljs-params">d, key</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"my-posts"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{key}</span>&gt;</span>
          {data.current_route}
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
      );
    });

    <span class="hljs-keyword">return</span>(
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          { display }
        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
</code></pre>
<p>But the problem is that you aren't able to access any of the inner objects.</p>
<p>The values of the inner objects will always change, so you aren't able to hardcode their keys and iterate through those to get the proper values.</p>
<h2 id="heading-possible-solutions">Possible solutions</h2>
<p>It can be difficult to work directly with complex API responses, so let's take a step back and simplify:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> visit = <span class="hljs-function">(<span class="hljs-params">obj, fn</span>) =&gt;</span> {
    <span class="hljs-keyword">const</span> values = <span class="hljs-built_in">Object</span>.values(obj)

    values.forEach(<span class="hljs-function"><span class="hljs-params">val</span> =&gt;</span> 
        val &amp;&amp; <span class="hljs-keyword">typeof</span> val === <span class="hljs-string">"object"</span> ? visit(val, fn) : fn(val))
}

<span class="hljs-comment">// Quick test</span>
<span class="hljs-keyword">const</span> print = <span class="hljs-function">(<span class="hljs-params">val</span>) =&gt;</span> <span class="hljs-built_in">console</span>.log(val)

<span class="hljs-keyword">const</span> person = {
    <span class="hljs-attr">name</span>: {
        <span class="hljs-attr">first</span>: <span class="hljs-string">"John"</span>,
        <span class="hljs-attr">last</span>: <span class="hljs-string">"Doe"</span>
    },
    <span class="hljs-attr">age</span>: <span class="hljs-number">15</span>,
    <span class="hljs-attr">secret</span>: {
        <span class="hljs-attr">secret2</span>: {
            <span class="hljs-attr">secret3</span>: {
                <span class="hljs-attr">val</span>: <span class="hljs-string">"I ate your cookie"</span>
            }
        }
    }
}

visit(person, print)
<span class="hljs-comment">/* Output
John
Doe
15
I ate your cookie
*/</span>
</code></pre>
<p>The <code>lodash</code> library has simple methods to accomplish the same thing, but this is a quick and dirty way to do the same thing in vanilla JS.</p>
<p>But say you want to simplify further, something like:</p>
<pre><code class="lang-jsx">render() {
    <span class="hljs-comment">// Logs data</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.state.myPosts);

    <span class="hljs-keyword">const</span> data = <span class="hljs-built_in">this</span>.state.myPosts;

    <span class="hljs-comment">// Stores nested object I want to access in posts variable</span>
    <span class="hljs-keyword">const</span> posts = data.content;

    <span class="hljs-comment">// Successfully logs nested object I want to access</span>
    <span class="hljs-built_in">console</span>.log(posts);

    <span class="hljs-comment">// Error, this will not allow me to pass posts variable to Object.keys</span>
    <span class="hljs-keyword">const</span> display = <span class="hljs-built_in">Object</span>.keys(posts).map(<span class="hljs-function"><span class="hljs-params">key</span> =&gt;</span>
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{key}</span>&gt;</span>{posts[key]}<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span></span>
    )


    <span class="hljs-keyword">return</span>(
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        {display}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
 }
</code></pre>
<p>But you get an error, <code>TypeError: can't convert undefined to object error</code> whenever you try to pass <code>posts</code> to <code>Object.keys</code>.</p>
<p>Keep in mind that this error has nothing to do with React. It's illegal to pass an object as a child of a component.</p>
<p><code>Object.keys()</code> only returns the keys of the object that's passed in as a parameter. You'll need to call it multiple times to iterate through all the nested keys.</p>
<p>If you need to display the whole nested object, one option is to use a function to convert each object into a React component and pass it as an array:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> data= []

visit(obj, <span class="hljs-function">(<span class="hljs-params">val</span>) =&gt;</span> {
    data.push(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{val}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>)  <span class="hljs-comment">// wraps any non-object type inside &lt;p&gt;</span>
})
...
return &lt;SomeComponent&gt; {data} &lt;/SomeComponent&gt;
</code></pre>
<h2 id="heading-useful-packages">Useful packages</h2>
<p>Another option is to use a package like <a target="_blank" href="https://www.npmjs.com/package/json-query">json-query</a> to help iterate through the nested JSON data.</p>
<p>Here's a modified version of the <code>render</code> function above using <code>json-query</code>:</p>
<pre><code class="lang-jsx"> render() {
   <span class="hljs-keyword">const</span> utopian = <span class="hljs-built_in">Object</span>.keys(<span class="hljs-built_in">this</span>.state.utopianCash);
   <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.state.utopianCash);

   <span class="hljs-keyword">var</span> author = jsonQuery(<span class="hljs-string">'[*][author]'</span>, { <span class="hljs-attr">data</span>: <span class="hljs-built_in">this</span>.state.utopianCash }).value
   <span class="hljs-keyword">var</span> title = jsonQuery(<span class="hljs-string">'[*][title]'</span>, { <span class="hljs-attr">data</span>: <span class="hljs-built_in">this</span>.state.utopianCash }).value
   <span class="hljs-keyword">var</span> payout = jsonQuery(<span class="hljs-string">'[*][total_payout_value]'</span>, { <span class="hljs-attr">data</span>: <span class="hljs-built_in">this</span>.state.utopianCash }).value
   <span class="hljs-keyword">var</span> postLink = jsonQuery(<span class="hljs-string">'[*][url]'</span>, { <span class="hljs-attr">data</span>: <span class="hljs-built_in">this</span>.state.utopianCash }).value
   <span class="hljs-keyword">var</span> pendingPayout = jsonQuery(<span class="hljs-string">'[*][pending_payout_value]'</span>, { <span class="hljs-attr">data</span>: <span class="hljs-built_in">this</span>.state.utopianCash }).value
   <span class="hljs-keyword">var</span> netVotes = jsonQuery(<span class="hljs-string">'[*][net_votes]'</span>, { <span class="hljs-attr">data</span>: <span class="hljs-built_in">this</span>.state.utopianCash }).value


   <span class="hljs-keyword">let</span> display = utopian.map(<span class="hljs-function">(<span class="hljs-params">post, i</span>) =&gt;</span> {
     <span class="hljs-keyword">return</span> (
       <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"utopian-items"</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">strong</span>&gt;</span>Author: <span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
          {author[i]}
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</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">strong</span>&gt;</span>Title: <span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">https:</span>//<span class="hljs-attr">www.steemit.com</span>` + <span class="hljs-attr">postLink</span>[<span class="hljs-attr">i</span>]}&gt;</span>{title[i]}<span class="hljs-tag">&lt;/<span class="hljs-name">a</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">p</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>Pending Payout: <span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
            {pendingPayout[i]}
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</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">strong</span>&gt;</span>Votes: <span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
          {netVotes[i]}
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
       <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
     );
   });

    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"utopian-container"</span>&gt;</span>
        {display}
        <span class="hljs-tag">&lt;<span class="hljs-name">User</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}
</code></pre>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ The Best Udemy Web Development Courses + Top Free Courses ]]>
                </title>
                <description>
                    <![CDATA[ Here's a list of some of the most popular web development courses on Udemy: Best General Web Development Courses on Udemy: The Complete Web Developer in 2019: Zero to Mastery by Andrei Neagoie The Web Developer Bootcamp by Colt Steele The Advanced W... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-best-udemy-web-development-courses-top-free-courses/</link>
                <guid isPermaLink="false">66c360e6c7095d76345eb035</guid>
                
                    <category>
                        <![CDATA[ online courses ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                    <category>
                        <![CDATA[ udemy ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:17:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9aa1740569d1a4ca26bd.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Here's a list of some of the most popular web development courses on Udemy:</p>
<p><strong>Best General Web Development Courses on Udemy:</strong></p>
<ul>
<li>The Complete Web Developer in 2019: Zero to Mastery by Andrei Neagoie</li>
<li>The Web Developer Bootcamp by Colt Steele</li>
<li>The Advanced Web Developer Bootcamp by Colt Steele</li>
</ul>
<p><strong>Best JavaScript Courses on Udemy:</strong></p>
<ul>
<li>The Modern JavaScript Bootcamp (2019) by Andrew Mead</li>
<li>Modern JavaScript From The Beginning by Brad Traversy</li>
<li>Accelerated JavaScript Training by Maximilian Schwarzmüller</li>
<li>Accelerated ES6 JavaScript Training by Maximilian Schwarzmüller</li>
<li>ES6 JavaScript: The Complete Developer’s Guide by Stephen Grider</li>
<li>JavaScript: Understanding the Weird Parts by Anthony Alicea</li>
</ul>
<p><strong>Best React Courses on Udemy:</strong></p>
<ul>
<li>React 16 - The Complete Guide (incl. React Router 4 &amp; Redux) by Maximilian Schwarzmüller</li>
<li>Modern React with Redux by Stephen Grider</li>
<li>Advanced React and Redux by Stephen Grider</li>
</ul>
<p><strong>Feel free to click through on the page above for:</strong></p>
<ul>
<li>The reasoning for the choices above.</li>
<li>My selection of the best Udemy courses for: SQL, Python, Android, Java, WordPress &amp; Unity.</li>
<li>A link to activate $9.99 Udemy site sale: To save you time rooting around for the best deals!</li>
<li>The top free (or 100% off) Udemy courses that I’m aware of (80+ courses in various subjects).</li>
<li>Tips to make the most of Udemy</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Which Operating System Would Be Better for Web Development ]]>
                </title>
                <description>
                    <![CDATA[ If you're new to web development and are in the market for a new laptop, you might be wondering which operating system is best. Spoiler: there is no straightforward answer. One person's preference might not be the best fit for you. Unlike some iOS or... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/which-operating-system-would-be-better-for-web-development/</link>
                <guid isPermaLink="false">66c366618e244e167873863e</guid>
                
                    <category>
                        <![CDATA[ Linux ]]>
                    </category>
                
                    <category>
                        <![CDATA[ mac ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Windows ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:17:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9a9d740569d1a4ca26ad.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you're new to web development and are in the market for a new laptop, you might be wondering which operating system is best.</p>
<p>Spoiler: there is no straightforward answer. One person's preference might not be the best fit for you.</p>
<p>Unlike some iOS or some types of application development, the tools for web development are all operating system agnostic. You can write HTML, CSS, and JavaScript on Windows, Mac, Linux, and even ChromeOS if you're willing to get a bit creative.</p>
<p>So while you won't find a definitive answer in this article, here are a few pros and cons for each of the three major operating systems, Windows, Mac, and Linux.</p>
<h2 id="heading-windows">Windows</h2>
<p>Windows is easily the most popular OS in terms of market share, with an <a target="_blank" href="https://venturebeat.com/2019/09/01/net-applications-windows-10-windows-7-market-share/">estimated 87.89%</a> of PCs running some form of the operating system. Windows devices also tend to be cheaper and more configurable than Apple devices.</p>
<p>Some people have trouble with doing more advanced development with Windows, like getting Docker containers to run, trouble with Node and npm, and so on. One reason for this is that the default command line utility for Windows is PowerShell, which is quite different than Bash found on most servers that run Linux. </p>
<p>But for basic web development, all you really need is an editor like VSCode, which is another Microsoft product.</p>
<p>Recently Microsoft has been embracing open source technologies, and allow you to install things like a Bash terminal with <a target="_blank" href="https://docs.microsoft.com/en-us/windows/wsl/install-win10">Windows Subsystem for Linux</a>. These advances should further close the gap between the Windows and Unix based OSs like MacOS and Linux.</p>
<h2 id="heading-macos">MacOS</h2>
<p>Computers running MacOS make up <a target="_blank" href="https://venturebeat.com/2019/09/01/net-applications-windows-10-windows-7-market-share/">about 9.68%</a> of the total market share, which has been growing slowly over the past decade.</p>
<p>While Apple computers tend to be more expensive than those running Windows, many swear by their build quality and the flexibility of the BSD Unix based MacOS. For web developers, especially those that work with the backend, MacOS is close enough to Linux that many servers run on, making development easier to jump into.</p>
<p>One of the understated advantages that MacOS PCs have is that, with Windows running in a VM, they can run all major browsers for testing. If one of your goals as a web developer is to shoot for complete compatibility, the ability to test your projects in Chrome, Firefox Safari, Opera, Internet Explorer, and Edge is a big plus.</p>
<h2 id="heading-linux">Linux</h2>
<p>Linux is everywhere. It's what runs the web, with Unix and Unix-like OSs running on <a target="_blank" href="https://www.wired.com/2016/08/linux-took-web-now-taking-world/">about 67%</a> of all web servers as of 2016. Android, the most popular mobile OS in the world, is built on top of Linux.</p>
<p>But desktop Linux only runs on <a target="_blank" href="https://venturebeat.com/2019/09/01/net-applications-windows-10-windows-7-market-share/">about 1.72%</a> of all desktop PCs, and has been slowly slipping over the years.</p>
<p>That said, Linux is very popular among developers, who praise the OS for being free (both as in freedom and as in beer) and the ability to quickly install everything you need for your dev environment.</p>
<p>However, not all devices run well with Linux. The Linux kernel, distributions – unique operating systems that run the Linux kernel like Ubuntu, Linux Mint, Red Hat, and so on – and application developers are largely open source projects. This means they're largely supported by volunteers who work on these projects during their spare time. </p>
<p>While many devices running Linux are considered secure because so many people review code and submit patches to fix new vulnerabilities, some hardware will not work out of the box. Make sure you do your research beforehand to see if the computer or components you buy are properly supported, or buy a device with a lot of support like a Lenovo Thinkpad or Dell XPS.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Angular - How to Retrieve Filename From a Blob Response ]]>
                </title>
                <description>
                    <![CDATA[ If you're new to Angular, you might be wondering how to retrieve a filename from an API response. Imagine you have a method that makes a POST request to a remote API and receives a Blob containing a file: public downloadExcel(data): void {   const ur... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/angular-how-to-retrieve-filename-from-a-blob-response/</link>
                <guid isPermaLink="false">66c344b00fa3812cdd5ea9a7</guid>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ how-to ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:16:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9aa3740569d1a4ca26cc.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you're new to Angular, you might be wondering how to retrieve a filename from an API response.</p>
<p>Imagine you have a method that makes a POST request to a remote API and receives a <code>Blob</code> containing a file:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">public</span> downloadExcel(data): <span class="hljs-built_in">void</span> {
  <span class="hljs-keyword">const</span> url: <span class="hljs-built_in">string</span> = <span class="hljs-string">'[api endpoint here ]'</span>;
  <span class="hljs-built_in">this</span>.http.post(url, data.body, { responseType: <span class="hljs-string">'blob'</span> })
    .subscribe(<span class="hljs-function">(<span class="hljs-params">response: Blob</span>) =&gt;</span> saveAs(response, data.fileName + <span class="hljs-string">'.xlsx'</span>));
}
</code></pre>
<p>According to MDN, <code>Blob</code> objects only contain a size and type, so you'll need another way to get the actual filename.</p>
<p>But since <code>data</code> is passed as a parameter to your function, it's possible that it also includes the payload from the server. Log it to the console and see what information is included.</p>
<h2 id="heading-reading-the-response-headers">Reading the response headers</h2>
<p>Another possible option is to read the HTTP response headers themselves. </p>
<p>Since you're fetching data from an API, it's likely that you're using <code>httpClient</code> to make the request. Often API responses include helpful information in the header.</p>
<p>One thing to look at is the <code>X-Token</code> entry. But keep in mind that not all headers can be accessed from the client side, so <code>access-control-expose-headers</code> will need to be set from the server side.</p>
<p>If <code>X-Token</code> is exposed, you can use the HTTP method <code>{ observe: 'response' }</code> to get the full response, then log <code>X-Token</code> to the console:</p>
<pre><code class="lang-ts">http
  .get&lt;<span class="hljs-built_in">any</span>&gt;(<span class="hljs-string">'url'</span>, { observe: <span class="hljs-string">'response'</span> })
  .subscribe(<span class="hljs-function"><span class="hljs-params">resp</span> =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(resp.headers.get(<span class="hljs-string">'X-Token'</span>));
  });
</code></pre>
<p>It's also worth reading up on <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Response/headers">response headers</a> in general.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Cannot Read Property 'split' of Undefined ]]>
                </title>
                <description>
                    <![CDATA[ If you've ever used JavaScript's split method, there's a good chance that you've encountered the following error: TypeError: Cannot read property 'split' of undefined. There are a few reasons why you would receive this error. Most likely it's just a ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/cannot-read-property-split-of-undefined-error/</link>
                <guid isPermaLink="false">66c347268ced2460cf9e9b82</guid>
                
                    <category>
                        <![CDATA[ error ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:16:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9aa5740569d1a4ca26db.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you've ever used JavaScript's <code>split</code> method, there's a good chance that you've encountered the following error: <code>TypeError: Cannot read property 'split' of undefined</code>.</p>
<p>There are a few reasons why you would receive this error. Most likely it's just a basic misunderstanding of how <code>split</code> works and how to iterate through arrays.</p>
<p>For example, if you try to submit the following code for the <a target="_blank" href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string">Find the Longest Word in a String challenge</a>:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findLongestWord</span>(<span class="hljs-params">str</span>) </span>{ 
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; str.length; i++) {
    <span class="hljs-keyword">const</span> array = str.split(<span class="hljs-string">" "</span>);
    array[i].split(<span class="hljs-string">""</span>);
  }
}

findLongestWord(<span class="hljs-string">"The quick brown fox jumped over the lazy dog"</span>);
</code></pre>
<p>it will throw the <code>TypeError: Cannot read property 'split' of undefined</code> error.</p>
<h3 id="heading-the-split-method">The <code>split</code> method</h3>
<p>When <code>split</code> is called on a string, it splits the string into substrings based on the separator passed in as an argument. If an empty string is passed as an argument, <code>split</code> treats each character as a substring. It then returns an array containing the substrings:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> testStr1 = <span class="hljs-string">"Test test 1 2"</span>;
<span class="hljs-keyword">const</span> testStr2 = <span class="hljs-string">"cupcake pancake"</span>;
<span class="hljs-keyword">const</span> testStr3 = <span class="hljs-string">"First,Second,Third"</span>;

testStr1.split(<span class="hljs-string">" "</span>); <span class="hljs-comment">// [ 'Test', 'test', '1', '2' ]</span>
testStr2.split(<span class="hljs-string">""</span>); <span class="hljs-comment">// [ 'c', 'u', 'p', 'c', 'a', 'k', 'e', ' ', 'p', 'a', 'n', 'c', 'a', 'k', 'e' ]</span>
testStr3.split(<span class="hljs-string">","</span>); <span class="hljs-comment">// [ 'First', 'Second', 'Third' ]</span>
</code></pre>
<p>Check out <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split">MDN</a> for more details about <code>split</code>.</p>
<h3 id="heading-the-problem-explained-with-examples">The problem explained with examples</h3>
<p>Knowing what the <code>split</code> method returns and how many substrings you can expect is the key to solving <a target="_blank" href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string">this challenge</a>.</p>
<p>Let's take another look at the code above and see why it's not working as expected:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findLongestWord</span>(<span class="hljs-params">str</span>) </span>{ 
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; str.length; i++) {
    <span class="hljs-keyword">const</span> array = str.split(<span class="hljs-string">" "</span>);
    array[i].split(<span class="hljs-string">""</span>);
  }
}

findLongestWord(<span class="hljs-string">"The quick brown fox jumped over the lazy dog"</span>);
</code></pre>
<p>Splitting <code>str</code> into an array like this (<code>const array = str.split(" ");</code>) works as expected and returns <code>[ 'The',   'quick',   'brown',   'fox',   'jumped',   'over',   'the',   'lazy',   'dog' ]</code>.</p>
<p>But take a closer look at the <code>for</code> loop. Rather than using the length of <code>array</code> as a condition to iterate <code>i</code>, <code>str.length</code> is used instead. </p>
<p><code>str</code> is "The quick brown fox jumped over the lazy dog", and if you log <code>str.length</code> to the console, you'll get 44.</p>
<p>The last statement in the body of the <code>for</code> loop is what's causing the error: <code>array[i].split("");</code>. The length of <code>array</code> is 9, so <code>i</code> would quickly go way over the maximum length of <code>array</code>:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findLongestWord</span>(<span class="hljs-params">str</span>) </span>{ 
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; str.length; i++) {
    <span class="hljs-keyword">const</span> array = str.split(<span class="hljs-string">" "</span>);
    <span class="hljs-built_in">console</span>.log(array[i]);
    <span class="hljs-comment">// array[0]: "The"</span>
    <span class="hljs-comment">// array[1]: "quick"</span>
    <span class="hljs-comment">// array[2]: "brown"</span>
    <span class="hljs-comment">// ...</span>
    <span class="hljs-comment">// array[9]: "dog"</span>
    <span class="hljs-comment">// array[10]: undefined</span>
    <span class="hljs-comment">// array[11]: undefined</span>
  }
}

findLongestWord(<span class="hljs-string">"The quick brown fox jumped over the lazy dog"</span>);
</code></pre>
<p>Calling <code>array[i].split("");</code> to split each string into substrings of characters is a valid approach, but it will throw <code>TypeError: Cannot read property 'split' of undefined</code> when it's passed <code>undefined</code>.</p>
<h3 id="heading-how-to-solve-find-the-longest-word-in-a-string-with-split">How to solve Find the Longest Word in a String with <code>split</code></h3>
<p>Let's quickly go over some pseudo code for how to solve this problem:</p>
<ol>
<li>Split <code>str</code> into an array of individual words</li>
<li>Create a variable to track the greatest word length</li>
<li>Iterate through the array of words and compare the length of each word to the variable mentioned above</li>
<li>If the length of the current word is greater than the one stored in the variable, replace that value with the current word length</li>
<li>Once the length of every word is compared with the maximum word length variable, return that number from the function</li>
</ol>
<p>First, split <code>str</code> into an array of individual words:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findLongestWordLength</span>(<span class="hljs-params">str</span>) </span>{
  <span class="hljs-keyword">const</span> array = str.split(<span class="hljs-string">" "</span>);
}
</code></pre>
<p>Create a variable to keep track of the longest word length and set it to zero:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findLongestWordLength</span>(<span class="hljs-params">str</span>) </span>{
  <span class="hljs-keyword">const</span> array = str.split(<span class="hljs-string">" "</span>);
  <span class="hljs-keyword">let</span> maxWordLength = <span class="hljs-number">0</span>;
}
</code></pre>
<p>Now that the value of <code>array</code> is <code>['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']</code>, you can use <code>array.length</code> in your <code>for</code> loop:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findLongestWordLength</span>(<span class="hljs-params">str</span>) </span>{
  <span class="hljs-keyword">const</span> array = str.split(<span class="hljs-string">" "</span>);
  <span class="hljs-keyword">let</span> maxWordLength = <span class="hljs-number">0</span>;

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; array.length; i++) {

  }
}
</code></pre>
<p>Iterate through the array of words and check the length of each word. Remember that strings also have a <code>length</code> method you can call to easily get the length of a string:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findLongestWordLength</span>(<span class="hljs-params">str</span>) </span>{
  <span class="hljs-keyword">const</span> array = str.split(<span class="hljs-string">" "</span>);
  <span class="hljs-keyword">let</span> maxLength = <span class="hljs-number">0</span>;

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; array.length; i++) {
    array[i].length;
  }
}
</code></pre>
<p>Use an <code>if</code> statement check if the length of the current word (<code>array[i].length</code>) is greater than <code>maxLength</code>. If so, replace the value of <code>maxLength</code> with <code>array[i].length</code>:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findLongestWordLength</span>(<span class="hljs-params">str</span>) </span>{
  <span class="hljs-keyword">const</span> array = str.split(<span class="hljs-string">" "</span>);
  <span class="hljs-keyword">let</span> maxLength = <span class="hljs-number">0</span>;

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; array.length; i++) {
    <span class="hljs-keyword">if</span> (array[i].length &gt; maxLength) {
      maxLength = array[i].length;
    }
  }
}
</code></pre>
<p>Finally, return <code>maxLength</code> at the end of the function, after the <code>for</code> loop:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findLongestWordLength</span>(<span class="hljs-params">str</span>) </span>{
  <span class="hljs-keyword">const</span> array = str.split(<span class="hljs-string">" "</span>);
  <span class="hljs-keyword">let</span> maxLength = <span class="hljs-number">0</span>;

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; array.length; i++) {
    <span class="hljs-keyword">if</span> (array[i].length &gt; maxLength) {
      maxLength = array[i].length;
    }
  }

  <span class="hljs-keyword">return</span> maxLength;
}
</code></pre>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
