diff --git a/debug.js b/debug.js index e69de29..ebf23d8 100644 --- a/debug.js +++ b/debug.js @@ -0,0 +1,2 @@ +//[ 4, 1, 4, 5, 3 ] [ 8, 8, 10, 9, 12 ] 33 +console.log(4); diff --git a/hackerrank/contacts.ts b/hackerrank/contacts.ts new file mode 100644 index 0000000..37fbedd --- /dev/null +++ b/hackerrank/contacts.ts @@ -0,0 +1,41 @@ +// wrong solution +function countContacts(queries: string[][]): number[] { + let contacts: string[] = [], + result: number[] = []; + + if (queries.length < 2) { + return [0]; + } + + for (let arr of queries) { + let action = arr[0], + value = arr[1]; + + switch (action) { + case "add": { + contacts.push(value); + break; + } + case "find": { + const filtered = contacts.filter((contact) => + contact.startsWith(value) + ); + result.push(filtered.length); + break; + } + default: { + break; + } + } + } + + return result; +} + +const input = [ + ["add", "hack"], + ["add", "hackerrank"], + ["find", "hac"], + ["find", "hak"], +]; +console.log(countContacts(input));