@@ -35,7 +35,7 @@ class App extends React.Component {
35
35
this . generateBars ( this . state . barCount ) ;
36
36
}
37
37
38
- handleStart = ( ) => {
38
+ generateSteps = ( ) => {
39
39
let array = this . state . array . slice ( ) ;
40
40
let steps = this . state . arraySteps . slice ( ) ;
41
41
let colorSteps = this . state . colorSteps . slice ( ) ;
@@ -46,10 +46,12 @@ class App extends React.Component {
46
46
arraySteps : steps ,
47
47
colorSteps : colorSteps ,
48
48
} ) ;
49
- this . run ( steps , colorSteps ) ;
50
49
}
51
50
52
- run ( steps , colorSteps ) {
51
+ setTimeouts ( ) {
52
+ let steps = this . state . arraySteps ;
53
+ let colorSteps = this . state . colorSteps ;
54
+
53
55
this . clearTimeouts ( ) ;
54
56
let timeouts = [ ] ;
55
57
let i = 0 ;
@@ -85,7 +87,7 @@ class App extends React.Component {
85
87
}
86
88
87
89
stepForward = ( ) => {
88
- if ( this . state . currentStep === this . state . arraySteps . length - 1 ) return ;
90
+ if ( this . state . currentStep === this . state . arraySteps . length ) return ;
89
91
this . clearTimeouts ( ) ;
90
92
91
93
let currentStep = this . state . currentStep + 1 ;
@@ -100,8 +102,8 @@ class App extends React.Component {
100
102
this . setState ( {
101
103
algorithm : event . target . value ,
102
104
currentStep : 0 ,
103
- arraySteps : [ this . state . arraySteps [ this . state . arraySteps . length - 1 ] ] ,
104
- } ) ;
105
+ arraySteps : [ this . state . arraySteps [ this . state . currentStep === 0 ? 0 : this . state . currentStep - 1 ] ] ,
106
+ } , ( ) => this . generateSteps ( ) ) ;
105
107
this . clearTimeouts ( ) ;
106
108
this . clearColorKey ( ) ;
107
109
} ;
@@ -144,7 +146,7 @@ class App extends React.Component {
144
146
arraySteps : [ barsTemp ] ,
145
147
barCount : barCount ,
146
148
currentStep : 0 ,
147
- } ) ;
149
+ } , ( ) => this . generateSteps ( ) ) ;
148
150
}
149
151
150
152
render ( ) {
@@ -162,14 +164,9 @@ class App extends React.Component {
162
164
< Pause />
163
165
</ IconButton >
164
166
) ;
165
- } else if ( this . state . arraySteps . length > 2 ) {
166
- playButton = (
167
- < IconButton color = "secondary" onClick = { ( ) => this . run ( this . state . arraySteps , this . state . colorSteps ) } >
168
- < PlayArrow />
169
- </ IconButton > ) ;
170
167
} else {
171
168
playButton = (
172
- < IconButton color = "secondary" onClick = { ( ) => this . handleStart ( ) } >
169
+ < IconButton color = "secondary" onClick = { ( ) => this . setTimeouts ( ) } >
173
170
< PlayArrow />
174
171
</ IconButton > ) ;
175
172
}
0 commit comments