<p dir="auto">The "JQurey's <math-renderer class="js-inline-math" style="display: inline" data-static-url="https://github.githubassets.com/static" data-run-id="0d2cbc5fdb5017ef1498a347d55521ac">$.getJSON" link in the Zipline's, instead of opening the getJSON document, link to the $</math-renderer>.each document.</p>
<h4 dir="auto">Issue Description</h4>

<p dir="auto">I've compiled a few thoughts of what I think could be improved for the "Create a Set"-challenges of the "Coding Interview Data Structure Questions" section. <strong>See the comments in the code below</strong> (which FYI, was the solution I created when working through them).</p>
<p dir="auto">This is partially covered by <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="207095293" data-permission-text="Title is private" data-url="https://github.com/freeCodeCamp/freeCodeCamp/issues/13312" data-hovercard-type="issue" data-hovercard-url="/freeCodeCamp/freeCodeCamp/issues/13312/hovercard" href="https://github.com/freeCodeCamp/freeCodeCamp/issues/13312">#13312</a> and <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="207078136" data-permission-text="Title is private" data-url="https://github.com/freeCodeCamp/freeCodeCamp/issues/13303" data-hovercard-type="issue" data-hovercard-url="/freeCodeCamp/freeCodeCamp/issues/13303/hovercard" href="https://github.com/freeCodeCamp/freeCodeCamp/issues/13303">#13303</a>, but I felt that these suggestions were to much to just be a comment of any of those issues.</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// NOTE: This code should probably not be used to update the challengeSeed.
// However, it demonstrates some of the issues.

class Set {
  // TODO: Add the requirement that the constructor should handle input.
  // It should be possible to create a new set based on an array of items,
  // where duplicates gets removed.
  constructor (items = []) {
    this._set = []

    for (const x of items) {
      this.add(x)
    }
  }

  has (x) {
    return this._set.includes(x)
  }

  values () {
    // TODO: Make sure a copy is returned
    // As JS objects always are passed by reference, returning out
    // internal store would allow external code to mutate our private state.
    // Because of this, we need to return a copy.
    return this._set.slice()
  }

  add (x) {
    // TODO: improve tests - ensure boolean status is returned
    // At least to increase concistency, as some challenges have seed code
    // returning booleans, even though previous challenges doesn't test for it.
    if (this.has(x)) return false

    this._set.push(x)
    return true
  }

  remove (x) {
    // TODO: improve tests - ensure boolean status is returned
    // At least to increase concistency, as some challenges have seed code
    // returning booleans, even though previous challenges doesn't test for it.
    if (!this.has(x)) return false

    this._set.splice(this._set.indexOf(x), 1)
    return true
  }

  size () {
    return this._set.length
  }

  union (other) {
    // TODO: We should encourage the use of pure methods
    // that return a result rather than modifying the input.
    // Returning a new set would be a solution for this.
    return new Set(
      this._set.concat(other.values())
    )
  }

  intersection (other) {
    // TODO: Check for missing `)`
    // https://github.com/freeCodeCamp/freeCodeCamp/blob/staging/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json#L462-L463
    const values = other.values()
    const common = this._set.filter(x =&gt; values.includes(x))
                    .concat(values.filter(x =&gt; this._set.includes(x)))
    return new Set(common)
  }

  difference (other) {
    // TODO: Check for missing `)`
    // https://github.com/freeCodeCamp/freeCodeCamp/blob/staging/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json#L538-L539
    const values = other.values()
    const diff = this._set.filter(x =&gt; !values.includes(x))
                  .concat(values.filter(x =&gt; !this._set.includes(x)))

    return new Set(diff)
  }

  subset (other) {
    // TODO: Check for missing `)`
    // https://github.com/freeCodeCamp/freeCodeCamp/blob/staging/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json#L625-L626
    return other.values()
            .every(x =&gt; this._set.includes(x))
  }
}"><pre class="notranslate"><span class="pl-c">// NOTE: This code should probably not be used to update the challengeSeed.</span>
<span class="pl-c">// However, it demonstrates some of the issues.</span>

