diff --git a/src/App.js b/src/App.js index 3784575..0188f40 100644 --- a/src/App.js +++ b/src/App.js @@ -1,23 +1,71 @@ -import logo from './logo.svg'; -import './App.css'; +import React, { useState } from "react"; +import "./App.css"; +import contactsData from "./contacts.json"; function App() { + // Step 1: Initialize contacts with the first 5 contacts from contacts.json + const [contacts, setContacts] = useState(contactsData.slice(0, 5)); + + // Step 3: Function to add a random contact from the remaining contacts + const addRandomContact = () => { + const remainingContacts = contactsData.filter( + (contact) => !contacts.includes(contact) + ); + const randomContact = + remainingContacts[Math.floor(Math.random() * remainingContacts.length)]; + setContacts([...contacts, randomContact]); + }; + + // Step 4: Function to sort contacts by name (alphabetically) + const sortByName = () => { + setContacts([...contacts].sort((a, b) => a.name.localeCompare(b.name))); + }; + + // Step 4: Function to sort contacts by popularity (highest first) + const sortByPopularity = () => { + setContacts([...contacts].sort((a, b) => b.popularity - a.popularity)); + }; + + // Step 5: Function to remove a contact by id + const removeContact = (id) => { + setContacts(contacts.filter((contact) => contact.id !== id)); + }; + 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 ? "🏆" : ""} | ++ + | +