From 2b7bc3111654b9b1db25bb3cc75598281ab94115 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Tue, 3 Dec 2019 15:30:07 +0000 Subject: [PATCH 1/2] 1-9 completed --- exercises.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/exercises.js b/exercises.js index 8b29f5d..9c2679a 100644 --- a/exercises.js +++ b/exercises.js @@ -1,3 +1,41 @@ +const cellphone = { //1 + phoneNumber: '3479224333', + contacts: [{name: 'test', number: '1234567890'}], + addContact (name, number){ //5 + this.contacts.push({name, number}); + return `${name} successfully added.`; + }, + numberOfContacts () { //7 + return this.contacts.length; + }, + lookUp (name) { //8 + for(let person in this.contacts){ + if (this.contacts[person].name === name)return this.contacts[person].number; + } + return 'Contact not found'; + }, + deleteContact (name) { //9 + for(let person in this.contacts){ + if (this.contacts[person].name === name){ + delete this.contacts[person]; + return `${name} sucessfully deleted`; + } + return 'Contact not found'; + } + }, + call () { + + } +} + +cellphone.model = 'Samsung Galaxy Note'; //2 +//cellphone.contacts = []; //3 +cellphone.contacts.push({name: "Devonte", phoneNumber: '3479224313'}); //4 +//6 +cellphone.addContact('Cielo', '1234567890'); +cellphone.addContact('Mavis', '0987654321'); +cellphone.addContact('Cobalt', '3471234567'); + module.exports = { From b311cf01a2550cb28aa593c00d1d295a407136b9 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Wed, 4 Dec 2019 07:00:37 +0000 Subject: [PATCH 2/2] Completed, linted, and test revised --- exercises.js | 115 ++++++++++++++++++++++++++++++++-------------- exercises.test.js | 4 +- 2 files changed, 82 insertions(+), 37 deletions(-) diff --git a/exercises.js b/exercises.js index 9c2679a..17b84b2 100644 --- a/exercises.js +++ b/exercises.js @@ -1,44 +1,89 @@ -const cellphone = { //1 +// Question 1 +const cellphone = { phoneNumber: '3479224333', - contacts: [{name: 'test', number: '1234567890'}], - addContact (name, number){ //5 - this.contacts.push({name, number}); - return `${name} successfully added.`; - }, - numberOfContacts () { //7 - return this.contacts.length; - }, - lookUp (name) { //8 - for(let person in this.contacts){ - if (this.contacts[person].name === name)return this.contacts[person].number; - } - return 'Contact not found'; - }, - deleteContact (name) { //9 - for(let person in this.contacts){ - if (this.contacts[person].name === name){ - delete this.contacts[person]; - return `${name} sucessfully deleted`; - } - return 'Contact not found'; - } - }, - call () { - - } -} - -cellphone.model = 'Samsung Galaxy Note'; //2 -//cellphone.contacts = []; //3 -cellphone.contacts.push({name: "Devonte", phoneNumber: '3479224313'}); //4 -//6 -cellphone.addContact('Cielo', '1234567890'); +}; + +// Question 2 +cellphone.model = 'Samsung Galaxy Note'; + +// Question 3 +cellphone.contacts = []; + +// Question 4 +cellphone.contacts.push({ name: 'Devonte', phoneNumber: '3479224313' }); + +// Question 5 +cellphone.addContact = function(name, phoneNumber) { + this.contacts.push({ name, phoneNumber }); + return `${name} successfully added.`; +}; + +// Question 6 +cellphone.addContact('Cielo', '1234567890'); cellphone.addContact('Mavis', '0987654321'); cellphone.addContact('Cobalt', '3471234567'); +// Question 7 +cellphone.numberOfContacts = function() { + return this.contacts.length; +}; + +// Question 8 +cellphone.lookUp = function(person) { + const result = this.contacts.find((contact) => contact.name === person || contact.phoneNumber === person); + return result === undefined ? 'Contact not found.' : result.phoneNumber; +}; + +cellphone.addContact('Reuben Ogbonna', '9196219388'); +console.log(cellphone.lookUp('Reuben Ogbonna')); + +// Question 9 +cellphone.deleteContact = function(person) { + const result = this.contacts.find((contact) => contact.name === person || contact.phoneNumber === person); + const deleteIndex = this.contacts.indexOf(result); + this.contacts.splice(deleteIndex, 1); + return result === undefined ? 'Contact not found.' : `${person} successfully deleted.`; +}; + + +// Question 10 +cellphone.call = function(person) { + const result = this.contacts.find((contact) => contact.name === person || contact.phoneNumber === person); + return result === undefined ? 'Contact not found.' : `Calling ${result.name} at ${result.phoneNumber}`; +}; + +// Question 11 +const makeCellPhone = function (phoneNumber, model) { + return { + phoneNumber, + model, + + addContact(name, phoneNumber) { + this.contacts.push({ name, phoneNumber }); + return `${name} successfully added.`; + }, + numberOfContacts() { + return this.contacts.length; + }, + lookUp(person) { + const result = this.contacts.find((contact) => contact.name === person || contact.phoneNumber === person); + return result === undefined ? 'Contact not found.' : result.phoneNumber; + }, + deleteContact(person) { + const result = this.contacts.find((contact) => contact.name === person || contact.phoneNumber === person); + const deleteIndex = this.contacts.indexOf(result); + this.contacts.splice(deleteIndex, 1); + return result === undefined ? 'Contact not found' : `${person} has been successfully deleted`; + }, + call(person) { + const result = this.contacts.find((contact) => contact.name === person || contact.phoneNumber === person); + return result === undefined ? 'Contact not found' : `Calling ${result.name} at ${result.phoneNumber}`; + }, + }; +}; module.exports = { cellphone, - // makeCellPhone, + makeCellPhone, }; diff --git a/exercises.test.js b/exercises.test.js index a9960f7..84f5595 100644 --- a/exercises.test.js +++ b/exercises.test.js @@ -1,4 +1,4 @@ -const { cellphone, makeCellPhone } = require(./exercises); +const { cellphone, makeCellPhone } = require('./exercises'); test('has phoneNumber property of 10 digits', () => { expect(cellphone.phoneNumber).toMatch(/\d{10}/); @@ -37,7 +37,7 @@ test('lookUp works', () => { test('deleteContact works', () => { cellphone.contacts.push({name: 'Jim Smith', phoneNumber: '9876543210'}); expect(cellphone.deleteContact('Jim Smith')).toBe('Jim Smith successfully deleted.'); - expect(cellphone.contacts.none((contact) => contact.name === 'Jim Smith')).toBe(true); + expect(cellphone.contacts.some((contact) => contact.name === 'Jim Smith')).toBe(false); expect expect(cellphone.deleteContact(Math.random().toString())).toBe('Contact not found.'); });