import { useState } from 'react'; function Square({ value, onSquareClick }) { return (…
- User
import { useState } from 'react';
function Square({ value, onSquareClick }) { return ( {value} ); }
function Board({ xIsNext, squares, onPlay }) { function handleClick(i) { if (calculateWinner(squares) || squares[i]) { return; } const nextSquares = squares.slice(); if (xIsNext) { nextSquares[i] = 'X'; } else { nextSquares[i] = 'O'; } onPlay(nextSquares); }
const winner = calculateWinner(squares); let status; if (winner) { status = 'Winner: ' + winner; } else { status = 'Next player: ' + (xIsNext ? 'X' : 'O'); }
return ( <> {status} <Square value={squares[0]} onSquareClick={() => handleClick(0)} /> <Square value={squares[1]} onSquareClick={() => handleClick(1)} /> <Square value={squares[2]} onSquareClick={() => handleClick(2)} /> <Square value={squares[3]} onSquareClick={() => handleClick(3)} /> <Square value={squares[4]} onSquareClick={() => handleClick(4)} /> <Square value={squares[5]} onSquareClick={() => handleClick(5)} /> <Square value={squares[6]} onSquareClick={() => handleClick(6)} /> <Square value={squares[7]} onSquareClick={() => handleClick(7)} /> <Square value={squares[8]} onSquareClick={() => handleClick(8)} /> </> ); }
export default function Game() { const [history, setHistory] = useState([Array(9).fill(null)]); const [currentMove, setCurrentMove] = useState(0); const xIsNext = currentMove % 2 === 0; const currentSquares = history[currentMove];
function handlePlay(nextSquares) { const nextHistory = [...history.slice(0, currentMove + 1), nextSquares]; setHistory(nextHistory); setCurrentMove(nextHistory.length - 1); }
function jumpTo(nextMove) { setCurrentMove(nextMove); }
const moves = history.map((squares, move) => { let description; if (move > 0) { description = 'Go to move #' + move; } else { description = 'Go to game start'; } return ( <button onClick={() => jumpTo(move)}>{description} ); });
return ( {moves} ); }
function calculateWinner(squares) { const lines = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6], ]; for (let i = 0; i < lines.length; i++) { const [a, b, c] = lines[i]; if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) { return squares[a]; } } return null; } is this type script or javascript
- Assistant
The code you've provided is written in JavaScript, specifically JSX syntax, which is commonly used in React applications. JSX allows you to write HTML-like code within JavaScript, making it easier to create UI components.
This code does not include any TypeScript specific syntax like type annotations or interfaces; it's pure JavaScript with React. If it were TypeScript, you would typically see type annotations defining the types of props, state, and other variables, which are absent in the provided code.
- User
.map in react jsx
- Assistant
In React, the
.map()function is a common pattern used to render lists of elements dynamically based on an array of data. JSX allows you to embed expressions like directly in your component's rendering logic. Here's a general example of how to use in React with JSX:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.