Handling Enter Key0:00
We can now re-render our grid each time the user presses a key. So now, when they get to the end and they press return, we want to evaluate their guess. All right, let's see how. We're back in our game here. And right down here, we're responding to a key press. So if they hit a letter, then we fill the tile. Otherwise, if the key is enter, then we'll say alert press enter. And let's see if that works. Refresh, C-A-T, enter. And now we're responding.
Creating submitGuess Method0:28
Refresh, C-A-T, enter. And now we're responding. Okay, so like we did up here, why don't we defer to a method like submitGuess. Okay, come on down. submitGuess. And if you think about it, hold on, right here, alert submit. The very first thing I want to check is did you press return before you've completed a row? And notice right here, it says submit, but I don't care to respond to that. You haven't entered enough letters. So maybe we could say something like if the guess length is less than the length of the word,
You haven't entered enough letters. So maybe we could say something like if the guess length is less than the length of the word, then return because we're not ready to do anything. Okay, now you'll notice right now we're not yet tracking the current guess. Like I said, maybe we could track it there. We could also just fetch it out of the board. Hmm, how do we want to do this? I guess the source of truth way would be to fetch it out of the array here. Otherwise, you're maintaining the current guess in two different places. But sometimes it's useful to do that.
Deriving Current Guess1:31
Otherwise, you're maintaining the current guess in two different places. But sometimes it's useful to do that. If we did want to grab something like, actually, I'll show you. You can do getCurrentGuess. And this is like setting up a getter property. It would allow me to, in my object, say this.currentGuess rather than calling it like a method. And yeah, what we could do there is basically say, grab the current row. So this.currentRow. And actually on that node, currentRow could be a getter.
So this.currentRow. And actually on that node, currentRow could be a getter. Like that, which means, and by the way, we'll have to update this section here. But now I could just say this.currentRow. Yeah. Okay, but anyways, get me the currentRow, map over it. And for each tile, we're going to return the tile letter. So I think that should return the currentGuess. Let's give it a shot. Let's come back here.
Let's give it a shot. Let's come back here. And we'll say alert(this.currentGuess). Okay, refresh. C-A-T, return. And oh, we get it as an array, of course. So we would want to make sure that we join it. This seems somewhat convoluted versus just storing a currentGuess property. But if we need to, we can change it. Okay, so let's come back down.
Blocking Incomplete Guesses2:48
But if we need to, we can change it. Okay, so let's come back down. And actually, real quick, let's get rid of, there's no longer a tile index. Okay, so if the guess length is less, if the current guess length is less than the length of the word, and the length of the word here is 3. So we'll say if it's less than the word length, then let's just return silently, maybe. We don't want to respond to it yet. Otherwise, we can continue. Okay, let's give it a shot. C, and I'll hit return or enter.
Checking Guess Against Word3:21
Okay, let's give it a shot. C, and I'll hit return or enter. And we don't do anything. But if I finish the row, now we do. So yeah, what we could do here is begin our check. For example, let's catch this currentGuess. So I could say if the guess length is less than the word length, then we don't do anything. Otherwise, perform our checks here. If the guess equals the word, and actually, on that note, we're not tracking what the word is. So for now, I'm going to hard code it.
If the guess equals the word, and actually, on that note, we're not tracking what the word is. So for now, I'm going to hard code it. And why don't we be specific, the word will be cat. And actually, on that note, maybe we don't need wordLength anymore, because we can always determine the length of the word based on the actual word itself. So we can come back to that. Okay, come on down. So if the guess equals the word, then why don't we say alert, "you win". It's so easy. Come back, refresh, c-a-t, return, "you win the game."
