From 839ffc73509277c51a7e8651a7522365861b7142 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Tue, 3 Dec 2019 14:38:24 +0000 Subject: [PATCH 1/4] few problems completed --- README.md | 12 ++++++++++++ exercises.js | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 19c00c0..18c9e86 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,24 @@ Answer short response questions directly in this markdown file. Answer questions **1. What is encapsulation? Why do we use it? How do objects enable it?** + Encapsulation is the concept of collecting information and creating object methods that have restrictive access to said data. + A good example of this is the Math object. Math is an object with data that is held privately and cannot be accessed unless you use + one of Math's object methods, like Math.Sqrt or Math.random. These methods execute code that we cannot see from the outside but have access to + solely because we used the proper method to access it. + **2. How do objects encapsulate state? How do they encapsulate behavior?** + + **3. How do we create _private data_ within JavaScript objects? What is the role of _accessor properties_?** **4. What are factory functions and why are they useful?** + A factory function is essentially a function that returns a new object. + (incomplete). + +Factory functions have always been attractive in JavaScript because they + ### Coding Practice Answer the questions in this section in the `exercises.js` file in this directory. Test are included. Install with `npm install`. Test with `npm test`. Lint with `npx eslint exercises.js` diff --git a/exercises.js b/exercises.js index 8b29f5d..f2d7825 100644 --- a/exercises.js +++ b/exercises.js @@ -1,4 +1,20 @@ +const cellphone = { + phoneNumber: '7462826352', + addContact(name, phoneNumber) { + cellphone.contacts.push({ name, phoneNumber }); + return `${name} successfully added`; + }, + numberOfContacts() { + return Object.keys(cellphone.contacts).length; + } +}; +cellphone.model = 'iPhone X iOS 13.2.2 #Checkra1n'; +cellphone.contacts = []; +cellphone.contacts.push({name:'Peter',phoneNumber:'3474236107'}); +cellphone.addContact('Pete', '1234567890'); +cellphone.addContact('Petra', '1234567890'); +cellphone.addContact('Pelican', '1234567890'); module.exports = { cellphone, From 0edd6e22319f74abadd6443cbed4ee6f4bf8d05b Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Tue, 3 Dec 2019 14:40:00 +0000 Subject: [PATCH 2/4] adjusted numbersOfContacts --- exercises.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises.js b/exercises.js index f2d7825..4a14c92 100644 --- a/exercises.js +++ b/exercises.js @@ -5,8 +5,8 @@ const cellphone = { return `${name} successfully added`; }, numberOfContacts() { - return Object.keys(cellphone.contacts).length; - } + return this.contacts.length; + }, }; cellphone.model = 'iPhone X iOS 13.2.2 #Checkra1n'; From e7d5a5d186fece3407cc368f332c2643145459a0 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Tue, 3 Dec 2019 14:57:34 +0000 Subject: [PATCH 3/4] call method complete --- exercises.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/exercises.js b/exercises.js index 4a14c92..2fc58bc 100644 --- a/exercises.js +++ b/exercises.js @@ -7,6 +7,30 @@ const cellphone = { numberOfContacts() { return this.contacts.length; }, + lookUp(str) { + for(key of this.contacts) { + if(key.name == str) { + return key.phoneNumber; + } + } + return "Contact not found." + }, + deleteContact(str) { + for(key of this.contacts) { + if(key.name == str) { + delete key.name; + delete key.phoneNumber; + return `${str} successfully deleted.` + } + } + return 'Contact not found.' + }, + call(str) { + for(key of this.contacts) { + if(key.name == str || key.phoneNumber == str) return `Calling ${name} at ${phoneNumber}` + else { return 'Contact not found.'} + } + }, }; cellphone.model = 'iPhone X iOS 13.2.2 #Checkra1n'; From dd18f85be1e5127ea1c318c7af0eb9cc8797dc73 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Tue, 3 Dec 2019 15:30:31 +0000 Subject: [PATCH 4/4] Finalizing, unfinished --- exercises.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/exercises.js b/exercises.js index 2fc58bc..6971fa3 100644 --- a/exercises.js +++ b/exercises.js @@ -20,6 +20,8 @@ const cellphone = { if(key.name == str) { delete key.name; delete key.phoneNumber; + // this.contacts.filter( (data) => { return data != undefined || null || NaN || "" }); + // Above line is to remove empty objects within array. return `${str} successfully deleted.` } } @@ -31,6 +33,12 @@ const cellphone = { else { return 'Contact not found.'} } }, + makeCellPhone(phoneNumber, model) { + return {}; + This function should return an object with all of the properties and methods that we defined above. + This function should take two string arguments such that + the resulting cellphone object should be initialized with a phoneNumber and a model. + } }; cellphone.model = 'iPhone X iOS 13.2.2 #Checkra1n';