<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pablo Felipe</title>
    <description>The latest articles on DEV Community by Pablo Felipe (@pablofelps).</description>
    <link>https://dev.to/pablofelps</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F643757%2Fcedb8932-2958-467e-875f-42c4ba4f0f79.jpg</url>
      <title>DEV Community: Pablo Felipe</title>
      <link>https://dev.to/pablofelps</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pablofelps"/>
    <language>en</language>
    <item>
      <title>Two small models ship in my Markdown editor. Three more didn't.</title>
      <dc:creator>Pablo Felipe</dc:creator>
      <pubDate>Thu, 12 Nov 2026 22:35:18 +0000</pubDate>
      <link>https://dev.to/pablofelps/two-small-models-ship-in-my-markdown-editor-three-more-didnt-a7h</link>
      <guid>https://dev.to/pablofelps/two-small-models-ship-in-my-markdown-editor-three-more-didnt-a7h</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt; — Marksage is a local-first Markdown editor whose assistant suggests fixes as you write. The AI in it can't write a word. Each model picks one answer from a fixed list, like which language a code block is in. It does that from a 385 KB file, in about 0.1 ms, inside your browser. Because every answer is right or wrong against a label, every model can be measured before it ships. That decided what's in the product: two models ship, and three were trained and left out. One of the three was better overall than the model in use. It still didn't ship, because it missed the one thing it was built to fix.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;On October 30, I ran a new code-language model once against a test set it had never seen. It won. It got 391 suggestions right at 93.8% precision, against 380 at 93.1% for the model already in the app.&lt;/p&gt;

&lt;p&gt;It didn't ship.&lt;/p&gt;

&lt;p&gt;The bar was written down before the run. This model existed to fix the near-twin languages: TypeScript and JavaScript, C++ and C, C# and Java. The model in use mixed those up. On exactly those languages, the new one lost: 103 right at 88.0%, against 106 at 89.8%. Better on average, worse where it was aimed. A model that wins the scoreboard and misses its own goal isn't an upgrade.&lt;/p&gt;

&lt;p&gt;That decision is the whole article in miniature. &lt;a href="https://marksage.vercel.app" rel="noopener noreferrer"&gt;Marksage&lt;/a&gt; is a Markdown editor I built. It has no backend, no accounts and no analytics: your documents stay in your browser. Its assistant, Smart Assist, suggests fixes as you write. That means a language for an untagged code block, a list item for &lt;code&gt;-item&lt;/code&gt;, a table for a pasted CSV. &lt;code&gt;Tab&lt;/code&gt; accepts, &lt;code&gt;Esc&lt;/code&gt; ignores, undo works. Behind some of those suggestions are small models trained in the repository. This is how they're built, how they're measured, and why most of what I trained isn't in the app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0riflcwpk3isjk84qxk0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0riflcwpk3isjk84qxk0.png" alt="An untagged Python block and the suggestion card: Add language &amp;quot;python&amp;quot;, Tab to accept, Esc to dismiss" width="799" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A model that picks, never writes
&lt;/h2&gt;

&lt;p&gt;Most AI features today generate text. Smart Assist never does. Every question it asks has its answers listed up front, and the engine has to pick from the list. The whole contract is three methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;DecisionEngine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'heuristic'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s1"&gt;'model'&lt;/span&gt;
  &lt;span class="nf"&gt;ready&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="cm"&gt;/** Pick one of `options` (key → human description). `q` is a stable question id. */&lt;/span&gt;
  &lt;span class="n"&gt;choice&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Decision&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;
  &lt;span class="cm"&gt;/** Place `state` on an ordered scale. `value` is the argmax index. */&lt;/span&gt;
  &lt;span class="nf"&gt;score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;probs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="cm"&gt;/** Probability that `statement` is true about `state`. */&lt;/span&gt;
  &lt;span class="nf"&gt;noul&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;statement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="n"&gt;dispose&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;choice&lt;/code&gt; picks an option. &lt;code&gt;score&lt;/code&gt; places text on a scale. &lt;code&gt;noul&lt;/code&gt; gives the probability that a statement is true. The engine decides, and my own code makes the edit. The rule that follows: if you can't list the possible answers up front, it's not a Smart Assist job.&lt;/p&gt;

&lt;p&gt;This kind of model is called a decision model. The term reached a wide audience in September 2026, when TypeSafe AI released Jev, a hosted model built to answer typed questions instead of writing text. Three days later, Convai Innovations released Laya, an open-weight model that answers the same three question types and runs on your own machine. Laya was my first plan. But its smallest checkpoint has 322 million parameters. The model I trained for "which language is this code?" has 262,333 numbers in all. That's over a thousand times smaller, in a 385 KB file.&lt;/p&gt;

&lt;p&gt;A closed answer space buys three things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nothing to parse.&lt;/strong&gt; The answer never comes back in an unexpected shape, so there is no output to validate. Checking generative output at runtime is a real problem, and a separate article.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It's cheap.&lt;/strong&gt; A decision is a few thousand multiplications: about 0.1 ms in plain JavaScript. No ML runtime, no GPU, no worker.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It's measurable.&lt;/strong&gt; The answer is right or wrong against a label. Generative output has many acceptable answers, which makes it much harder to score.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The third one shaped the whole project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules first
&lt;/h2&gt;

&lt;p&gt;Every question goes through the same order, and stops at the first step that's good enough:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Heuristic rules.&lt;/strong&gt; Patterns written by hand that the code checks directly. A line that starts with &lt;code&gt;def&lt;/code&gt; and ends with &lt;code&gt;:&lt;/code&gt; points to Python. They're instant, every decision can be explained, and they're rarely wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A small model trained for that question.&lt;/strong&gt; Only where the rules leave too much unanswered, and only where real labeled examples exist.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A general decision model.&lt;/strong&gt; For questions with no training data at all. No feature needs one yet, and Laya hasn't been tried in a browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For code-block languages, step 1 wasn't enough. The rules are precise, but they stay quiet on about three blocks in four. Writing a rule for every pattern of 20 languages never ends. That gap is what the first model was trained to fill.&lt;/p&gt;

&lt;p&gt;Rules and models share one confidence gate. A suggestion shows only if the top answer is at least 80% and at least 20 points ahead of the second:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;passesGate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;probs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minMargin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;probs&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;minMargin&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A missing suggestion costs nothing. A wrong one costs trust. So the number I care about most is precision when shown: of the suggestions you actually see, how many are right.&lt;/p&gt;

&lt;h2&gt;
  
  
  385 KB, trained in the repo
&lt;/h2&gt;

&lt;p&gt;The labels were already there. Documentation is full of fenced code blocks whose authors typed the language after the opening backticks. Those tags are free labels. For the current model, about 270,000 blocks were collected from 309 public repositories. Training used 63,751 of them, at most 6,000 per language, so the common languages don't drown out the rest.&lt;/p&gt;

&lt;p&gt;The split is by repository, never by block. Blocks from one project share names and style, so a model could pass a test by remembering the project. Whole repositories go to training, to dev (where choices are made) or to test. A leak guard also drops any training block that matches a test block.&lt;/p&gt;

