Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Random damage generator

Hi guys.

I've been learning javascript for the past couple of days, and can't figure out how to get this code to work.

I want there to only be three tries using Math.random to reach 600 damage.

Can anyone tell me what I would need to do/look into in order to increase my knowledge of making a simple js game with similar mechanics?

Am I using Math.random properly? Will i have to add more code like loops/if else statements, or can I get it to work without them? Please help!

I am trying to approach javascript in the most fun way possible, while using everything that I am learning so that I retain the information. Any resources or tips will be greatly appreciated.

var name = prompt("What shall I call you, warrior?");

var objective = alert("Okay, " + name + " you will attack three times. " + "You need a total of 600 damage to win!" );

var hit = Math.floor(Math.random()*300)+1

var crit = Math.floor(Math.random(301)*700)+1

var attack = Math.random(hit || crit)

var damage = 0;

var swingOne = prompt("If you're gonna attack, do it now! [ATTACK or RUN]" + "[" + "damage: " + damage + "]");

var swingTwo = prompt("It's not over yet! You still need more damage! [ATTACK or RUN]" + "[" + "damage: " + damage + "]");

var swingThree = prompt("This is your last swing. Make it count! [ATTACK or RUN]" + "[" + "damage: " + damage + "]");

score += hit + crit;

document.write(damage);

1 Answer

Hey Joseph,

I made up a little version of your game for you at this codepen: http://codepen.io/anon/pen/KpaBdM?editors=101. I noted in the JavaScript, but I'll say it here too: this shouldn't be considered a final version of any code and can be greatly optimized. It is, however, great for learning the logic behind rolls for games because of its stark simplicity.

If you notice, some things I did was instead of telling the user that they hit a critical, you can express that through words. A regular hit just has some kind of generic text whereas the critical hit has a special text where something mystical happened while hitting the monster. i.e. If your second hit was regular, the text will display like this: "You swing your sword a second time and hit the monster with 140 damage!". But, if you happen to be able to hit a critical, it will instead say something like this: "You swing your sword the second time and the heavens shook from your fury! You hit the monster with 580 damage!" Even though I didn't expressly say you hit a critical, you know you did because your damage was so much and that text felt special. It's boring to just say that the user hit a critical. Be sure to flavor it up :)

Math.random() rolls

It would take me an hour to fill this answer box up with how to use random rolls and things like that. I'll give you the basics, though.

Math.random() never takes an argument inside its parenthesis. You always manipulate Math.random() by using operations with it. Math.random() generates a number from 0 - 0.9999 although those two values are rare themselves.

In order to truly get a grasp on Math.random(), you'll want to make up some numbers and do the math yourself, maybe on a sheet of paper so you can see how the minimum and maximum possible values work. But here are the basics to that:

Use Math.floor() to surround your rolls so that you get an integer value. This is the formula you can use to achieve the desired range of rolls:

Math.floor(Math.random() * (maximumRoll - minimumRoll + 1))  + minimumRoll;

Where maximumRoll is the maximum number you want to be able to roll and minimumRoll is the minimum number you wish to be able to roll.

For example, in the game I linked you to, the maximum critical damage is 700 and the base damage is 300. So a critical hit should probably hit somewhere in between that range of 300 - 700. So, my maximumRoll here is 700 (aka the variable critical in the game) and my minimumRoll is 300 (aka damage in the game).

This is really something you have to play around with and really focus on to understand. It would take way too long to try to explain every single detail, but I learned by messing around with the code and doing the math myself.

I hope that helps!

Thanks, this answer explains a lot and i'll definitely keep it in mind while continuing to learn JS. Also, the game is entertaining lol! Thanks for the codepen.

Haha glad you enjoyed it! :D It actually makes me want to write a full fledged adventure funny game haha When writing your own game, try to actually avoid using those alert and prompt boxes that pop up on screen. I don't like using them at all because they can be very annoying, but they are some of the first things you learn so I figured you'd know what was going on when I used them. I'd instead switch over to inputs and outputting information to the page. Good luck, Joseph, and I hope to see you create your adventure game so I can play it! :)