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..6971fa3 100644 --- a/exercises.js +++ b/exercises.js @@ -1,4 +1,52 @@ +const cellphone = { + phoneNumber: '7462826352', + addContact(name, phoneNumber) { + cellphone.contacts.push({ name, phoneNumber }); + return `${name} successfully added`; + }, + 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; + // this.contacts.filter( (data) => { return data != undefined || null || NaN || "" }); + // Above line is to remove empty objects within array. + 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.'} + } + }, + 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'; +cellphone.contacts = []; +cellphone.contacts.push({name:'Peter',phoneNumber:'3474236107'}); +cellphone.addContact('Pete', '1234567890'); +cellphone.addContact('Petra', '1234567890'); +cellphone.addContact('Pelican', '1234567890'); module.exports = { cellphone,