&lt;p&gt;The model is fastText-style, and small on purpose:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The snippet becomes a set of clues: tokens, pairs of neighboring tokens, how lines start and end, the indentation, three-letter pieces of longer words.&lt;/li&gt;
&lt;li&gt;Each clue is hashed into one of 32,768 slots. There's no dictionary, and an unseen word still lands somewhere.&lt;/li&gt;
&lt;li&gt;Each slot holds 8 learned numbers. The snippet's clues are averaged into one list of 8.&lt;/li&gt;
&lt;li&gt;One linear layer scores the 21 answers (20 languages and "not code"), and softmax turns the scores into probabilities.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The numbers are stored as 8-bit integers with one scale per slot: 394,280 bytes. Training runs 8 epochs in plain TypeScript, in about 15 seconds. The same code builds the clues in training and in the browser, so the model sees in the app exactly what it saw in training.&lt;/p&gt;

&lt;p&gt;Raw confidences from a model like this are too high: when it says 99%, it's right much less often than that. So version 2 fits its temperature on repositories it never trained on. Five extra models each hold out a fifth of the training repositories, which gives about 7,600 out-of-repository predictions. The temperature goes up until 95% of the suggestions it would show are right. On the dev set, the effect looks like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Suggestions shown (of 280)&lt;/th&gt;
&lt;th&gt;Right when shown&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;No calibration&lt;/td&gt;
&lt;td&gt;210&lt;/td&gt;
&lt;td&gt;87.6%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Calibrated (shipped)&lt;/td&gt;
&lt;td&gt;157&lt;/td&gt;
&lt;td&gt;94.9%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Fewer suggestions, and more of them right.&lt;/p&gt;

&lt;p&gt;A second model of the same size reads document sections. The Outline gives each section a type (setup, reference, security…) from its heading. When the heading says nothing, like "Before you begin", the model reads the text under it. Its labels come from unambiguous headings, and the heading is then hidden from it. On 134 repositories it never saw, it types 13.5% of sections confidently and agrees with their heading 88.9% of the time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6xzcdyqtntc7v03ulsd6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6xzcdyqtntc7v03ulsd6.png" alt="The Outline: the section model types &amp;quot;Keeping your data private&amp;quot; as Security (✦)" width="702" height="1646"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bars written before the run
&lt;/h2&gt;

&lt;p&gt;The process is the same for every model. Choices are made on the dev set. The held-out set runs once, at the end, against a bar written down before the run. Once a held-out set has been seen, it's used up: the next version needs a fresh one. The code model has been judged on three of them.&lt;/p&gt;

&lt;p&gt;The results are mixed, and I'd rather show that than round it off. The first model's bar was the rules' precision, with far more suggestions. It met part of it: 133 right suggestions against the rules' 73 (1.8×), but about 94% precise against the rules' 97%, and just short of the coverage target. So it shipped as an opt-in mode, not the default. Version 2 was still about a point under the rules on its fresh set: 91.8% against 92.9%.&lt;/p&gt;

&lt;p&gt;Here is how the opt-in mode, &lt;strong&gt;Rules + AI&lt;/strong&gt;, compares with today's rules on the three sets:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Held-out set&lt;/th&gt;
&lt;th&gt;Rules: shown · right&lt;/th&gt;
&lt;th&gt;Rules + AI: shown · right&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;First (314 blocks)&lt;/td&gt;
&lt;td&gt;76 · 97.4%&lt;/td&gt;
&lt;td&gt;164 · 95.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fresh (284 blocks, 67 repositories)&lt;/td&gt;
&lt;td&gt;70 · 92.9%&lt;/td&gt;
&lt;td&gt;158 · 91.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Third (842 blocks, 158 repositories)&lt;/td&gt;
&lt;td&gt;239 · 92.1%&lt;/td&gt;
&lt;td&gt;408 · 93.1%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That's 1.7–2.2× the right suggestions, at about the rules' precision: a little below on two sets, a little above on the third. Real, and modest. The mode stays opt-in, and it downloads the models only after a notice.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6pxq32mzfm3myyr1hqe4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6pxq32mzfm3myyr1hqe4.png" alt="Rules + AI on an untagged block: &amp;quot;Which language?&amp;quot;, TypeScript 54% or JavaScript 44%" width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk9fk6r9246ilx8iew5vl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk9fk6r9246ilx8iew5vl.png" alt="The Assist menu with Off, Rules and Rules + AI, and the notice shown before Rules + AI downloads anything" width="800" height="614"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What didn't ship
&lt;/h2&gt;

&lt;p&gt;Three more models went through the same process and came out the other way. They're documented next to the ones that shipped, with the numbers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Structure hints.&lt;/strong&gt; The question: is this near-miss line, like &lt;code&gt;-item&lt;/code&gt; or &lt;code&gt;**Title**&lt;/code&gt;, really a list item or a heading? The model learned from 66,076 lines. It was judged on 227 hand-labeled lines from the issues and pull requests of 43 repositories it never saw.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Held-out (227 lines)&lt;/th&gt;
&lt;th&gt;Shown&lt;/th&gt;
&lt;th&gt;Right when shown&lt;/th&gt;
&lt;th&gt;True lines found (of 132)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Rules&lt;/td&gt;
&lt;td&gt;140&lt;/td&gt;
&lt;td&gt;88.6%&lt;/td&gt;
&lt;td&gt;124&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rules + the model&lt;/td&gt;
&lt;td&gt;141&lt;/td&gt;
&lt;td&gt;88.7%&lt;/td&gt;
&lt;td&gt;125&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It met the bar by one line in 227. That's not worth a 385 KB download.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Paste types.&lt;/strong&gt; The question: what kind of text is this paste (CSV, a list, JSON, code…)? It trained on 16,977 pastes, and was judged on 53 real pastes from 19 sources outside its data. The rules showed 41 answers at 97.6%. The model showed 44 at 90.9%. It took &lt;code&gt;kubectl&lt;/code&gt; output for code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code model v3.&lt;/strong&gt; This is the one from the top. Mean pooling averages a snippet's clues, so a few decisive ones can drown: &lt;code&gt;: number&lt;/code&gt; pushes hard toward TypeScript, but it's 5 clues out of 70. Version 3 kept each dimension's strongest clue next to the average (mean + max pooling). It was better overall, and worse on the near-twins. On TypeScript and JavaScript alone, it got 22 right and 5 wrong, against v2's 33 right and 4 wrong.&lt;/p&gt;

&lt;p&gt;The cause was in the labels, not the model. Only 42% of the code tagged TypeScript in training has any TypeScript-only syntax. The rest is valid JavaScript, and its tag says which project it came from. No way of pooling clues can tell those apart.&lt;/p&gt;

&lt;p&gt;The section model also has a type it never learned: "explanation". Two training rounds got 0 right on the dev set, because explanatory prose uses the same words as any topic. That type is read from headings only.&lt;/p&gt;

&lt;p&gt;Not shipping these was my call, and so was the rule that came out of them. A model is used only where, on a fresh held-out set, it gets clearly more answers right than the rules at about the same precision. The three modes say exactly that: &lt;strong&gt;Off&lt;/strong&gt;, &lt;strong&gt;Rules&lt;/strong&gt; and &lt;strong&gt;Rules + AI&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The v3 run found one more problem. Rebuilding the training data showed that v2 couldn't be reproduced exactly from its recorded sources: 77% dev accuracy against the shipped file's 81.8%. So the shipped file became the fixed baseline that every new model is compared with. Every code model shipped from now on carries a fingerprint of the data it learned from.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6jk518y724g09418bk7p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6jk518y724g09418bk7p.png" alt="The story page entry &amp;quot;Better overall, not where it was aimed&amp;quot;, with its numbers" width="800" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Marksage reads its own docs
&lt;/h2&gt;

