Skip to content

Commit 33df2b6

Browse files
committed
feat: add letter-tiles.jsx solution
1 parent 036d970 commit 33df2b6

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

solutions/letter-tiles.jsx

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import React, { useState, useCallback, useMemo } from "react"
2+
import { createRoot } from "react-dom/client"
3+
4+
const style = {
5+
letterContainer: {
6+
overflow: "auto",
7+
marginBottom: "10px",
8+
display: "flex",
9+
flexWrap: "wrap",
10+
justifyContent: "center",
11+
alignItems: "center",
12+
},
13+
letter: {
14+
float: "left",
15+
padding: "10px 10px",
16+
background: "#c9e4ed",
17+
borderRadius: "5px",
18+
marginRight: "5px",
19+
marginTop: "5px",
20+
cursor: "pointer",
21+
},
22+
outputString: {
23+
marginTop: "20px",
24+
textAlign: "center",
25+
},
26+
}
27+
28+
const Tile = ({ letter, outputArray, setOutputArray, tally, setTally }) => {
29+
const onClick = useCallback(() => {
30+
if (!tally[letter]) {
31+
setTally({ ...tally, [`${letter}`]: 1 })
32+
setOutputArray([...outputArray, letter])
33+
} else if (tally[`${letter}`] && tally[`${letter}`] === 2) {
34+
setTally({ ...tally, [`${letter}`]: 0 })
35+
const slicedArray = outputArray.slice(0, outputArray.length - 2)
36+
setOutputArray([...slicedArray, "_"])
37+
} else {
38+
setTally({ ...tally, [`${letter}`]: tally[`${letter}`] + 1 })
39+
setOutputArray([...outputArray, letter])
40+
}
41+
}, [letter, outputArray, setOutputArray, tally, setTally])
42+
43+
return (
44+
<button type="button" onClick={onClick} style={style.letter}>
45+
{letter}
46+
</button>
47+
)
48+
}
49+
50+
const Application = () => {
51+
const [outputArray, setOutputArray] = useState([])
52+
const [tally, setTally] = useState({})
53+
const alphabetArray = useMemo(() => {
54+
let arr = []
55+
for (let i = 65; i <= 90; i++) {
56+
const char = String.fromCharCode(i)
57+
arr.push(char)
58+
}
59+
return arr
60+
}, [])
61+
return (
62+
<section>
63+
<aside style={style.letterContainer} id="letterContainer">
64+
{alphabetArray.map((letter, index) => (
65+
<Tile
66+
tally={tally}
67+
setTally={setTally}
68+
letter={letter}
69+
key={index}
70+
outputArray={outputArray}
71+
setOutputArray={setOutputArray}
72+
/>
73+
))}
74+
</aside>
75+
<div style={style.outputString} id="outputString">
76+
{outputArray.join("")}
77+
</div>
78+
</section>
79+
)
80+
}
81+
82+
const root = createRoot(document.getElementById("root"))
83+
root.render(<Application />)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy