Created
October 31, 2016 14:09
-
-
Save fforres/f08e21a4e2c26e73d77bfb9daf4c7e65 to your computer and use it in GitHub Desktop.
hackerrank ctcy contacts - object solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://www.hackerrank.com/challenges/ctci-contacts | |
/////////////// ignore above this line //////////////////// | |
function analizeContacts(){ | |
this.contactObject = {}; | |
this.currentFinds = []; | |
} | |
analizeContacts.prototype.find = function(term){ | |
let currentWords = null; | |
if(this.contactObject[term]) { | |
currentWords = this.contactObject[term]; | |
} else { | |
currentWords = 0; | |
} | |
this.currentFinds.push(currentWords); | |
} | |
analizeContacts.prototype.add = function(term) { | |
for( let i = 1; i < term.length + 1; i++) { | |
const el = term.substring(0, i); | |
this.contactObject[el] = this.contactObject[el] ? this.contactObject[el] + 1 : 1 ; | |
} | |
} | |
analizeContacts.prototype.results = function() { | |
this.currentFinds.forEach((el) => { | |
console.log(el); | |
}); | |
} | |
function main() { | |
var n = parseInt(readLine()); | |
const analize = new analizeContacts(); | |
for(var a0 = 0; a0 < n; a0++){ | |
var op_temp = readLine().split(' '); | |
var op = op_temp[0]; | |
var contact = op_temp[1]; | |
analize[op](contact); | |
} | |
analize.results(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment