|
| 1 | +class Dictionary { |
| 2 | + constructor(dataStore = []) { |
| 3 | + this.dataStore = dataStore; |
| 4 | + } |
| 5 | + |
| 6 | + add(key, value) { |
| 7 | + this.dataStore[key] = value; |
| 8 | + } |
| 9 | + |
| 10 | + find(key) { |
| 11 | + return this.dataStore[key]; |
| 12 | + } |
| 13 | + |
| 14 | + remove(key) { |
| 15 | + delete this.dataStore[key]; |
| 16 | + } |
| 17 | + |
| 18 | + showOne() { |
| 19 | + if(this.count() > 0) { |
| 20 | + const key = Object.keys(this.dataStore)[0]; |
| 21 | + console.log(key, ' -> ', this.dataStore[key]); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + showAll() { |
| 26 | + // Display in ascending order |
| 27 | + Object.keys(this.dataStore).forEach(key => { |
| 28 | + console.log(key, ' -> ', this.dataStore[key]); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + showAllSorted() { |
| 33 | + // Display in ascending order |
| 34 | + Object.keys(this.dataStore).sort().forEach(key => { |
| 35 | + console.log(key, ' -> ', this.dataStore[key]); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + count() { |
| 40 | + // or this.dataStore.length since dataStore is an array, but this aproach is only for arrays |
| 41 | + // Object.keys(obj).length returns how many elements has the Dictionary |
| 42 | + return Object.keys(this.dataStore).length; |
| 43 | + } |
| 44 | + |
| 45 | + clear() { |
| 46 | + this.dataStore = []; |
| 47 | + } |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +// Implementation |
| 52 | +// ##################################### |
| 53 | +console.log('CHAPTER 7'); |
| 54 | +const pbook = new Dictionary(); |
| 55 | +pbook.add("Raymond","123"); |
| 56 | +pbook.add("David", "345"); |
| 57 | +pbook.add("Cynthia", "456"); |
| 58 | +pbook.add("Mike", "723"); |
| 59 | +pbook.add("Jennifer", "987"); |
| 60 | +pbook.add("Danny", "012"); |
| 61 | +pbook.add("Jonathan", "666"); |
| 62 | +console.log("Number of entries: " + pbook.count()); |
| 63 | +console.log("David's extension: " + pbook.find("David")); |
| 64 | +pbook.showAll(); |
| 65 | +console.log('\n Cleat Dictionary'); |
| 66 | +pbook.clear(); |
| 67 | +console.log("Number of entries: " + pbook.count()); |
| 68 | + |
| 69 | +// ################################################# |
| 70 | +/* |
| 71 | + 1. Write a program that takes a set of names and phone numbers from a text file |
| 72 | + and stores them in a Dictionary object. Include in your program the ability to |
| 73 | + display one phone number, display all phone numbers, add new phone numbers, |
| 74 | + remove phone numbers, and clear out the list of numbers. |
| 75 | +*/ |
| 76 | +console.log('\n\n### Exercise 1'); |
| 77 | +const readline = require('readline'); |
| 78 | +const fs = require('fs'); |
| 79 | + |
| 80 | +const dictionary = new Dictionary(); |
| 81 | + |
| 82 | +const rl = readline.createInterface({ |
| 83 | + input: fs.createReadStream('phoneDictionary.txt') |
| 84 | +}); |
| 85 | + |
| 86 | +rl.on('line', (line) => { |
| 87 | + const split = line.split(' '); |
| 88 | + dictionary.add(split[0], split[1]); |
| 89 | +}); |
| 90 | + |
| 91 | +rl.on('close', () => { |
| 92 | + console.log('\nDisplay 1 phone number'); |
| 93 | + dictionary.showOne(); |
| 94 | + |
| 95 | + console.log('\nDisplay All'); |
| 96 | + dictionary.showAll(); |
| 97 | + |
| 98 | + dictionary.add('Cristian', '111-111-111-111'); |
| 99 | + dictionary.add('Kalesei', '222-111-222-111'); |
| 100 | + dictionary.add('Kal', '111-333-111-333'); |
| 101 | + console.log('\nDisplay All'); |
| 102 | + dictionary.showAll(); |
| 103 | + |
| 104 | + console.log('\nRemoving elements'); |
| 105 | + dictionary.remove('Mike'); |
| 106 | + dictionary.remove('Raymond'); |
| 107 | + dictionary.remove('Kal'); |
| 108 | + |
| 109 | + console.log('\nDisplay All'); |
| 110 | + dictionary.showAll(); |
| 111 | + console.log(dictionary.find('Jane')); |
| 112 | + |
| 113 | + /* |
| 114 | + 2. Using the Dictionary class, write a program that stores the number of occurrences |
| 115 | + of words in a text. Your program should display each word in a text just once as well |
| 116 | + as the number of times the word occurs in the text. |
| 117 | + */ |
| 118 | + console.log('\n\n### Exercise 2'); |
| 119 | + const d = new Dictionary(); |
| 120 | + let string = 'the brown fox jumped over the blue fox'; |
| 121 | + |
| 122 | + string = string.split(' '); |
| 123 | + string.forEach(item => { |
| 124 | + if(item !== ' ') { |
| 125 | + let w = d.find(item); |
| 126 | + if(w !== undefined) { |
| 127 | + const value = w + 1; |
| 128 | + d.remove(item); |
| 129 | + d.add(item, value); |
| 130 | + } else { |
| 131 | + d.add(item, 1); |
| 132 | + } |
| 133 | + } |
| 134 | + }); |
| 135 | + console.log('showing unsorted'); |
| 136 | + d.showAll(); |
| 137 | + |
| 138 | + /* |
| 139 | + 3. Rewrite exercise 2 so that it displays the words in sorted order. |
| 140 | + */ |
| 141 | + console.log('\n\n### Exercise 3'); |
| 142 | + console.log('showing sorted'); |
| 143 | + d.showAllSorted(); |
| 144 | +}); |
0 commit comments