Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Create a component that renders a paragraph with a random color every time a button is clicked.

Easy

Problem Statement

Create a component that renders a paragraph with a random color every time a button is clicked.

Sample Output

The paragraph text color changes to a random color when the button is clicked.

Limits

Time: 2s · Memory: 256 MB

Code Editor

Try your code in this preview. Submit marks it done.

import React, { useState } from "react";
 
 const RandomColor = () => {
  const [color, setColor] = useState("black");
 
  const changeColor = () => {
  // Generate and set a random color
  };
 
  return (
  <div>
  <p style={{ color }}>This is random color text.</p>
  <button onClick={changeColor}>Change Color</button>
  </div>
  );
 };
 
 export default RandomColor;