diff --git a/src/App.css b/src/App.css index 74b5e05..bd53550 100644 --- a/src/App.css +++ b/src/App.css @@ -1,5 +1,6 @@ .App { text-align: center; + } .App-logo { @@ -36,3 +37,37 @@ transform: rotate(360deg); } } +img{ + width: 200px; + height:300px +} +table{ + width: 1100px; + margin-inline: auto; + border-collapse: collapse; + margin-top: 20px; +} +th, td{ + font-size: 25px; + text-align: center; + vertical-align: bottom; + border-bottom: 1px solid #282c34; + padding:8px; +} +tr:nth-child(even){ +background-color: chocolate; +} +tr:hover{ + background-color:chartreuse; +} +button{ + background-color: #04AA6D; + font-size: 16px; + padding: 14px 40px; + border-radius: 8px; + transition-duration: 0.4s; + margin-right: 30px; +} +button:hover{ + color: white; +} \ No newline at end of file diff --git a/src/App.js b/src/App.js index 3784575..f237397 100644 --- a/src/App.js +++ b/src/App.js @@ -1,23 +1,100 @@ -import logo from './logo.svg'; +import React, { useState } from 'react'; import './App.css'; +import contacts from './contacts.json'; function App() { + const [contactsList, setContactsList] = useState(contacts.slice(0, 7)); + const remainingContacts = contacts.slice(5); + + //function to add a random contact + const addRandomContact = () => { + if (remainingContacts.length === 0) { + // Handling the case where there are no more remaining contacts + alert('No more remaining contacts'); + return; + } + + // Randomly selecting a contact from the remaining contacts + const randomIndex = Math.floor(Math.random() * remainingContacts.length); + const randomContact = remainingContacts[randomIndex]; + + // Adding the selected contact to the array in the state + setContactsList((prevContacts) => [...prevContacts, randomContact]); + + // Removing the selected contact from the remaining contacts + const updatedRemainingContacts = [ + ...remainingContacts.slice(0, randomIndex), + ...remainingContacts.slice(randomIndex + 1), + ]; + + // Updating the state of remaining contacts if needed + // setRemainingContacts(updatedRemainingContacts); + }; + //function to sort by name alphabetically + const sortByName = () => { + //create new array and updating state with sorted array + const sortedByName = [...contactsList].sort((a,b)=> a.name.localeCompare(b.name)); + setContactsList(sortedByName); + } + //fucntion to sort by popularity in descending order + const sortByPopularity = () => { + //create new array and updating state with sorted array + const sortedByPopularity = [...contactsList].sort((a,b)=> b.popularity - a.popularity); + setContactsList(sortedByPopularity); + } + + const removeContact = (id) =>{ + //finding the index of the contact with the given ID + const contactIndex =contactsList.findIndex((contact) => contact.id === id); + + if (contactIndex !== -1){ + //creating a new array exluding the contact to be removed + const updateContactsList = [ + ...contactsList.slice(0, contactIndex), + ...contactsList.slice(contactIndex + 1) + ]; + + //updating the stae variable with the modified new array of contacts list + setContactsList(updateContactsList) + } + + + } + return (
- Edit src/App.js and save to reload.
-
| Picture | +Name | +Popularity | +Won an Oscar | +Won an Emmy | +Action | +
|---|---|---|---|---|---|
|
+ |
+ {contact.name} | +{contact.popularity} | +{contact.wonOscar ? '🏆' : ''} | +{contact.wonEmmy ? '🏆' : ''} | ++ + | +