From c55ced057cce6e819c7d12782f61b7fbee621463 Mon Sep 17 00:00:00 2001 From: Elsa2116 Date: Sun, 16 Mar 2025 09:14:04 -0400 Subject: [PATCH] done --- src/App.js | 80 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 16 deletions(-) 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 (
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
+

Contact Management

+ + + + + + + + + + + + + + + + + {contacts.map((contact) => ( + + + + + + + + + ))} + +
PictureNamePopularityWon an OscarWon an EmmyAction
+ {contact.name} + {contact.name}{contact.popularity}{contact.wonOscar ? "🏆" : ""}{contact.wonEmmy ? "🏆" : ""} + +
); }