Two exercises in JS dictionary

Source: Internet
Author: User

1. Read the name and phone number from a text file and save it in a dictionary. The functions of the program include displaying a single phone number, displaying all phone numbers, and adding new phone numbers.

2. The program stores the number of occurrences of each word in a text. Function: Displays the number of occurrences of each word, but only once per word.

Programming:

function dictionary () {
This.datastore = new Array ();
This.displaysingle = Displaysingle;
This.displayall = Displayall;
This.inserttle = Inserttle;
This.removetle = Removetle;
This.clear = clear;
This.containskey = ContainsKey;

}

function Displaysingle (key) {
Console.log (This.datastore[key]);
}
function Displayall () {
for (var key in This.datastore) {
Console.log (key+ ", +this.datastore[key]);
}
}
function ContainsKey (checkkey) {
for (var key in This.datastore) {
if (key = = Checkkey) {
return true;
}
}
}
function Inserttle (key,value) {
if (This.containskey (key)) {//has a dictionary value
This.datastore[key] = parseint (value) +1;
}else{//No dictionary value
This.datastore[key] = value;
}

}
function Removetle (key) {
Delete This.datastore[key];
}
function Clear () {
for (var key in This.datastore) {
Delete This.datastore[key];
}
}

Test Exercises 1:

var peopletle = new Dictionary ();
Peopletle.inserttle ("R", 123456);
Peopletle.inserttle ("A", 223333);
Peopletle.inserttle ("B", 854845);
Peopletle.inserttle ("C", 777878);
Peopletle.inserttle ("D", 6966587);
Peopletle.displaysingle ("B");
Peopletle.displayall ();
Peopletle.removetle ("C");
Peopletle.displayall ();
Peopletle.clear ();
Peopletle.displayall ();

Test Exercises 2:

var str1 = ["This", "was", "a", "Fly", "the", "name", "is", "a"];

var str2 = "This mt CTT DT, DT RT OT TP RT this this MT";


String.prototype.trim = function () {//go to whitespace handling
var str = this,
str = str.replace (/^\s\s*/, ","),
WS =/\s/,
i = str.length;
while (Ws.test (Str.charat ()));
Return Str.slice (0, i + 1);
}

var t = Str2.trim ();
Console.log (t);
Console.log (STR2);
Console.log (typeof (T));
var arr2 = T.replace (/\s/g, ","). Split (",");
var arr3 = [];
for (Var i=0;i<arr2.length;i++) {
Arr3.push (Arr2[i]);
}


function Checknum (str1) {
var word = new Dictionary ();
for (var i = 0;i<str1.length;i++) {
if (str1[i]== "" | | str1[i]==undefined| | Str1[i]==null) {
return false;
}else{
Word.inserttle (str1[i],1);
}
}
Word.displayall ()
}
Checknum (STR1);
Console.log ("&&&&&&&&&&&&&&&&&&& & ");
Checknum (ARR3);

Two exercises in JS 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.