Guest User

test

a guest
Sep 7th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. const Contacts = (function() {
  2. function Contacts(){};
  3. var arr = [];
  4.  
  5. // add a contact to the contacts list
  6. Contacts.prototype.add = function(contact) {
  7. arr.push(contact);
  8. console.log('added!');
  9. };
  10.  
  11. //returns the number of contacts that start with the term
  12. Contacts.prototype.count = function(term) {
  13. var cContacts = 0;
  14. for(var i=0; i<arr.length; i++){
  15. if(arr[i].indexOf(term) === 0){
  16. cContacts++;
  17. }
  18. }
  19. // arr.reduce((currctValue, contact) => contact.indexOf(term) === 0 ? currctValue + 1 : currctValue, 0)
  20. return cContacts;
  21. };
  22. return Contacts;
  23. })();
  24.  
  25.  
  26. const contacts = new Contacts();
  27. contacts.add('tsachi');
  28. console.log(contacts.count('tsachi'));
Advertisement
Add Comment
Please, Sign In to add comment