Created
April 10, 2019 08:13
-
-
Save ragasubekti/cbba9560a268fa0b149b15167ebc90bb to your computer and use it in GitHub Desktop.
Delete Contact Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Contact With Array | |
const myContacts = [ | |
["Upin", "087870505005", "upin@gmail.com"], | |
["Ipin", "087123456777", "ipin@gmail.com"], | |
["Ismail bin Mail", "087127493726", "mail@gmail.com"], | |
["Jarjit Singh", "087235789642", "jarjit@gmail.com"], | |
["Ehsan bin Azaruddin", "087367192345", "ehsan@gmail.com"], | |
["Mei Mei", "087367198256", "mei@gmail.com"] | |
]; | |
const showContacts = contacts => { | |
for (let i = 0; i < contacts.length; i++) { | |
console.log(`[${i + 1}] ${contacts[i].join(" | ")}`); | |
} | |
}; | |
const insertContacts = contact => { | |
myContacts.push(contact); | |
console.log(`\n Contact successfully added`); | |
showContacts(myContacts); | |
}; | |
const deleteContactsByNumber = numbContact => { | |
myContacts.splice(numbContact - 1, 1); | |
console.log(`\n Contact successfully deleted`); | |
showContacts(myContacts); | |
}; | |
const filterNameByLength = maxLength => { | |
let filterContacts = []; | |
for (let i = 0; i < myContacts.length; i++) { | |
if (myContacts[i][0].length < maxLength) { | |
filterContacts.push(myContacts[i]); | |
} | |
} | |
console.log(`\n Contact successfully filtered`); | |
showContacts(filterContacts); | |
}; | |
insertContacts([`Dicky Muhamad R`, "0234932042", "dickymr@gmail.com"]); | |
deleteContactsByNumber(5); | |
filterNameByLength(8); | |
// Contact With Object | |
console.log(`\n \n ============================================= \n \n`); | |
let myContactsObject = [ | |
{ | |
id: 1, | |
name: "Alpha Avalon", | |
phone: "+1 111 101010", | |
email: "alpha@avalon.org" | |
}, | |
{ | |
id: 2, | |
name: "Betty Brave", | |
phone: "+62 812 242424", | |
email: "betty@braverian.com" | |
}, | |
{ | |
id: 3, | |
name: "Gamma Gacurio", | |
phone: "+63 813 363636", | |
email: "gamma@gacurio.dev" | |
} | |
]; | |
const showContactsObject = contactObject => { | |
for (let i = 0; i < contactObject.length; i++) { | |
console.log( | |
`[${contactObject[i].id}] | ${contactObject[i].name} | ${ | |
contactObject[i].phone | |
} | ${contactObject[i].email}` | |
); | |
} | |
}; | |
const createContactObject = (id, name, phone, email) => { | |
let newContactObject = { | |
id: id, | |
name: `${name}`, | |
phone: `${phone}`, | |
email: `${email}` | |
}; | |
myContactsObject.push(newContactObject); | |
console.log( | |
`\n Contact object successfully added id: ${newContactObject.id}` | |
); | |
showContactsObject(myContactsObject); | |
}; | |
const deleteContactsObjectById = id => { | |
let isItemDeleted = false; | |
for (let i = 0; i < myContactsObject.length; i++) { | |
if (myContactsObject[i].id === id && !isItemDeleted) { | |
myContactsObject.splice(i, 1); | |
isItemDeleted = true; | |
console.log(`\n Contact object successfully remove id: ${id}`); | |
showContactsObject(myContactsObject); | |
} else if (i >= myContactsObject.length - 1 && !isItemDeleted) { | |
console.log(`\n Contact object failed to remove. id: ${id} Not found`); | |
} | |
// console.log(id, myContactsObject); | |
} | |
}; | |
const filterContactsObjectByNameLength = maxLength => { | |
let newContactObject = []; | |
for (let i = 0; i < myContactsObject.length; i++) { | |
if (myContactsObject[i].name.length <= maxLength) { | |
newContactObject.push(myContactsObject[i]); | |
} | |
} | |
console.log( | |
`\n Contact object successfully filtered max length name: ${maxLength}` | |
); | |
showContactsObject(newContactObject); | |
}; | |
showContactsObject(myContactsObject); | |
createContactObject(4, `Dicky`, `08787503921`, `dickymr@gmail.com`); | |
deleteContactsObjectById(2); | |
deleteContactsObjectById(7); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment