React CoderByte Quetions
React CoderByte Quetions
--------------------------------------------------------------
import React, { useState } from "react"
import { createRoot } from "react-dom/client"
return (
<button type="button" onClick={handleClick}>
{toggle ? "ON" : "OFF"}
</button>
)
}
function ColorSelector() {
const colors = { red: "Red", blue: "Blue", green: "Green" }
const [color, setColor] = useState([Link])
const onChange = e => setColor([Link])
return (
<>
<select onChange={onChange} value={color}>
<option value={[Link]}>{[Link]}</option>
<option value={[Link]}>{[Link]}</option>
<option value={[Link]}>{[Link]}</option>
</select>
{color && <p>{`You have selected: ${color}`}</p>}
</>
)
}
const style = {
container: {
padding: "20px",
border: "1px solid #E0E0E0",
borderRadius: "15px",
width: "max-content",
marginBottom: "40px",
},
question: {
fontWeight: "bold",
marginBottom: "10px",
},
options: {
marginBottom: "5px",
},
button: {
marginTop: "10px",
padding: "10px 15px",
border: "none",
backgroundColor: "#007BFF",
color: "#FFF",
fontSize: "14px",
borderRadius: "5px",
cursor: "pointer",
},
feedback: {
marginTop: "10px",
fontSize: "14px",
},
}
function QuizApp() {
// do not modify the questions or answers below
const questions = useMemo(
() => [
{
question: "What is the capital of France?",
options: ["London", "Paris", "Berlin", "Madrid"],
correct: "Paris",
},
{
question: "What is the capital of Germany?",
options: ["Berlin", "Munich", "Frankfurt", "Hamburg"],
correct: "Berlin",
},
],
[]
)
return (
<div style={[Link]}>
<div id="question" style={[Link]}>
{`${questions[questionsIndex].question}`}
</div>
<div style={[Link]}>
{questions[questionsIndex].[Link]((option, index) => (
<QuizOption
key={`option-${index}`}
option={option}
index={index}
answer={answer}
setAnswer={setAnswer}
/>
))}
</div>
<button
disabled={completedQuiz}
style={[Link]}
id="submitBtn"
onClick={submit}
>
Submit
</button>
<div id="feedback" style={[Link]}>
{questionsIndex !== 0 && !completedQuiz && `${feedback}`}
</div>
<div id="score" style={[Link]}>
{completedQuiz &&
`Quiz complete! You scored ${score} out of
${[Link]}!`}
</div>
</div>
)
}
const style = {
marginTop: {
marginTop: "10px",
},
marginRight: {
marginRight: "10px",
},
}
const defaultWeather = {
temperature: "",
humidity: "",
windSpeed: "",
}
if (cache[city]) {
setWeather(cache[city])
return
}
try {
const data = await mockFetchWeatherData(city)
setCache({ ...cache, [city]: data })
setWeather(data)
setPreviousSearches([...previousSearches, city])
} catch {
throw new Error("Could not fetch weather data.")
}
}
return (
<div>
<input
type="text"
id="citySearch"
placeholder="Search for a city..."
value={city}
onChange={e => setCity([Link])}
/>
<button id="searchButton" onClick={() => search(city)}>
Search
</button>
<div id="weatherData" style={[Link]}>
<div>Temperature: {[Link]}</div>
<div>Humidity: {[Link]}</div>
<div>Wind Speed: {[Link]}</div>
{notFound && <div style={[Link]}>City not found.</div>}
</div>
<div id="previousSearches" style={[Link]}>
{[Link] > 0 &&
[Link]((previousSearch, index) => (
<button
key={index}
onClick={() => {
setCity(previousSearch)
search(previousSearch)
}}
style={[Link]}
>
{previousSearch}
</button>
))}
</div>
</div>
)
}
const style = {
letterContainer: {
overflow: "auto",
marginBottom: "10px",
display: "flex",
flexWrap: "wrap",
justifyContent: "center",
alignItems: "center",
},
letter: {
float: "left",
padding: "10px 10px",
background: "#c9e4ed",
borderRadius: "5px",
marginRight: "5px",
marginTop: "5px",
cursor: "pointer",
},
outputString: {
marginTop: "20px",
textAlign: "center",
},
}
return (
<button type="button" onClick={onClick} style={[Link]}>
{letter}
</button>
)
}
return (
<View style={[Link]}>
<Text style={[Link]}>
button count: <span id="actualCount">{count}</span>
</Text>
<button id="mainButton" onClick={increment}>
Increase
</button>
</View>
)
}
const rowStyle = {
display: "flex",
}
const squareStyle = {
width: "60px",
height: "60px",
backgroundColor: "#ddd",
margin: "4px",
display: "flex",
justifyContent: "center",
alignItems: "center",
fontSize: "20px",
color: "white",
}
const disabledSquareStyle = {
...squareStyle,
cursor: "not-allowed",
}
const boardStyle = {
backgroundColor: "#eee",
width: "208px",
alignItems: "center",
justifyContent: "center",
display: "flex",
flexDirection: "column",
border: "3px #eee solid",
}
const containerStyle = {
display: "flex",
alignItems: "center",
flexDirection: "column",
}
const instructionsStyle = {
marginTop: "5px",
marginBottom: "5px",
fontWeight: "bold",
fontSize: "16px",
}
const buttonStyle = {
marginTop: "15px",
marginBottom: "16px",
width: "80px",
height: "40px",
backgroundColor: "#8acaca",
color: "white",
fontSize: "16px",
}
const matches = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
]
const checkWinner = newBoard => {
for (let i = 0; i < [Link]; i++) {
const [a, b, c] = matches[i]
if (
newBoard[a] === player &&
newBoard[b] === player &&
newBoard[c] === player
) {
return true
}
}
return false
}
if (checkWinner(newBoard)) {
setWinner(player)
} else {
setPlayer(player === "X" ? "O" : "X")
}
}
return (
<div style={containerStyle} className="gameBoard">
<div id="statusArea" className="status" style={instructionsStyle}>
Next player: <span>{player === "X" ? "O" : "X"}</span>
</div>
<div id="winnerArea" className="winner" style={instructionsStyle}>
Winner: <span>{winner !== "None" && winner}</span>
</div>
<button style={buttonStyle} onClick={onReset}>
Reset
</button>
<div style={boardStyle}>
<div className="board-row" style={rowStyle}>
<Square
value={board[0]}
onClick={() => onClick(0)}
winner={winner}
/>
<Square
value={board[1]}
onClick={() => onClick(1)}
winner={winner}
/>
<Square
value={board[2]}
onClick={() => onClick(2)}
winner={winner}
/>
</div>
<div className="board-row" style={rowStyle}>
<Square
value={board[3]}
onClick={() => onClick(3)}
winner={winner}
/>
<Square
value={board[4]}
onClick={() => onClick(4)}
winner={winner}
/>
<Square
value={board[5]}
onClick={() => onClick(5)}
winner={winner}
/>
</div>
<div className="board-row" style={rowStyle}>
<Square
value={board[6]}
onClick={() => onClick(6)}
winner={winner}
/>
<Square
value={board[7]}
onClick={() => onClick(7)}
winner={winner}
/>
<Square
value={board[8]}
onClick={() => onClick(8)}
winner={winner}
/>
</div>
</div>
</div>
)
}
const Game = () => {
return (
<div className="game">
<div className="game-board">
<Board />
</div>
</div>
)
}
return (
<div id="mainArea">
<p>
{`Button Count: `}
<span>{count}</span>
</p>
<button onClick={increment} id="mainButton">
Increase
</button>
</div>
)
}
const style = {
table: {
borderCollapse: "collapse",
},
tableCell: {
border: "1px solid gray",
margin: 0,
padding: "5px 10px",
width: "max-content",
minWidth: "150px",
},
form: {
container: {
padding: "20px",
border: "1px solid #F0F8FF",
borderRadius: "15px",
width: "max-content",
marginBottom: "40px",
},
inputs: {
marginBottom: "5px",
},
submitBtn: {
marginTop: "10px",
padding: "10px 15px",
border: "none",
backgroundColor: "lightseagreen",
fontSize: "14px",
borderRadius: "5px",
},
},
}
return (
<form
onSubmit={e => {
[Link]()
addEntryToPhoneBook(formState)
dispatch({ type: "RESET" })
}}
style={[Link]}
>
<label>First name:</label>
<br />
<input
style={[Link]}
className="userFirstname"
name="userFirstname"
type="text"
onChange={onChange}
value={[Link]}
/>
<br />
<label>Last name:</label>
<br />
<input
style={[Link]}
className="userLastname"
name="userLastname"
type="text"
onChange={onChange}
value={[Link]}
/>
<br />
<label>Phone:</label>
<br />
<input
style={[Link]}
className="userPhone"
name="userPhone"
type="text"
onChange={onChange}
value={[Link]}
/>
<br />
<input
style={[Link]}
className="submitButton"
type="submit"
value="Add User"
/>
</form>
)
}
function Application() {
const [entries, setEntries] = useState([])
return (
<section>
<PhoneBookForm entries={entries} setEntries={setEntries} />
<InformationTable entries={entries} />
</section>
)
}
const data = [
{ name: "Daniel", age: 25 },
{ name: "John", age: 24 },
{ name: "Jen", age: 31 },
]