0

Super new to javascript. I'm trying to make a psychic guessing game. Everything here works except the onkeyup function. When I open the console log and type letters, it tells me that the userGuess variable is undefined. How do I defined the userGuess variable to match the onkeyup function?

Thanks:

    //Available choices
var letterChoices = ['a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

//score
var wins = 0;
var losses = 0;
var guesses = 9;
var guessesLeft = 9;
var guessedLetters = [];
var letterToGuess = null;

//computer randomly chooses a letter

var computerGuess = letterChoices [Math.floor(Math.random()*letterChoices.length)];

//guesses left function

var updateGuessesLeft = function() {
    document.querySelector('#guessLeft').innerHTML = "Guesses Left: " + guessesLeft;
};

//letter to guess function

var updateletterToGuess = function(){
    this.letterToGuess = this.letterChoices[Math.floor(Math.random() * this.letterChoices.length)];
};

var updateGuessesSoFar = function(){
    document.querySelector('#let').innerHTML = "Your guesses so far: " + guessedLetters.join(', ');
};

//reset

var reset = function(){
    totalGuesses = 9;
    guessesLeft = 9;
    guessedLetters = [];

    updateletterToGuess();
    updateGuessesSoFar();
    updateGuessesLeft();

};

updateGuessesLeft();
updateletterToGuess();

//user input key

document.onkeyup = function(event) {
    guessesLeft--;
    var userGuess;
    console.log(userGuess)

    guessedLetters.push(userGuess);
    updateGuessesLeft();
    updateGuessesSoFar();

        if (guessesLeft > 0){
            if (userGuess === letterToGuess){
                wins++;
                document.querySelector('#wins').innerHTML = 'Wins: ' + wins;
                alert("How did you know!?!");
                reset();
            }
        } else if (guessesLeft == 0){
            losses++;
            document.querySelector('#losses').innerHTML = 'Losses: ' + losses;
            alert("Sorry, you're not a psychic!");

            reset();
        }
}
  • You're doing `var userGuess; console.log(userGuess);`. The variable `userGuess` is hence `undefined`. Are you trying to take user input using `console.log`? See [_What is console.log_](http://stackoverflow.com/q/4539253/2950032) – maazadeeb May 22 '17 at 02:47
  • I was doing console.log to test if the user input was registering. – Shawn Mukherji May 22 '17 at 08:10
  • I'm trying to get userGuess become a string, which displays in guessedLetters. and userGuess should be the onkeyup event. – Shawn Mukherji May 22 '17 at 08:12
  • Are you trying to figure out which was the key that was pressed? You can access it as [`event.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) in the `onkeyup` callback. If not, please provide a minimal working sample, with the precise problem. – maazadeeb May 22 '17 at 08:19
  • Here's the working example: https://shawnmukherji.github.io/psychic-game/ The problem is that userGuess is undefined. I don't know why. Ideally, every key stroke should be logged in under guessedLetters. ex: a, t, y, m – Shawn Mukherji May 22 '17 at 08:34

1 Answers1

0

The variable userGuess is undefined because it is just declared. There is no assignment operation happening to this variable. I looked at the working sample, modified it in the browser, and it seems to be working as expected. The only change I did was to assign userGuess to the key pressed, as follows

var userGuess = event.key;

Here is the output I got by playing around a bit with your code

Since you have a list of characters that you want to allow, you could add the following code at the beginning, to make sure anything other than the allowed characters are not considered

if (letterChoices.indexOf(userGuess) === -1) return;
maazadeeb
  • 5,922
  • 2
  • 27
  • 40