&lt;p&gt;If Marksage can tell that a README has a vague link or a 90-word sentence, its own README should pass. So &lt;code&gt;npm run check:docs&lt;/code&gt; runs every Smart Assist and Document-tab feature on the README and the docs pages, in English and Portuguese, in both modes. The rule is in the script's header:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Two kinds of document:
 * - The ones we ship to be read (README, the docs pages, EN + PT-BR) must be clean: no suggestion and no "Worth a
 *   look" flag, unless it's listed in ALLOWED with the reason Pablo approved. Fix the document, never the rules:
 *   a flag that's wrong is a rules finding (ROADMAP), not something to tune against these files.
 * - The sample (Pitwall, lib/sampleDocument.ts) is the inverse: every slip left in on purpose must trigger its
 *   feature, in EN and PT-BR, nothing else may show — and once every suggestion is accepted, only the two slips
 *   that have no automatic fix (the vague link and the long sentence) may remain.
 */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first run found no suggestions and 40 long-sentence flags. I fixed the documents. The run also found real bugs in the rules, which went to the roadmap instead of being tuned away:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Portuguese never read as personal.&lt;/strong&gt; The tone check looked for "você" with &lt;code&gt;\b&lt;/code&gt; in a JavaScript regex. Without the &lt;code&gt;u&lt;/code&gt; flag, &lt;code&gt;\b&lt;/code&gt; only knows ASCII letters, so there's no word boundary after "ê". Every Portuguese text came out "impersonal".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A fix that broke the document.&lt;/strong&gt; Accepting every suggestion in the sample turned &lt;code&gt;**Options**&lt;/code&gt; under &lt;code&gt;## Configuration&lt;/code&gt; into &lt;code&gt;## Options&lt;/code&gt;. That left Configuration an empty section. Now the new heading goes one level under the heading above it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Short sentences glued together.&lt;/strong&gt; The sentence splitter only splits before a capital letter, a digit, a quote or a parenthesis. A sentence that starts with inline code stays glued to the one before it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The sample is a made-up README for a lap-time CLI, with slips planted on purpose. It's the inverse test: every slip must fire, in both languages, and nothing else may show.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4qwahhvrfpugk5taum1i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4qwahhvrfpugk5taum1i.png" alt="The sample tour: Tab turns -item into a list item and Options into a heading, Esc dismisses, Cmd+Z undoes" width="760" height="721"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffjeb6grkoue6vam0pkya.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffjeb6grkoue6vam0pkya.png" alt="The Suggestions tab with the sample's 12 fixes" width="702" height="1335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F145cqi34r6janfyf5e12.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F145cqi34r6janfyf5e12.png" alt="The Document tab: a README, very easy to read, neutral tone, and two items worth a look" width="702" height="1827"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All of it runs in CI on every push: type-check, lint, unit tests with a 50% line-coverage floor, the build, a first-load size budget, the docs check and a version check. The Smart Assist evaluation prints 1,215 "✗" lines today, and CI fails if that count moves either way. A change isn't wrong by itself, but it has to be noticed and explained in the commit that makes it. The end-to-end tests run in Chromium, WebKit (desktop and iPhone) and Firefox.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it doesn't do yet
&lt;/h2&gt;

&lt;p&gt;The repository keeps its own list of gaps. These are the ones that matter if you try it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The section model reads Portuguese poorly.&lt;/strong&gt; In the sample, the privacy section is typed Security at 0.75 in English, and "other" at 0.30 in Portuguese. It learned mostly from English docs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One-liners get overconfident answers.&lt;/strong&gt; &lt;code&gt;x = 1&lt;/code&gt; comes out as Python at 97%, though it's also valid Ruby and JavaScript. A "too short to judge" rule is the obvious next step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The held-out sets are used up.&lt;/strong&gt; Any model change now needs fresh test sets first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No test runs Rules + AI in a browser.&lt;/strong&gt; The evaluation checks both models' answers in Node. Nothing yet downloads them in a page and uses them while typing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Editor basics are missing.&lt;/strong&gt; The editor has no syntax coloring, &lt;code&gt;Tab&lt;/code&gt; doesn't indent lists, and Export downloads a copy instead of saving back to the file you opened.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No real users yet.&lt;/strong&gt; With no analytics by design, the only feedback loop is people telling me. This is the first round.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;I built Marksage with AI assistance (Claude) for implementation and review. The architecture and the measurement bars are mine.&lt;/p&gt;

&lt;p&gt;Next on the list: TypeScript/JavaScript labels that say what the code is, not which project it came from. After that, connecting other AI models with your own key: an agent, with the model and the key you choose, to help with anything inside Marksage. It will be opt-in and off by default. The built-in features will still never use a generative model.&lt;/p&gt;

&lt;p&gt;You can &lt;a href="https://marksage.vercel.app" rel="noopener noreferrer"&gt;try Marksage on a sample document&lt;/a&gt;, read &lt;a href="https://marksage.vercel.app/#story" rel="noopener noreferrer"&gt;the story with every model's numbers&lt;/a&gt;, or read &lt;a href="https://github.com/PabloFLPs/Marksage" rel="noopener noreferrer"&gt;the code, the training pipeline and the model files&lt;/a&gt; (AGPL-3.0).&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Every number here comes from the repository's docs and model cards. If you've shipped a small model next to rules, I'd like to know where yours drew the line.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>softwareengineering</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Context engineering is engineering work — not prompt-writing</title>
      <dc:creator>Pablo Felipe</dc:creator>
      <pubDate>Thu, 25 Jun 2026 21:37:40 +0000</pubDate>
      <link>https://dev.to/pablofelps/context-engineering-is-engineering-work-not-prompt-writing-203g</link>
      <guid>https://dev.to/pablofelps/context-engineering-is-engineering-work-not-prompt-writing-203g</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;TL;DR — When the spec is good, implementation needs less model. I started using a top-tier model to write the spec and a cheaper, faster one to implement it — still using the strong model, just spending it on the spec instead of the implementation. The gain isn't some magic prompt phrasing; it's the context: explicit business rules, audited project constraints, a defined output contract. That's systems engineering — the discipline of anyone who's kept real software alive, whatever their stack.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Every backend dev knows the scene: the Swagger is out of date, the last hotfix shipped without a unit test, and the &lt;code&gt;README.md&lt;/code&gt; documents a command nobody's used in six months. The code works. The docs lie. And the gap between the two is exactly where AI — and we — start to go wrong.&lt;/p&gt;

&lt;p&gt;I've spent the last few months developing with AI &lt;em&gt;for real&lt;/em&gt; inside production projects, not tutorial greenfield. My takeaway was less about which model to use and more about a shift that already has a name: the move from &lt;strong&gt;prompt engineering&lt;/strong&gt; to &lt;strong&gt;context engineering&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The difference isn't semantic. Prompt engineering treats the problem as writing — finding the magic phrase. Context engineering treats it as what it always was: &lt;strong&gt;a systems engineering problem&lt;/strong&gt;. And it's where my backend background applied most directly — though anyone who's kept a real system alive has the same instinct.&lt;/p&gt;

&lt;h2&gt;
  
  
  The experiment that convinced me
&lt;/h2&gt;

&lt;p&gt;Let me start with the evidence, because that's what made me take this seriously.&lt;/p&gt;

&lt;p&gt;My reflex, for a long time, was to reach for the strongest model for everything — more expensive, smarter, fewer errors. Makes sense on paper.&lt;/p&gt;

