LOTTERY COMPONENT
HERE, LOTTERY COMPONENT IS THE LOGICAL COMPONENT
PROPS- n,winningSum
STATE - ticket[]
EVENTS - buyTicket()
APP.JSX
import Lottery from "./Lottery";
function App() {
return (
<>
<Lottery n={4} winningSum={20}/>
</>
LOTTERY.JSX
import { useState } from "react";
import { genTicket, sum } from "./helper";
import Ticket from "./Ticket";
export default function Lottery({ n=3, winningSum=15 }) {
let [ticket, setTicket] = useState(genTicket(n));
let isWinning = sum(ticket) === winningSum;
let buyTicket = () => {
setTicket(genTicket(n));
};
return (
<>
<h1>Lottery Game!</h1>
<h5>
<Ticket ticket={ticket} />
</h5>
{isWinning ? <h3>Congratulations!! you won!</h3> : ""}
<button onClick={buyTicket}>Buy new Ticket</button>
</>
);
}

Comments
Post a Comment