freeCodeCamp/curriculum/challenges/italian/10-coding-interview-prep/data-structures/create-and-add-to-sets-in-e...

1.8 KiB

id title challengeType forumTopicId dashedName
587d8254367417b2b2512c70 Creare e aggiungere ai set in ES6 1 301636 create-and-add-to-sets-in-es6

--description--

Ora che avete lavorato su ES5, state per eseguire qualcosa di simile in ES6. Questo sarà molto più facile. ES6 contiene una struttura di dati integrata Set quindi molte delle operazioni che hai scritto a mano sono ora incluse per te. Diamo un'occhiata:

Per creare un nuovo set vuoto:

var set = new Set();

È possibile creare un set con un valore:

var set = new Set(1);

È possibile creare un set con un array:

var set = new Set([1, 2, 3]);

Una volta creato un set, puoi aggiungere i valori che desideri utilizzando il metodo add:

var set = new Set([1, 2, 3]);
set.add([4, 5, 6]);

Come promemoria, un insieme è una struttura di dati che non può contenere valori duplicati:

var set = new Set([1, 2, 3, 1, 2, 3]);
// set contains [1, 2, 3] only

--instructions--

Per questo esercizio, restituisci un set con i seguenti valori: 1, 2, 3, 'Taco', 'Cat', 'Awesome'

--hints--

Il tuo Set deve contenere solo i valori 1, 2, 3, Taco, Cat, Awesome.

assert(
  (function () {
    var test = checkSet();
    return (
      test.size == 6 &&
      test.has(1) &&
      test.has(2) &&
      test.has(3) &&
      test.has('Taco') &&
      test.has('Cat') &&
      test.has('Awesome')
    );
  })()
);

--seed--

--seed-contents--

function checkSet() {
  var set = new Set([1, 2, 3, 3, 2, 1, 2, 3, 1]);
  // Only change code below this line

  // Only change code above this line
  console.log(Array.from(set));
  return set;
}

checkSet();

--solutions--

function checkSet(){var set = new Set([1,2,3,'Taco','Cat','Awesome']);
return set;}