&lt;p&gt;In practice, I saw something else. When the task's &lt;em&gt;specification&lt;/em&gt; is well done — explicit business rules, audited project constraints, a defined output format — the model capability needed for &lt;strong&gt;implementation&lt;/strong&gt; drops sharply. Enough to split the work by stage: I started using a top-tier model (currently Opus) to &lt;strong&gt;write the spec&lt;/strong&gt;, and a cheaper, faster model (Sonnet) to &lt;strong&gt;implement&lt;/strong&gt; from it. I didn't drop the strong model — I moved it: off the implementation, onto the spec.&lt;/p&gt;

&lt;p&gt;The comparison that actually matters isn't cheap implementer vs. expensive implementer. It's &lt;em&gt;strong model implementing with no structured spec&lt;/em&gt; vs. &lt;em&gt;strong model writing the spec, cheaper model implementing from it&lt;/em&gt; — and the second is both cheaper and more accurate. The accuracy isn't coming from the cheap model — it's that the spec carries the precision. With unlimited budget you could run the strong model on both stages, and that's the ceiling; the useful finding is narrower: good context makes a cheaper implementer beat the strong model left to implement without it — while saving on the highest-volume stage.&lt;/p&gt;

&lt;p&gt;Be precise about what that does and doesn't show, though. Sonnet isn't a weak model — it's a notch below Opus, not a tiny one. So this isn't evidence that some minimal model can implement anything; it's that a good spec lowers the capability &lt;em&gt;ceiling&lt;/em&gt; a task demands, and in my case that ceiling dropped below top-tier. How far it drops depends on the task.&lt;/p&gt;

&lt;p&gt;I want to be honest about the scope of this claim, because it easily turns into misleading clickbait. &lt;strong&gt;It's not a universal recipe.&lt;/strong&gt; The exact split depends on task complexity, codebase maturity, and how good your patterns already are. Apply this to a messy project with no specs and the cheap model will hallucinate, and you'll conclude I was wrong — when what was missing is exactly the half that makes the claim true.&lt;/p&gt;

&lt;p&gt;What &lt;strong&gt;is&lt;/strong&gt; generalizable is the principle behind it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Context quality lowers the model capability a task requires.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Opus→Sonnet split is just concrete evidence that the principle works. And it explains, as a bonus, why the hype migrated so fast from "prompt engineering" to "context engineering": the marginal gain from investing in context is bigger than from investing in the phrasing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What counts as "good context"
&lt;/h2&gt;

&lt;p&gt;Here's a trap I also fell into: thinking &lt;strong&gt;more&lt;/strong&gt; context is better context. It isn't. Dumping the whole repo into the window doesn't help the model infer better — it just inflates cost and dilutes signal. Good context isn't volume, it's &lt;strong&gt;precision&lt;/strong&gt;. In practice, what moves the needle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What the project does&lt;/strong&gt; — one sentence. Sounds obvious; it's almost never written down.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The stack, the architecture, and the patterns&lt;/strong&gt; — because LLMs are excellent at &lt;em&gt;replicating&lt;/em&gt;. That's their core strength. A project with consistent, documented patterns gives the model a mold to copy, and that's where hallucination drops.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clear commands in the &lt;code&gt;README.md&lt;/code&gt;&lt;/strong&gt; — silly but real example: I had a test breaking, asked the agent to fix it, and it ran the test command itself to see the error before touching anything. It only managed that because the command was documented.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The output contract&lt;/strong&gt; — defining the input and output format of functions and methods up front gives the model a target instead of a guess. One caution before calling this "TDD as executable spec," though: in my flow the same model writes both the test and the implementation, so the test isn't an independent oracle — it's the model checking its own reading of the task against itself. What actually anchors things is the contract in the spec I review by hand; the test only operationalizes it, and its assertions still have to survive Check 2 of the verifier below.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice none of this is "prompt technique." It's engineering hygiene. AI just made the cost of skipping that hygiene visible and immediate.&lt;/p&gt;

&lt;h2&gt;
  
  
  The system: a pipeline of skills
&lt;/h2&gt;

&lt;p&gt;A thesis without a method is a motivational talk. So here's the how.&lt;/p&gt;

&lt;p&gt;I treat every repeatable step of my flow as a &lt;strong&gt;skill&lt;/strong&gt;: a structured prompt with three fixed parts — &lt;em&gt;Instructions&lt;/em&gt;, &lt;em&gt;Constraints&lt;/em&gt;, and &lt;em&gt;Output Format&lt;/em&gt;. They live versioned in &lt;code&gt;.md&lt;/code&gt; files, reachable from both the editor and the agent. When I catch myself asking the model for the same thing a third time across different projects, I formalize it into a skill.&lt;/p&gt;

