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