8

I'm learning javascript and I decided to create simple Rock, Paper, Scissors game. I want to make it controllable by buttons. So I made this in html:

<div id="game">
    <button onClick="user(rock)">Rock</button>
    <button onClick="user(paper)">Paper</button>
    <button onClick="user(scissors)">Scissors</button>
    <div id="result"></div>
    <br>
    <br>
    <button onClick="test()">DEBUG</button>
</div>

and this in .js file.

var user = "none";
function user(choice){
    var user = choice;
}

function test(click){
    alert("You chose " + user);
}

So I thought that after I click Rock button it will change var user to rock but it doesn't. After I click rock and then Debug button I get "You chose none".

8 Answers 8

8
<div id="game">
    <button onClick="choose('rock')">Rock</button>
    <button onClick="choose('paper')">Paper</button>
    <button onClick="choose('scissors')">Scissors</button>
    <div id="result"></div>
    <br>
    <br>
    <button onClick="test()">DEBUG</button>
</div>

and

var user;
function choose(choice){
    user = choice;
}

function test(click){
    alert("You chose " + user);
}                         
Sign up to request clarification or add additional context in comments.

1 Comment

This one helped me a lot. I found a mistake. :)
4

var is used for declaring a variable. You don't need to declare user variable again in user function. You just need to assign a value to declared one.

var user; //declaration
function user(choice) {
    user = choice; //assignment
}

1 Comment

I did it same way you typed it and I still get "none" from debug button.
2

One problem:

var user = "none";
function user(choice){
    var user = choice;
}

One variable of user is hiding the other variable of user.

And having a function and variable with the same name is a BAD idea.

Comments

1

The var keyword used in the scope of a function will declare a new local variable.

Hence, in the global scope, user retains the value "none".

Comments

1

Maybe try this.. cleaner markup.. uses jQuery

<div id="game">
    <button class="user" data-name="rock">Rock</button>
    <button class="user" data-name="paper">Paper</button>
    <button class="user" data-name="scissors">Scissors</button>
    <div id="result"></div>
    <br>
    <br>
    <button id="test">DEBUG</button>
</div>


$(document).ready(function() {
    var user = "none";
    $(".user").click(function() {
       user = $(this).attr("data-name");
    });

    $("#test").click(function() {
       alert(user);
    });
});

http://jsfiddle.net/rQDbe/

2 Comments

Thank you. I don't wanna use jQuerry yet. I'm learning javascript right now and after that I wanna learn jQuerry. :)
good to know this :) you should learn JavaScript before you use a library.
0

Seems like a scoping issue. Try removing the var inside the function.

Comments

0

The other answers are fixing some issues. There is also a problem with the way you're calling the functions as you're passing rock as a variable, you need to use a string:

<button onClick="user('rock')">Rock</button>

Unless you're declaring the variables somewhere but it's not shown in your code.

3 Comments

I fixed that. Now it's as you say: <button onClick="user("rock")">Rock</button> <button onClick="user("paper")">Paper</button> <button onClick="user("scissors")">Scissors</button>
Don't use double quotes for the string in the attributes or this won't work
Ahh that was the problem. Thank you!
0

The selections should be encased in the quotes, javascript is looking for the variables named paper etc.

<button onClick="user(rock)">Rock</button>
<button onClick="user(paper)">Paper</button>
<button onClick="user(scissors)">Scissors</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.