-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.js
More file actions
58 lines (46 loc) · 1.15 KB
/
Copy pathcontact.js
File metadata and controls
58 lines (46 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
// https://www.hackerrank.com/challenges/contacts/problem
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
/*
* Complete the contacts function below.
*/
function contacts(queries) {
const allContacts = new Map()
const results = []
function findContacts (val) {
let found = 0
for (let key of allContacts.keys()) {
if (key.indexOf(val) == 0 ) {
found += +1
}
}
results.push(found)
}
for (let x = 0; x < queries.length; x++) {
if (queries[x]) {
const command = queries[x][0]
const value = queries[x][1] || false
if (command === 'add') {
allContacts.set(value, '')
} else if (command === 'find') {
findContacts(value)
}
}
}
results.forEach((r, i) => {
console.log(`${i+1} - ${r}`)
})
return results
}
let reader = fs.createReadStream('input03.txt');
reader.on('data', function (chunk) {
inputString += chunk.toString()
});
reader.on("end", () => {
inputString = inputString.trim().split('\n').map(str => str.trim().split(" "))
inputString.shift()
contacts(inputString)
});