freeCodeCamp/curriculum/challenges/portuguese/08-coding-interview-prep/data-structures/create-a-map-data-structure...

4.8 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
8d5823c8c441eddfaeb5bdef Create a Map Data Structure 1 Criar uma estrutura de dados do mapa

Description

Os próximos desafios cobrirão mapas e tabelas de hash. Mapas são estruturas de dados que armazenam pares de valores-chave. Em JavaScript, eles estão disponíveis para nós como objetos. Mapas fornecem pesquisa rápida de itens armazenados com base em valores-chave e são estruturas de dados muito comuns e úteis. Instruções: Vamos praticar um pouco criando nosso próprio mapa. Como os objetos JavaScript fornecem uma estrutura de mapa muito mais eficiente do que qualquer coisa que possamos escrever aqui, isso se destina principalmente a um exercício de aprendizado. No entanto, os objetos JavaScript nos fornecem apenas determinadas operações. E se quiséssemos definir operações personalizadas? Use o objeto Map fornecido aqui como um wrapper em torno de um object JavaScript. Crie os seguintes métodos e operações no objeto Map:
  • add aceita um par de key, value para adicionar ao mapa.
  • remove aceita uma chave e remove a key, value associada key, value par de key, value
  • get aceita uma key e retorna o value armazenado
  • has aceita uma key e retorna verdadeiro se a chave existir ou falso, se isso não acontece.
  • values retorna uma matriz de todos os valores no mapa
  • size retorna o número de itens no mapa
  • clear esvazia o mapa

Instructions

Tests

tests:
  - text: A estrutura de dados do mapa existe.
    testString: 'assert((function() { var test = false; if (typeof Map !== "undefined") { test = new Map() }; return (typeof test == "object")})(), "The Map data structure exists.");'
  - text: 'O objeto Map possui os seguintes métodos: add, remove, get, tem, valores, clear e size.'
    testString: 'assert((function() { var test = false; if (typeof Map !== "undefined") { test = new Map() }; return (typeof test.add == "function" && typeof test.remove == "function" && typeof test.get == "function" && typeof test.has == "function" && typeof test.values == "function" && typeof test.clear == "function" && typeof test.size == "function")})(), "The Map object has the following methods: add, remove, get, has, values, clear, and size.");'
  - text: O método add adiciona itens ao mapa.
    testString: 'assert((function() { var test = false; if (typeof Map !== "undefined") { test = new Map() }; test.add(5,6); test.add(2,3); test.add(2,5); return (test.size() == 2)})(), "The add method adds items to the map.");'
  - text: O método has retorna true para itens adicionados e false para itens ausentes.
    testString: 'assert((function() { var test = false; if (typeof Map !== "undefined") { test = new Map() }; test.add("test","value"); return (test.has("test") && !test.has("false"))})(), "The has method returns true for added items and false for absent items.");'
  - text: O método get aceita as chaves como entrada e retorna os valores associados.
    testString: 'assert((function() { var test = false; if (typeof Map !== "undefined") { test = new Map() }; test.add("abc","def"); return (test.get("abc") == "def")})(), "The get method accepts keys as input and returns the associated values.");'
  - text: O método values retorna todos os valores armazenados no mapa como strings em uma matriz.
    testString: 'assert((function() { var test = false; if (typeof Map !== "undefined") { test = new Map() }; test.add("a","b"); test.add("c","d"); test.add("e","f"); var vals = test.values(); return (vals.indexOf("b") != -1 && vals.indexOf("d") != -1 && vals.indexOf("f") != -1)})(), "The values method returns all the values stored in the map as strings in an array.");'
  - text: O método clear esvazia o mapa e o método size retorna o número de itens presentes no mapa.
    testString: 'assert((function() { var test = false; if (typeof Map !== "undefined") { test = new Map() }; test.add("b","b"); test.add("c","d"); test.remove("asdfas"); var init = test.size(); test.clear(); return (init == 2 && test.size() == 0)})(), "The clear method empties the map and the size method returns the number of items present in the map.");'

Challenge Seed

var Map = function() {
  this.collection = {};
  // change code below this line
  // change code above this line
};

Solution

// solution required