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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
Answer short response questions directly in this markdown file. Answer questions in 2 - 3 sentences. Be sure to format your responses appropriately.

**1. What is encapsulation? Why do we use it? How do objects enable it?**

- Encapsulation is making sure a piece of a program is kept private, meaning it should not be touched by anyone, versus making it public so that others can interact with this piece of code. This can be useful if you want the users to interact with this part of the code in a specific way, and make sure they input the right things to ensure an accurate computation of the data. Object methods like get and set, as well as an underscore, can influence the way we read these objects, and how to go about interacting with this data.
**2. How do objects encapsulate state? How do they encapsulate behavior?**

- If you want the data to be in a specific state, the object methods can specify this. By making strict data comparisons and guiding the data to a specific state, you can avoid results that will show errors or come out inaccurately. As with behavior, or methods, they make things strict by calling on methods that specify whether data is private or public, which can influence its access.
**3. How do we create _private data_ within JavaScript objects? What is the role of _accessor properties_?**

- To create private data, simply add an underscore in front of a key or method. Accessor proprties specify to the user whether the data is being set, or being gotten, which can influence how the data is used or manipulated.
**4. What are factory functions and why are they useful?**

- They add more flexibility to objects by allowing you to create as many objects as you want with a few parameters, much like creating a factory. It can save time and make it easy to create objects.
### 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 All @@ -30,17 +30,17 @@ Answer the questions in this section in the `exercises.js` file in this director

7. Add a method to your object called `numberOfContacts`. It should return the total number of `contacts`. Test your method by calling it on your object. It should return `4` if you've been following along correctly 😉.

9. Add a method called `lookUp` which takes a string argument that represents the name of a contact. This method should return the `phoneNumber` of the contact with the given `name`. If no such contact is present, it should return `'Contact not found.'`
8. Add a method called `lookUp` which takes a string argument that represents the name of a contact. This method should return the `phoneNumber` of the contact with the given `name`. If no such contact is present, it should return `'Contact not found.'`

8. Add a method called `deleteContact`. It should take a string argument that represents a name. It should delete the contact with the matching `name` and return `<<name>> successfully deleted.`. If no matching `name` is found, it should return `Contact not found.`
9. Add a method called `deleteContact`. It should take a string argument that represents a name. It should delete the contact with the matching `name` and return `<<name>> successfully deleted.`. If no matching `name` is found, it should return `Contact not found.`

9. Add a method called `call` which takes a string argument and returns `"Calling <<name>> at <<phoneNumber>>..."` if the contact is found. **Note: This string argument can be a name OR a phone number.** If there is no contact with a `name` or `phoneNumber` that matches the given argument, it should return `'Contact not found'`.
10. Add a method called `call` which takes a string argument and returns `"Calling <<name>> at <<phoneNumber>>..."` if the contact is found. **Note: This string argument can be a name OR a phone number.** If there is no contact with a `name` or `phoneNumber` that matches the given argument, it should return `'Contact not found'`.
```javascript
cellphone.call('Reuben Ogbonna'); // 'Calling Reuben Ogbonna at 9196219388' (if contact is found)
cellphone.call('9196219388'); // 'Calling Reuben Ogbonna at 9196219388' (if contact is found)

cellphone.call('lakjsdfasdl'); // 'Contact not found.'
```

10. Lastly, create a _factory function_ called `makeCellPhone`. 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`.
11. Lastly, create a _factory function_ called `makeCellPhone`. 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`.

62 changes: 61 additions & 1 deletion exercises.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,66 @@
const cellphone = {
phoneNumber: '1234567890',
model: 'iPhone',
contacts: [],
addContact(name, phoneNumber) {
this.contacts.push({name, phoneNumber});
return `${name} has been successfully added.`;
},
numberOfContacts() {
return this.contacts.length;
},
lookUp(name) {
let result = this.contacts.find(contact => contact.name === name);
return result ? result.phoneNumber : 'Contact not found.';
},
deleteContact(name) {
let result = this.contacts.find(contact => contact.name === name);
if (!result) return 'Contact not found.';
let deleteIndex = this.contacts.indexOf(result);
this.contacts.splice(deleteIndex, 1);
return `${name} successfully deleted.`;
},
call(str) {
let result = this.contacts.find(contact => {
return contact.name === str || contact.phoneNumber === str;
});
return result ? `Calling ${result.name} at ${result.phoneNumber}...` : 'Contact not found.';
}
};

const makeCellPhone = (model, phoneNumber) => {
return {
phoneNumber: phoneNumber,
model: model,
contacts: [],
addContact(name, phoneNumber) {
this.contacts.push({name, phoneNumber});
return `${name} has been successfully added.`;
},
numberOfContacts() {
return this.contacts.length;
},
lookUp(name) {
let result = this.contacts.find(contact => contact.name === name);
return result ? result.phoneNumber : 'Contact not found.';
},
deleteContact(name) {
let result = this.contacts.find(contact => contact.name === name);
if (!result) return 'Contact not found.';
let deleteIndex = this.contacts.indexOf(result);
this.contacts.splice(deleteIndex, 1);
return `${name} successfully deleted.`;
},
call(str) {
let result = this.contacts.find(contact => {
return contact.name === str || contact.phoneNumber === str;
});
return result ? `Calling ${result.name} at ${result.phoneNumber}...` : 'Contact not found.';
}
};
};

module.exports = {
cellphone,
// makeCellPhone,
makeCellPhone,
};