<span class="pl-k">class</span> <span class="pl-v">Set</span> <span class="pl-kos">{</span>
  <span class="pl-c">// TODO: Add the requirement that the constructor should handle input.</span>
  <span class="pl-c">// It should be possible to create a new set based on an array of items,</span>
  <span class="pl-c">// where duplicates gets removed.</span>
  <span class="pl-en">constructor</span> <span class="pl-kos">(</span><span class="pl-s1">items</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">]</span>

    <span class="pl-k">for</span> <span class="pl-kos">(</span><span class="pl-k">const</span> <span class="pl-s1">x</span> <span class="pl-k">of</span> <span class="pl-s1">items</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
      <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-en">add</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span>
    <span class="pl-kos">}</span>
  <span class="pl-kos">}</span>

  <span class="pl-en">has</span> <span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">return</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">includes</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span>
  <span class="pl-kos">}</span>

  <span class="pl-en">values</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-c">// TODO: Make sure a copy is returned</span>
    <span class="pl-c">// As JS objects always are passed by reference, returning out</span>
    <span class="pl-c">// internal store would allow external code to mutate our private state.</span>
    <span class="pl-c">// Because of this, we need to return a copy.</span>
    <span class="pl-k">return</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">slice</span><span class="pl-kos">(</span><span class="pl-kos">)</span>
  <span class="pl-kos">}</span>

  <span class="pl-en">add</span> <span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-c">// TODO: improve tests - ensure boolean status is returned</span>
    <span class="pl-c">// At least to increase concistency, as some challenges have seed code</span>
    <span class="pl-c">// returning booleans, even though previous challenges doesn't test for it.</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-en">has</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-k">return</span> <span class="pl-c1">false</span>

    <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span>
    <span class="pl-k">return</span> <span class="pl-c1">true</span>
  <span class="pl-kos">}</span>

  <span class="pl-en">remove</span> <span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-c">// TODO: improve tests - ensure boolean status is returned</span>
    <span class="pl-c">// At least to increase concistency, as some challenges have seed code</span>
    <span class="pl-c">// returning booleans, even though previous challenges doesn't test for it.</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-en">has</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-k">return</span> <span class="pl-c1">false</span>

    <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">splice</span><span class="pl-kos">(</span><span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">indexOf</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span><span class="pl-kos">,</span> <span class="pl-c1">1</span><span class="pl-kos">)</span>
    <span class="pl-k">return</span> <span class="pl-c1">true</span>
  <span class="pl-kos">}</span>

  <span class="pl-en">size</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">return</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-c1">length</span>
  <span class="pl-kos">}</span>

  <span class="pl-en">union</span> <span class="pl-kos">(</span><span class="pl-s1">other</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-c">// TODO: We should encourage the use of pure methods</span>
    <span class="pl-c">// that return a result rather than modifying the input.</span>
    <span class="pl-c">// Returning a new set would be a solution for this.</span>
    <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-v">Set</span><span class="pl-kos">(</span>
      <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">concat</span><span class="pl-kos">(</span><span class="pl-s1">other</span><span class="pl-kos">.</span><span class="pl-en">values</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
    <span class="pl-kos">)</span>
  <span class="pl-kos">}</span>

  <span class="pl-en">intersection</span> <span class="pl-kos">(</span><span class="pl-s1">other</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-c">// TODO: Check for missing `)`</span>
    <span class="pl-c">// https://github.com/freeCodeCamp/freeCodeCamp/blob/staging/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json#L462-L463</span>
    <span class="pl-k">const</span> <span class="pl-s1">values</span> <span class="pl-c1">=</span> <span class="pl-s1">other</span><span class="pl-kos">.</span><span class="pl-en">values</span><span class="pl-kos">(</span><span class="pl-kos">)</span>
    <span class="pl-k">const</span> <span class="pl-s1">common</span> <span class="pl-c1">=</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">filter</span><span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">values</span><span class="pl-kos">.</span><span class="pl-en">includes</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
                    <span class="pl-kos">.</span><span class="pl-en">concat</span><span class="pl-kos">(</span><span class="pl-s1">values</span><span class="pl-kos">.</span><span class="pl-en">filter</span><span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">includes</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
    <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-v">Set</span><span class="pl-kos">(</span><span class="pl-s1">common</span><span class="pl-kos">)</span>
  <span class="pl-kos">}</span>

  <span class="pl-en">difference</span> <span class="pl-kos">(</span><span class="pl-s1">other</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-c">// TODO: Check for missing `)`</span>
    <span class="pl-c">// https://github.com/freeCodeCamp/freeCodeCamp/blob/staging/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json#L538-L539</span>
    <span class="pl-k">const</span> <span class="pl-s1">values</span> <span class="pl-c1">=</span> <span class="pl-s1">other</span><span class="pl-kos">.</span><span class="pl-en">values</span><span class="pl-kos">(</span><span class="pl-kos">)</span>
    <span class="pl-k">const</span> <span class="pl-s1">diff</span> <span class="pl-c1">=</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">filter</span><span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-c1">=&gt;</span> <span class="pl-c1">!</span><span class="pl-s1">values</span><span class="pl-kos">.</span><span class="pl-en">includes</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
                  <span class="pl-kos">.</span><span class="pl-en">concat</span><span class="pl-kos">(</span><span class="pl-s1">values</span><span class="pl-kos">.</span><span class="pl-en">filter</span><span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-c1">=&gt;</span> <span class="pl-c1">!</span><span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">includes</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">)</span>

    <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-v">Set</span><span class="pl-kos">(</span><span class="pl-s1">diff</span><span class="pl-kos">)</span>
  <span class="pl-kos">}</span>

  <span class="pl-en">subset</span> <span class="pl-kos">(</span><span class="pl-s1">other</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-c">// TODO: Check for missing `)`</span>
    <span class="pl-c">// https://github.com/freeCodeCamp/freeCodeCamp/blob/staging/seed/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json#L625-L626</span>
    <span class="pl-k">return</span> <span class="pl-s1">other</span><span class="pl-kos">.</span><span class="pl-en">values</span><span class="pl-kos">(</span><span class="pl-kos">)</span>
            <span class="pl-kos">.</span><span class="pl-en">every</span><span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_set</span><span class="pl-kos">.</span><span class="pl-en">includes</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">What do you think of these suggested changes? <g-emoji class="g-emoji" alias="blush" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f60a.png">😊</g-emoji></p>