Basic wild front-end data structure exercises (4)-dictionary

Source: Internet
Author: User

There are a lot of tutorials on the Internet, and you can search for the basic knowledge on your own.

The exercise is mainly based on orelly's book "data structure and algorithm JavaScript description.

See Code visible: https://github.com/dashnowords/blogs/tree/master/Structure/Dictionary

Basic dictionary knowledge

ToKey-value pairsData structure of data storage in the formJavascriptObjects are more directly used. Generally, the object constructed in this article is used only in scenarios with sorting requirements.DictionaryClass, because the object attributes are unordered.

Dictionary Application

DictionaryJavascript is one of the most commonly used technologies.Rule Mode. Policy mode refers to defining a series of algorithms and encapsulating them one by one. Separating the unchanged and changing parts is the topic of each design pattern, and the policy pattern is no exception. The purpose of the policy pattern is to separate the use of the algorithm from the implementation of the algorithm. For example, the following example calculates the salary and bonus:

var obj = {        "A": function(salary) {            return salary * 4;        },        "B" : function(salary) {            return salary * 3;        },        "C" : function(salary) {            return salary * 2;        } };var calculateBouns =function(level,salary) {    return obj[level](salary);};console.log(calculateBouns(‘A‘,10000)); // 40000
Basic exercises

ConstructDictionaryClass. Implement the following basic methods:

  • add(key, value)-- Add a record
  • find(key)-- Find the value corresponding to the specified key
  • remove(key)-- Remove a specified key-Value Pair
  • showAll( )-- Display all key-value pairs
  • clear( )-- Clear data
Exercises after class (the seventh exercise in the book)
  1. Write a program that reads the name and phone number from the text and stores it in a dictionary. The program includes the following functions: display a single phone number and display all phone numbers, add a new phone number, delete the phone number, and clear all the phone numbers.
  2. UseDictionaryClass to write a program to count the number of times each word appears in a piece of text. For example, "the brown for jumped over the Blue Fox", the corresponding output is:
the:2brown:1fox:2jumped:1over:1blue:1
  1. Modify Exercise 2 to display words alphabetically.

Basic wild front-end data structure exercises (4)-dictionary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.