Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
48 changes: 48 additions & 0 deletions exercises.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down