-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
65 lines (54 loc) · 1.75 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { Component } from 'react';
import './App.css';
import Letter from './Letter'
const VISUAL_PAUSE_MSECS=750
class App extends Component {
state = {
secretMessage : "HELLO WORLD",
testedLetters : new Set(),
letters : "abcdefghijklmnopqrstuvwxyz".toUpperCase().split(""),
currentLetter: ""
}
computeDisplay() {
const { secretMessage, testedLetters, letters, currentLetter } = this.state
return secretMessage.replace(/\w/g,
(letter) => (testedLetters.has(letter) ? letter : '_')
)
}
//arrow to get the this
handleLetterClick = letter => {
const { secretMessage, testedLetters, letters, currentLetter } = this.state
if(currentLetter==="" && !testedLetters.has(letter)){
testedLetters.add(letter)
this.setState({testedLetters: testedLetters})
this.setState({currentLetter: letter})
setTimeout(() => this.setState({ currentLetter: "" }), VISUAL_PAUSE_MSECS)
}
}
getFeedbackForLetter(letter){
const { secretMessage, testedLetters, letters, currentLetter } = this.state
if(currentLetter === letter){return "current"}
else if (testedLetters.has(letter)) {return secretMessage.indexOf(letter)>=0 ? "testedGood" : "testedBad"}
else{return "toTest"}
}
render() {
const {secretMessage, testedLetters, letters } = this.state
return (
<div className="pendu">
<p>{this.computeDisplay()}</p>
<div className="alphabet">
{letters.map((letter, index) => (
<Letter
letter={letter}
feedback={this.getFeedbackForLetter(letter)}
index={index}
key={index}
onClick={this.handleLetterClick}
/>
))}
</div>
</div>
)
}
}
export default App;