Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Contacts = (function() {
- function Contacts(){};
- var arr = [];
- // add a contact to the contacts list
- Contacts.prototype.add = function(contact) {
- arr.push(contact);
- console.log('added!');
- };
- //returns the number of contacts that start with the term
- Contacts.prototype.count = function(term) {
- var cContacts = 0;
- for(var i=0; i<arr.length; i++){
- if(arr[i].indexOf(term) === 0){
- cContacts++;
- }
- }
- // arr.reduce((currctValue, contact) => contact.indexOf(term) === 0 ? currctValue + 1 : currctValue, 0)
- return cContacts;
- };
- return Contacts;
- })();
- const contacts = new Contacts();
- contacts.add('tsachi');
- console.log(contacts.count('tsachi'));
Advertisement
Add Comment
Please, Sign In to add comment