&lt;p&gt;The flow of a task in an existing project looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extract task description
   → spec-writer        (generates the spec from the informal ticket)
   → [review the spec]  (human in the loop — the step I don't delegate)
   → implement
   → ai-output-verifier (red-team what the AI generated)
   → code-reviewer      (senior-level critical review)
   → commit-message     (Conventional Commits from the diff)
   → pr-description     (description in the team's format)
   → push
   → review by another model on the PR
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three of these skills do the heavy lifting. Let me open all three.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The one that generates context: &lt;code&gt;spec-writer&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The heart of the pipeline. It turns an informal ticket (natural language, bullets, a Slack thread) into an implementation-ready spec. The detail that makes the difference isn't the template — it's the &lt;strong&gt;Constraints Audit&lt;/strong&gt;: before describing what the code &lt;em&gt;should do&lt;/em&gt;, the skill forces an inspection of what the project &lt;em&gt;already has&lt;/em&gt; configured that could invalidate the model's output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gu"&gt;# Skill: Spec Writer
&lt;/span&gt;
Turns an informal ticket description into an implementation-ready SDD spec.

&lt;span class="gu"&gt;## Instructions
&lt;/span&gt;1. Read the provided description.
2. Identify ambiguities BEFORE writing the spec. If there's at least 1
   critical doubt, STOP and ask.
3. Project Constraints Audit (mandatory). Before describing what the code
   should do, audit what the project ALREADY HAS configured that could
   invalidate the output. Run concrete inspection — don't assume:
   &lt;span class="k"&gt;- &lt;/span&gt;Schema (DB writes): inspect the table. NOT NULL, defaults,
     UNIQUE/FK, encoding. Sample existing data to see the expected
     format of special fields.
   &lt;span class="k"&gt;- &lt;/span&gt;Transpile/syntax (JS/TS): Babel rules may differ across
     .js, .jsx, .ts, .tsx. Confirm modern syntax (?., ??) IN THE SAME
     file type that will be modified.
   &lt;span class="k"&gt;- &lt;/span&gt;TypeScript config: target, strict, noImplicitAny.
   &lt;span class="k"&gt;- &lt;/span&gt;Lint config: rules that could reject the output.
   &lt;span class="k"&gt;- &lt;/span&gt;Dependencies in use: suggest libs ALREADY present, don't add
     new ones without justification.
   &lt;span class="k"&gt;- &lt;/span&gt;Existing patterns: grep for a similar implementation and follow the style.
4. Suggest at least 3 non-obvious edge cases from the original ticket.

&lt;span class="gu"&gt;## Constraints
&lt;/span&gt;&lt;span class="k"&gt;- &lt;/span&gt;DON'T invent business rules. If info is missing, mark it as TODO.
&lt;span class="k"&gt;- &lt;/span&gt;DON'T assume default stack/config. Use what the Constraints Audit
  found. If transpile doesn't support ?. in .jsx, the spec says
  "use traditional conditionals in .jsx".
&lt;span class="k"&gt;- &lt;/span&gt;Keep the spec short: one screen max.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why this matters: most of the hallucinations I've seen weren't the model "inventing" from nothing. They were the model &lt;strong&gt;assuming a reasonable default&lt;/strong&gt; that the specific project didn't follow — &lt;code&gt;?.&lt;/code&gt; in a file that project's Babel doesn't transpile, an insert that violates a &lt;code&gt;NOT NULL&lt;/code&gt; nobody mentioned. The Constraints Audit kills that entire class of error before the first line of code.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. The one that fights sycophancy: &lt;code&gt;ai-output-verifier&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Sycophancy is the tendency of LLMs to agree with, flatter, and validate the user at the expense of accuracy — the model tells you what you want to hear instead of acting as a neutral truth-seeker. In a flow where AI generates a lot of code, this is the &lt;strong&gt;hidden tax&lt;/strong&gt;: the code &lt;em&gt;looks&lt;/em&gt; right, &lt;em&gt;passed&lt;/em&gt; the tests (which the same AI wrote), and nobody checked whether the tests test anything.&lt;/p&gt;

&lt;p&gt;This skill formalizes distrust into a checklist. I run the checks in order and stop at the first red flag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gu"&gt;# Skill: AI Output Verifier
&lt;/span&gt;
Red-team of AI-generated output. Fights sycophancy, fabrication, and the
"code that looks right but isn't" pattern.

&lt;span class="gu"&gt;### Check 1: Fabricated references
&lt;/span&gt;Did the AI cite file:line, a function, or an API? Open each one and confirm
it exists AND does what it claimed. Red flag: a plausibly-named but
nonexistent function, a lib version different from the installed one.

&lt;span class="gu"&gt;### Check 2: Tests that don't test
&lt;/span&gt;Does each expect validate BEHAVIOR, or just that it "ran without throwing"?
Would the test fail if you commented out the implementation? Red flag:
expect(result).toBeDefined(), mocks that return the expected result
instead of simulating real behavior.

&lt;span class="gu"&gt;### Check 3: Happy path syndrome
&lt;/span&gt;Does it work with empty input (null, undefined, "")? Under concurrency?
With the external integration down? Red flag: no try/catch on I/O, no
input validation.

&lt;span class="gu"&gt;### Check 4: Adjectives without evidence
&lt;/span&gt;Did the AI use "robust", "performant", "secure", "production-ready"? Is
there a benchmark or technical argument to back it? Red flag: "more
performant" with no comparison.

&lt;span class="gu"&gt;### Check 5: Sycophancy detection
&lt;/span&gt;Did you push the AI in a direction and it changed its mind too fast,
without a new technical argument? If so, ask explicitly: "what's the
evidence for this new position?"

&lt;span class="gu"&gt;### Check 6: Plan vs delivery
&lt;/span&gt;Did the AI reference files/specs/paths as if they exist — in summaries,
conclusions, or next steps? For each path, run &lt;span class="sb"&gt;`ls`&lt;/span&gt; or open the file: does
it exist? Watch verb tense — "X becomes the destination in Y/Z/" is a plan
dressed as delivery. In the final summary, separate "I created" (verified
by &lt;span class="sb"&gt;`ls`&lt;/span&gt;) from "I suggest creating" (just a plan).

&lt;span class="gu"&gt;### Check 7: Confidence on inferences
&lt;/span&gt;Did the AI make claims it couldn't have verified directly? It must not give
the same affirmative authority to what it SAW in the code and what it
INFERRED from folder structure. Demand a level: High (directly observed) /
Medium (inferred from structure) / Low (speculative).

&lt;span class="gu"&gt;## Constraints
&lt;/span&gt;&lt;span class="k"&gt;- &lt;/span&gt;DON'T mark "verified" if any check landed on "maybe". Go back to the code.
&lt;span class="k"&gt;- &lt;/span&gt;DON'T rely on the AI's tests alone. They came from the same source that
  might have the bug.
&lt;span class="k"&gt;- &lt;/span&gt;DON'T skip a check because "it's a small change". Sycophancy scales —
  small changes are where AI cheats most.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The metric that started to matter to me isn't "how much code per day." It's &lt;strong&gt;how much bad code I kept from reaching production&lt;/strong&gt;. This skill is the instrument for that.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The one that gives the verdict: &lt;code&gt;code-reviewer&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Short and direct. Critical review at a senior backend Node/TS level, focused on correctness and security — not style, which Prettier handles.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gu"&gt;# Skill: Code Reviewer
&lt;/span&gt;
Critical code review at a senior backend Node/TS level. Focus on
correctness, security, maintainability — not style.

&lt;span class="gu"&gt;## Instructions
&lt;/span&gt;1. Read the provided diff.
2. Classify each issue as Blocker, Major, or Minor.
3. For each Blocker/Major, give a concrete example of the bug (input that
   breaks it, race condition scenario).
4. Suggest a refactor ONLY when it reduces real complexity. Not for aesthetics.
5. End with a Verdict: Ship / Fix and ship / Block.

&lt;span class="gu"&gt;## Constraints
&lt;/span&gt;&lt;span class="k"&gt;- &lt;/span&gt;Assume Node 20+, TypeScript strict, ESLint configured.
&lt;span class="k"&gt;- &lt;/span&gt;Don't comment on formatting.
&lt;span class="k"&gt;- &lt;/span&gt;Consider concurrency, DB transactions, idempotency, promise leaks,
  silent error handling.
&lt;span class="k"&gt;- &lt;/span&gt;If it's an HTTP endpoint: input validation, authz, rate limit, PII logging.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the pattern across all three: &lt;strong&gt;Instructions + Constraints + Output Format&lt;/strong&gt;, always. The Constraints are the part that matters most, because that's where you encode the "do NOT" — and the "do NOT" is what keeps the AI on the rails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this points
&lt;/h2&gt;

&lt;p&gt;The Achilles' heel of all this is documentation aging. A spec is a snapshot of the past; the code keeps changing. The direction that feels right to me is &lt;strong&gt;living documentation&lt;/strong&gt; — generated and maintained on every change, not in a heroic quarterly effort. Tools are already aiming exactly at this, and while they mature, you can approximate it: take a snapshot of the project and ask the model to generate a context file from the code + &lt;code&gt;README.md&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How I see this evolving inside a repo: alongside the &lt;code&gt;README.md&lt;/code&gt;, a &lt;code&gt;_Skills/&lt;/code&gt; folder with the team's structured prompts and anchor files (&lt;code&gt;CLAUDE.md&lt;/code&gt;, &lt;code&gt;CODEX.md&lt;/code&gt;) that list the available skills and point to the project's &lt;code&gt;CONTEXT.md&lt;/code&gt;. The model enters the repo and already knows what to read, in what order.&lt;/p&gt;

&lt;p&gt;And there's a layer beyond this one. Context engineering is about what you put &lt;em&gt;into&lt;/em&gt; a model call; the moment you wrap those calls in a system — quality gates, retries, an independent reviewer, persistent memory — you've crossed into what the field now calls &lt;em&gt;harness engineering&lt;/em&gt;: the discipline of everything that isn't the model. The skill pipeline above is already a harness in miniature. That's where I go next.&lt;/p&gt;

&lt;p&gt;None of this is exotic. It's the old engineering discipline — document decisions, keep patterns, write tests that test — with a new, impossible-to-ignore incentive: now the cost of skipping those steps shows up immediately, in the form of an agent that hallucinates. AI didn't change what good development is. It just stopped letting us pretend that shortcuts are free.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This is the flow I run today. The skills evolve every week as I catch the AI failing in new ways — if you run something similar and hit different walls, I'd want to know which.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>softwareengineering</category>
      <category>programming</category>
    </item>
    <item>
      <title>Difference Between Two Arrays in JavaScript</title>
      <dc:creator>Pablo Felipe</dc:creator>
      <pubDate>Thu, 17 Mar 2022 07:54:07 +0000</pubDate>
      <link>https://dev.to/pablofelps/difference-between-two-arrays-in-javascript-180k</link>
      <guid>https://dev.to/pablofelps/difference-between-two-arrays-in-javascript-180k</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fulbdxwgx7mtw1w8ujj2p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fulbdxwgx7mtw1w8ujj2p.png" alt="An olive green background with the text: &amp;quot;JavaScript Algorithm - Difference of Two Array&amp;quot; in it" width="799" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Introducing the &lt;code&gt;.includes&lt;/code&gt; Array entity method that is used to determine if an element belongs to an array, we will make use of it to determine which elements of the first array, is included too in the second array.&lt;/p&gt;

&lt;p&gt;With this, we will use it inside the &lt;code&gt;condition()&lt;/code&gt; function within the &lt;code&gt;.filter&lt;/code&gt; method. This callback function can be either an arrow function or a common function as a callback function. The &lt;code&gt;.filter&lt;/code&gt; can be used literally to filter array elements based on a condition, and the callback function will dictate what elements the &lt;code&gt;.filter&lt;/code&gt; will add or not to the returned array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Difference Between Two Arrays in JavaScript
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;.include&lt;/code&gt; is a method of the Array entity. It returns &lt;code&gt;true&lt;/code&gt; if the element passed as parameter is included in the array that the method is called, or &lt;code&gt;false&lt;/code&gt; if the element is not included. As a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if the element doesn't belongs to the array, we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'z'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method can only receive two parameters. If you pass more than one, it can return a wrong value to the set of elements that was passed as parameter. The first parameter is the element, as seen above; the second is the index or the &lt;code&gt;fromIndex&lt;/code&gt;, that is optional.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;fromIndex&lt;/code&gt; is the index that the &lt;code&gt;.includes&lt;/code&gt; will start "searching" for the element. Let's see the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the &lt;code&gt;3&lt;/code&gt; value is on the index &lt;code&gt;array[2]&lt;/code&gt;, it is &lt;code&gt;false&lt;/code&gt; that the array have an element equals to &lt;code&gt;3&lt;/code&gt; starting from the index &lt;code&gt;array[3]&lt;/code&gt; to the end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now for the &lt;code&gt;.filter&lt;/code&gt; method, it is also a method of the &lt;code&gt;Array&lt;/code&gt; entity and this method returns a new array filtered by a condition, that is provided by the &lt;code&gt;condition()&lt;/code&gt; function within it. Returning a new array, means that the original array which the method is called will stand immutable.&lt;/p&gt;

&lt;p&gt;Also, this &lt;code&gt;condition()&lt;/code&gt; function is a callback function. A callback function is a function passed as parameter to another function or method which is called "outer function". The callback function will be called by the outer function to do something; in the case of the &lt;code&gt;.filter&lt;/code&gt; method, it will call the condition callback function to filter the array based on this condition.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.filter&lt;/code&gt; method will call the callback function for each element in the array. So the &lt;code&gt;.filter&lt;/code&gt; will have &lt;code&gt;array.length&lt;/code&gt; iterations and will finally return a new array with a number of elements equal to the number of iterations that the callback function return a value equivalent to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, if we want all elements that have their size equal to 3, we can use the &lt;code&gt;.filter&lt;/code&gt; as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'one'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'two'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'three'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'four'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'five'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, it receives an &lt;code&gt;element&lt;/code&gt; as parameter, and if this &lt;code&gt;element&lt;/code&gt; have its size equal to 3, it returns &lt;code&gt;true&lt;/code&gt; and if not, returns &lt;code&gt;false&lt;/code&gt;. So, the &lt;code&gt;.filter&lt;/code&gt; method add any of the &lt;code&gt;element&lt;/code&gt; that the condition results in &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s1"&gt;'one'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'two'&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As expected, the &lt;code&gt;.filter&lt;/code&gt; method returned an array based on the &lt;code&gt;element.length == 3&lt;/code&gt; condition. Each value of the array that have its size equals to 3, was added to the returned array.&lt;/p&gt;

&lt;p&gt;But we want to get the difference between two arrays, and this will be possible putting all together.&lt;/p&gt;

&lt;p&gt;Will use the &lt;code&gt;.filter&lt;/code&gt; method on the array that we want to get the difference, and inside it, we will use the &lt;code&gt;.include&lt;/code&gt; as the condition, verifying if the element on the array that the &lt;code&gt;.filter&lt;/code&gt; is called is included in the second element. Let's see this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'e'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;array2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, see that the output is not the difference between the two array, but their intersection. Not that the condition &lt;code&gt;array2.includes(element)&lt;/code&gt; is comparing if the &lt;code&gt;element&lt;/code&gt; is included in the second array, and if it is &lt;code&gt;true&lt;/code&gt;, the &lt;code&gt;.filter&lt;/code&gt; will add this element to the resultant array.&lt;/p&gt;

&lt;p&gt;But if we put a logical "not" or &lt;code&gt;!&lt;/code&gt; in the condition? This way, the &lt;code&gt;.filter&lt;/code&gt; will add only elements that are not included in the second array. Check the example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'e'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;array2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'e'&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we have the difference between the two arrays.&lt;/p&gt;

&lt;p&gt;As a plus, if we want to get all elements that are not in the intersection, we can just do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'e'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'f'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'y'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'z'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;array2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we want all elements that are not &lt;code&gt;'a'&lt;/code&gt;, &lt;code&gt;'b'&lt;/code&gt; or &lt;code&gt;'c'&lt;/code&gt;, so the output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'e'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'f'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'y'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'z'&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, as the last thing we can add our solution to the difference between two arrays in a prototype method of the Array entity. The &lt;code&gt;.prototype&lt;/code&gt; is a property of the Array entity that allow us to add custom properties and methods to an entity. &lt;/p&gt;

&lt;p&gt;To make a &lt;code&gt;.difference&lt;/code&gt; method to the Array entity, we can use the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="c1"&gt;// Declaring the prototype .difference method&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;difference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;array2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'e'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'e'&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, we can use &lt;code&gt;.difference&lt;/code&gt; each time that is necessary to, instead to rewriting the logic every time.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Sort an Array of Integers in JavaScript</title>
      <dc:creator>Pablo Felipe</dc:creator>
      <pubDate>Thu, 17 Mar 2022 07:50:44 +0000</pubDate>
      <link>https://dev.to/pablofelps/sort-an-array-of-integers-in-javascript-oda</link>
      <guid>https://dev.to/pablofelps/sort-an-array-of-integers-in-javascript-oda</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3o9q3wdbm3va6n7h6dt1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3o9q3wdbm3va6n7h6dt1.jpg" alt="An image of a Macbook on a table with a little plant vase and a tea cup as background, with the JavaScript logo at right and &amp;quot;Array Methods&amp;quot; at left followed by &amp;quot;.sort()&amp;quot; below" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Introducing the &lt;code&gt;.sort&lt;/code&gt; method that can be used to sort an array by a specific order, this tutorial has the purpose of explaining why this method needs a comparison function if you want to sort an array of integer numbers correctly.&lt;/p&gt;

&lt;p&gt;This comparison function will dictate the sorting order. Also, we will explain how to use it to make a descending order sorting, plus a shorter way to use it all together, by utilizing the comparison function as an Arrow Function within the &lt;code&gt;.sort&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sort an Array of Integers in JavaScript using the &lt;code&gt;.sort&lt;/code&gt; Method
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;.sort&lt;/code&gt; method is a method of the &lt;code&gt;Array&lt;/code&gt; entity that returns an ordered array from the array that this method was originally called. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sure, this is expected as the &lt;code&gt;.sort&lt;/code&gt; method orders the array. But, if we have the below input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;12900&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;877&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;992&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have a wrong ordering like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12900&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;877&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;992&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It happens because &lt;code&gt;.sort&lt;/code&gt; default ordering is based on the &lt;code&gt;UTF-16&lt;/code&gt; or &lt;code&gt;16-bit Unit Transformation Format&lt;/code&gt;, which is an encoding of the Unicode pattern. The method converts the array values into the string type and then orders their Unicode values.&lt;/p&gt;

&lt;p&gt;With this explained, the &lt;code&gt;.sort&lt;/code&gt; method can also be used to order other data types, not only numbers. &lt;/p&gt;

&lt;p&gt;But how can the &lt;code&gt;.sort&lt;/code&gt; method be used to order an array correctly? It is simple: by using a Compare Function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Passing a Compare Function as Parameter
&lt;/h2&gt;

&lt;p&gt;As the &lt;code&gt;.sort&lt;/code&gt; method can be used without any parameter, a Compare Function is optional. Basically, this function defines the &lt;code&gt;.sort&lt;/code&gt; method ordering, and this function receives two parameters: the first element to be compared and the second element to be compared.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.sort&lt;/code&gt; method will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Put &lt;code&gt;first&lt;/code&gt; after &lt;code&gt;second&lt;/code&gt; if the &lt;code&gt;compareFunction&lt;/code&gt; return a value greater than 0;&lt;/li&gt;
&lt;li&gt;Put &lt;code&gt;first&lt;/code&gt; before &lt;code&gt;second&lt;/code&gt; if the &lt;code&gt;compareFunction&lt;/code&gt; return a value less than 0;&lt;/li&gt;
&lt;li&gt;Do nothing if the &lt;code&gt;compareFunction&lt;/code&gt; return a value equals to 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, with the &lt;code&gt;compareFunction(first, second)&lt;/code&gt;, we can dictate the order of the sort by passing an operation among the &lt;code&gt;first&lt;/code&gt; and &lt;code&gt;second&lt;/code&gt; parameters. To ascending ordering,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Ascending ordering&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// 1 is greater than 0, so .sort will put first after second.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// -1 is less than 0, so .sort will put first before second.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for descending order, we can invert the operators.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Descending ordering&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// 1 is greater than 0, so .sort will put first after second.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// -1 is less than 0, so .sort will put first before second.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, putting &lt;code&gt;compareFunction&lt;/code&gt; for ascending ordering together with the &lt;code&gt;.sort&lt;/code&gt; method, finally, we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input:&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;12900&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;877&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;992&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;// Ascending&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// 1 is greater than 0, so .sort will put first before second.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// -1 is less than 0, so .sort will put first after second.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;877&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;992&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12900&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Sort an Array of Integers in JavaScript using with the Arrow Function
&lt;/h2&gt;

&lt;p&gt;We can also reduce all the code block to a minimum syntax, utilizing Arrow functions.&lt;/p&gt;

&lt;p&gt;An Arrow function is another way to use a function with shorter syntax. Arrow Functions are anonymous functions, and this means that they are not named (are stored in variables or passed as function parameters) and can't be used in all situations.&lt;/p&gt;

&lt;p&gt;With the structure of an Arrow Function, the traditional functions can be transformed into a shorter block, as the example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Common anonymous function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Arrow function&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Besides that, the structure of an Arrow Function can return automatically the expression value without the reserved word &lt;code&gt;return&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Arrow function&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;arrowFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;arrowFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Output&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;console.log()&lt;/code&gt; prints the value of &lt;code&gt;1 + 1&lt;/code&gt;, that is &lt;code&gt;2&lt;/code&gt; even though the &lt;code&gt;arrowFunction&lt;/code&gt; does not utilize the &lt;code&gt;return&lt;/code&gt; statement. This will help us in the next step.&lt;/p&gt;

&lt;p&gt;As said, the &lt;code&gt;.sort&lt;/code&gt; method can have a Compare Function within it, and this function can be an Arrow Function. Converting the previously Compare Function structure, we can transform all that block of code to a shorter block as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input:&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Ascending: If first &amp;gt; second == true, then change one by the other.&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can drop the condition to &lt;code&gt;first &amp;lt; second&lt;/code&gt; and instead, return a &lt;code&gt;-1&lt;/code&gt; value as default, if the primary condition is not the case; given that the &lt;code&gt;0&lt;/code&gt; value to the &lt;code&gt;.sort&lt;/code&gt; method is to equal values and this way, they can have their positions changed without interfering in the final result. This way, we can reduce even more, as the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input:&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Ascending: If first &amp;gt; second == true, then change one by the other.&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look that the previous &lt;code&gt;&amp;gt;&lt;/code&gt; comparison  and default &lt;code&gt;return&lt;/code&gt; was changed to only one comparison: &lt;code&gt;first &amp;gt; second ? 1 : -1&lt;/code&gt;. This means that, if the comparison is &lt;code&gt;true&lt;/code&gt;, then returns &lt;code&gt;1&lt;/code&gt;, if not, then  returns &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We need the &lt;code&gt;?&lt;/code&gt; ternary operator because the &lt;code&gt;first &amp;gt; second&lt;/code&gt; comparison, result in only in &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. But as said, the &lt;code&gt;.sort&lt;/code&gt; method expects &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;-1&lt;/code&gt; or &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for descending ordering:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input:&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Descending: If first &amp;lt; second == true, then change one by the other.&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another way to do the same is using the &lt;code&gt;-&lt;/code&gt; ternary operator for subtraction. When we use &lt;code&gt;array.sort((first, second) =&amp;gt; first &amp;gt; second ? 1 : -1)&lt;/code&gt;, if &lt;code&gt;first - second&lt;/code&gt; result in a value greater than 0, then will be an index changing among each other. If &lt;code&gt;first - second&lt;/code&gt; result in a value less than 0, nothing will happen and for equal values, the comparison will return &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input:&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What can we do in descending order? No, it is not changing the &lt;code&gt;-&lt;/code&gt; ternary operator to &lt;code&gt;+&lt;/code&gt; because each positive number plus another positive number result in a value greater than 0. But we have a simple solution to this: invert the &lt;code&gt;first - second&lt;/code&gt; to &lt;code&gt;second - first&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This way, if &lt;code&gt;second - first&lt;/code&gt; results in a value greater than 0, then the .sort method will change their positions with each other.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input:&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Count Certain Elements of an Array in JavaScript</title>
      <dc:creator>Pablo Felipe</dc:creator>
      <pubDate>Thu, 17 Mar 2022 07:45:29 +0000</pubDate>
      <link>https://dev.to/pablofelps/count-certain-elements-of-an-array-in-javascript-4049</link>
      <guid>https://dev.to/pablofelps/count-certain-elements-of-an-array-in-javascript-4049</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frdy504lxg25f2zs5ugun.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frdy504lxg25f2zs5ugun.png" alt="An yellow background with the following text: &amp;quot;Array in JavaScript&amp;quot; inside a square brackets symbol" width="800" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Introducing the &lt;code&gt;.filter&lt;/code&gt; method that can be used literally to filter array elements based on a condition, we can use it in addition to the &lt;code&gt;.length&lt;/code&gt; property to count certain elements of an array. The condition of the &lt;code&gt;.filter&lt;/code&gt; method is provided by a function, passed as parameter.  Also, we will introduce the &lt;code&gt;.reduce&lt;/code&gt; method that executes a function for each element in the array and return a single value.&lt;/p&gt;

&lt;p&gt;The function passed as parameter can be either an arrow function or a common function as a callback function, and it will dictate what elements the &lt;code&gt;.filter&lt;/code&gt; will add or not to the returned array or what exactly the &lt;code&gt;.reduce&lt;/code&gt; method will do for each elements in the array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Count Certain Elements of an Array in JavaScript using the &lt;code&gt;.filter&lt;/code&gt; Method
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;.filter&lt;/code&gt; is a method of the &lt;code&gt;Array&lt;/code&gt; entity that returns a new array filtered by the provided condition. Returning a new array, means that the original array which the method is called will stand immutable. For example, if we want all elements that is equal to the value 'one' in an array, we can use the &lt;code&gt;.filter&lt;/code&gt; as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'one'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'two'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'three'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'four'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'five'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'one'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s1"&gt;'one'&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As expected, the &lt;code&gt;.filter&lt;/code&gt; method returned an array based on the &lt;code&gt;element == 'one'&lt;/code&gt; condition. So if the actual element of the array is equal to &lt;code&gt;'one'&lt;/code&gt;, then the filter will add this element to the returned array.&lt;/p&gt;

&lt;p&gt;But what is happening inside the &lt;code&gt;.filter&lt;/code&gt; method? We can see better by using a common function structure to the &lt;code&gt;condition()&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'one'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'two'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'three'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'four'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'five'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'one'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, we have the same first example output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s1"&gt;'one'&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;condition()&lt;/code&gt; function receives an &lt;code&gt;element&lt;/code&gt; as parameter, and if this &lt;code&gt;element&lt;/code&gt; is equals to a certain value, in this case &lt;code&gt;'one'&lt;/code&gt;, it returns &lt;code&gt;true&lt;/code&gt; and if not, returns &lt;code&gt;false&lt;/code&gt;. So, the &lt;code&gt;.filter&lt;/code&gt; method add any of the &lt;code&gt;element&lt;/code&gt; that the condition results in &lt;code&gt;true&lt;/code&gt;; as the original array has only the first element as &lt;code&gt;one&lt;/code&gt;, the &lt;code&gt;.filter&lt;/code&gt; return only one element.&lt;/p&gt;

&lt;p&gt;Note that the &lt;code&gt;condition()&lt;/code&gt; function is declared inside the &lt;code&gt;.filter&lt;/code&gt; method. If you declare it outside and call it inside the &lt;code&gt;.filter&lt;/code&gt;, it will not work, because the &lt;code&gt;element&lt;/code&gt; is part of the callback scope.&lt;/p&gt;

&lt;p&gt;Basically, a callback function is a function passed as parameter to another function which is called "outer function". The callback function will be called by the outer function to do something; in the case of the &lt;code&gt;.filter&lt;/code&gt; method, it will call the &lt;code&gt;condition()&lt;/code&gt; callback function to filter the array based on its condition.&lt;/p&gt;

&lt;p&gt;Back to our objective, the &lt;code&gt;.filter&lt;/code&gt; will finally return a new array that have &lt;code&gt;array.length&lt;/code&gt; elements, which each one is an element that the callback function returned a value equivalent to &lt;code&gt;true&lt;/code&gt;. As we want to count these filtered elements, we can easily do this with the &lt;code&gt;.length&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.length&lt;/code&gt; is as property of the Array entity that returns the number of elements in an array. This number is always greater than the highest index in that array, because the first element is always on the index equals to &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The number of elements in the &lt;code&gt;array&lt;/code&gt; is 5. 5 is a number greater than the index of the value &lt;code&gt;5&lt;/code&gt;, that is 4.&lt;/p&gt;

&lt;p&gt;Putting all together, as an example we can use the following: How to get the number of all test notes of an school, that are greater than seven? The array of the test notes is in the example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;notes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;7.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;6.7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;7.3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;9.8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;5.6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;7.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;7.8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;6.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;8.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;9.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;4.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;6.9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;7.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;8.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;6.4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;greaterThanSeven&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greaterThanSeven&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output (The array returned by the .filter is: [7.3, 9.8, 7.1, 7.8, 8, 9, 7.5, 8.5])&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, &lt;code&gt;8&lt;/code&gt; is the number of notes that are greater than 7 of all 15 notes in the original &lt;code&gt;notes&lt;/code&gt; array. &lt;/p&gt;

&lt;p&gt;Now, as another option we can use the &lt;code&gt;.reduce&lt;/code&gt; Array entity method. This method is used to execute a callback function (as well as the &lt;code&gt;.filter&lt;/code&gt;) on each element of the array, but it will finally return a single value.&lt;/p&gt;

&lt;p&gt;A simple example to the &lt;code&gt;.reduce&lt;/code&gt; working, is the sum of all element in an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="mi"&gt;15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this method can be used in other way, that is by passing the initial value. In the previous example, we can see that the &lt;code&gt;previous&lt;/code&gt; begin with the first index element in the array, being the callback function, this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
previous + current = result
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15 -&amp;gt; final result
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sum is working correctly because if we don't pass a second parameter (as the callback function is the first parameter) to the &lt;code&gt;.reduce&lt;/code&gt; method, it will consider &lt;code&gt;previous&lt;/code&gt; as the first element of the array in the first iteration.&lt;/p&gt;

&lt;p&gt;But if we want to count certain elements in the array? We cannot use the first element as the &lt;code&gt;previous&lt;/code&gt;, because it could result in a wrong value. The example below is about counting how many 2 numbers are in the array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, the result is &lt;code&gt;2&lt;/code&gt;, but the correct answer is &lt;code&gt;1&lt;/code&gt;. This happened because the &lt;code&gt;sum&lt;/code&gt; is being initialized with the first element &lt;code&gt;1&lt;/code&gt;, as we don't passed any other parameter beyond the callback function. To do the correction, we can just pass a &lt;code&gt;0&lt;/code&gt; as the second &lt;code&gt;.reduce&lt;/code&gt; parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, the &lt;code&gt;.reduce&lt;/code&gt; is making &lt;code&gt;sum + 1&lt;/code&gt; every time that the current element is equal to 2, with the &lt;code&gt;sum&lt;/code&gt; being initialized with &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extra
&lt;/h2&gt;

&lt;p&gt;As a plus, instead of using all theses logics every time that we need to count certain elements in an array, we can use the &lt;code&gt;.prototype&lt;/code&gt; Array entity property to write custom methods or properties to this entity.&lt;/p&gt;

&lt;p&gt;To make a &lt;code&gt;.countCertainElements&lt;/code&gt; method to the Array entity, we can use the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="c1"&gt;// Declaring the prototype .countCertainElements method&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;countCertainElements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arrayElement&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arrayElement&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;countCertainElements&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the same can be done for the &lt;code&gt;.reduce&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Input&lt;/span&gt;
&lt;span class="c1"&gt;// Declaring the prototype .countCertainElements method&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;countCertainElements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;countCertainElements&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
