PRACTICASL QUESTIONS
PRACTICASL QUESTIONS
Q1-Implement a TO-DO list where tasks can be added, marked as complete, and removed using
React state.
CODE-
return (
<div className="p-4 max-w-md mx-auto bg-gray-100 shadow-md rounded-lg">
<h1 className="text-2xl font-bold mb-4">To-Do List</h1>
<div className="flex gap-2 mb-4">
<input
type="text"
value={task}
onChange={(e) => setTask(e.target.value)}
placeholder="Add a new task"
className="border p-2 flex-1 rounded"
/>
<button onClick={addTask} className="bg-blue-500 text-white px-4 py-2 rounded">
Add
</button>
</div>
<ul>
{tasks.map((t, index) => (
<li key={index} className="flex justify-between items-center p-2 bg-white mb-2 rounded
shadow">
<span
onClick={() => toggleComplete(index)}
className={`cursor-pointer ${t.completed ? "line-through text-gray-500" : ""}`}
>
{t.text}
</span>
<button onClick={() => removeTask(index)} className="text-red-500"> </button>
</li>
))}
</ul>
</div>
);
}
export default TodoApp;
OUTPUT-
Q2-Create a Counter component with buttons to increment and decrement a number. Pass initial
CODE-
function App() {
const [count, setCount] = useState(5); // You can set initial value here
return (
<div style={{
minHeight: '100vh',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f3f4f6'
}}>
<div style={{
backgroundColor: 'white',
padding: '2rem',
borderRadius: '1rem',
boxShadow: '0 4px 12px rgba(0,0,0,0.1)',
textAlign: 'center',
width: '250px'
}}>
<h2 style={{ marginBottom: '1rem' }}>Counter</h2>
<div style={{ fontSize: '2rem', fontWeight: 'bold', marginBottom: '1rem' }}>{count}</div>
<div style={{ display: 'flex', justifyContent: 'space-around' }}>
<button
onClick={decrement}
style={{
padding: '0.5rem 1rem',
backgroundColor: '#ef4444',
color: 'white',
border: 'none',
borderRadius: '0.5rem',
cursor: 'pointer'
}}
>
-
</button>
<button
onClick={increment}
style={{
padding: '0.5rem 1rem',
backgroundColor: '#10b981',
color: 'white',
border: 'none',
borderRadius: '0.5rem',
cursor: 'pointer'
}}
>
+
</button>
</div>
</div>
</div>
);
}
OUTPUT-