Robert Zobrist
Robert Zobrist

Reputation: 59

Introduction Javascript - functions, syntax, logic

I'm attempting to build a rock, paper, scissors game. Here is my code:

var options = ["r", "p", "s"];
    
document.onkeyup = function() {
    var userGuess = String.fromCharCode(event.keycode).toLowerCase();

    console.log(userGuess);

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

    console.log(computerGuess);

    if (userGuess == "r" || userGuess == "p" || userGuess == "s") {
        alert("Chosen Correctly - Good job!");
    } else {
        alert("Please choose r, p, or s");
    }
}

when I press r, p, or s my alert doesn't say "Chosen Correctly - Good job!"; instead, the alert is please choose "r", "p", or "s". HHHmmmm, without this working I can't begin to compare the user guess to the computer guess. Will any fresh set of eyes help me out? Please and thank you.

Upvotes: 2

Views: 98

Answers (6)

hardy
hardy

Reputation: 901

Just change event.keycode TO event.keyCode

Upvotes: 0

Lambart
Lambart

Reputation: 2086

As others have said, you missed the capital "C" in keyCode.

But it also looks to me that you aren't defining event anywhere. Using a global event object is a bad idea and (thankfully) isn't supported by all browsers. You may also want to use event.which rather than event.keyCode, which doesn't work consistently for all events.

The start of your event handler should look more like this:

document.onkeyup = function(event) {
    var userGuess = String.fromCharCode(event.which).toLowerCase();
    ...

But you can also use console logging to see what's going on, like this:

document.onkeyup = function(event) {
    console.log("event=", event);
    var userGuess = String.fromCharCode(event.which).toLowerCase();

Note that I didn't say console.log("event=" + event);

The + would cause the event to be output as something like [object Object]; with the comma (and any modern browser's developer console, usually accessible with F12, you'll be able to browse the contents of the object.

Upvotes: 1

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

You will want to have event as a paramater to the event handler and it's event.keyCode rather than event.keycode (which will be undefined):

var options = ["r", "p", "s"];
    
document.onkeyup = function(event) {
    var userGuess = String.fromCharCode(event.keyCode).toLowerCase();

    console.log(userGuess);

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

    console.log(computerGuess);

    if (userGuess == "r" || userGuess == "p" || userGuess == "s") {
        alert("Chosen Correctly - Good job!");
    } else {
        alert("Please choose r, p, or s");
    }
}

Upvotes: 0

msanford
msanford

Reputation: 12227

Two changes:

  1. Pass in the event so it's available locally.
  2. keyCode is written in intercaps, (not keycode, as you have it); JavaScript is case-sensitive.

Good debugging tip is to console.dir() an object you're trying to inspect, that will reveal the correct (and available) property and method names.

(Note, however, that event is huge so you'll have a lot to sift through.)

var options = ["r", "p", "s"];
    
document.onkeyup = function(event) {

    var userGuess = String.fromCharCode(event.keyCode).toLowerCase();

    console.log(userGuess);

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

    console.log(computerGuess);

    if (userGuess == "r" || userGuess == "p" || userGuess == "s") {
        alert("Chosen Correctly - Good job!");
    } else {
        alert("Please choose r, p, or s");
    }
}

Upvotes: 1

conman124
conman124

Reputation: 116

event.keycode should be event.keyCode, notice the capital C. Also, it would probably be better to just use event.key, since that is already a string.

What browser are you developing in? Developer tools can be accessed in most by pressing F12, where you can put a breakpoint to access variables while your program is running.

Upvotes: 1

IrkenInvader
IrkenInvader

Reputation: 4050

You are missing a captial C on keyCode.

var userGuess = String.fromCharCode(event.keyCode).toLowerCase();

Upvotes: 1

Related Questions