0

I'm trying to get a random number each time someone clicks a button. The range of the numbers is from one(1) to five(5). If this question has already been asked and answered, please point me to the question and I'll delete this one to avoid duplication.

HTML

<div id="content"></div>
<button id="add">Add Object</button>

JS

$(document).ready(function(){
    var Player = {
        name: "One",
        weapon: Sword = {
            damage: 1 + Math.floor(Math.random() * 5),
            speed: 10
        }
    }
    var player_damage = Player.weapon.damage;

    $("#add").on("click", function(){
        $("#content").append(player_damage + " ");
    })
})

Fiddle

2
  • 3
    You only generated the number once. Everytime you click the button, you have the number generated that only time (always the same). Commented Feb 13, 2015 at 19:25
  • @HugoSousa so how do I get a random number with each click? Commented Feb 13, 2015 at 19:26

4 Answers 4

4

You don't update your number on click. You set it only once.

var Player = {
  name: "One",
  weapon: Sword = {
    damage: 0,
    speed: 10
  }
}

$("#add").on("click", function(){
  Player.weapon.damage = 1 + Math.floor(Math.random() * 5);
  $("#content").append(Player.weapon.damage + " ");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content"></div>
		<button id="add">Add Object</button>

Sign up to request clarification or add additional context in comments.

4 Comments

You're welcome - Please validate the answer if you're happy with it :)
I'm accepting it, when it lets me in about 4 minutes
These were the longest 4 minutes of my life :p
Haha, sorry, I got side tracked and honestly forgot about it.
2

Generate the new number inside the click function:

 $("#add").on("click", function(){
            var damage= 1 + Math.floor(Math.random() * 5);
            $("#content").append(damage.toString() + " ");
        })

jsfiddle

Comments

0

I updated it using a JavaScript property on the Sword object. Whenever the damage property is accessed, it generates a new value using a getter:

http://jsfiddle.net/hd7tec5f/3/

Object.defineProperty(Sword, "damage", {
    get: function() {
        return 1 + Math.round(Math.random() * 1));
    }
});

Comments

0
$(document).ready(function(){
   $("#add").on("click", function(){
        var Player = {
            name: "One",
            weapon: Sword = {
                damage: 1 + Math.floor(Math.random() * 5),
                speed: 10
            }
        };

        var player_damage = Player.weapon.damage;                                           
        $("#content").append(player_damage + " ");
    })
});

http://jsfiddle.net/manzapanza/rayn6m4L/

3 Comments

I believe recreating an object again and again unless absolutely necessary is a bad design.
This is creating the object each time I click the button, not something I want to do as it will eventually cause huge memory and performance issues.
I agree with you, my answer could be improved. Personally I prefer much more the solution of the accepted answer

Your Answer

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