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

My javascript won't give me new numbers

Hello,

I'm taking the information I just learned from the JavaScript Basics course and trying my hand at making a fun "turn based battle simulator" between you and an Orc. Nothing super special here just trying to use what I learned about functions and how to create a random number generator.

When I have the JavaScript run the characterAttack() function and the orcAttack() function twice each, the numbers provided by the random number generator I learned how to build in the Basics course are the same each time the functions run.

The characterAttack() gets a 13 on a roll and does 10 damage (for examples), the next time the function is run the numbers are exactly the same. Is there something wrong with my code or is there just something that I haven't learned yet?

I thought functions ran their code anew every time they were called.

Thanks, -Michael Cane

function attackRoll() {
    return Math.floor( Math.random() * 20) + 1;
}

function profDmg() {
    return Math.floor( Math.random() * 8) + 1;
}

function playerAttack() {
    document.write("<br>You roll " + charAttack + " for your attack.");
    if (charAttack > enemyDef || charAttack === enemyDef) {
        var message = "<br>Your weapon cuts deep into your enemy\'s abdomen, dealing " + charDamage + " damage. A good clean hit.";
        document.write(message);
        enemyHP -= charDamage;
        document.write("<br>Your enemy has " + enemyHP + " Hit Points left.");
    } else if (charAttack < enemyDef) {
        var message = "<br>Your swing goes wide and catches nothing but air.";
        document.write(message);
        document.write("<br>Your enemy has " + enemyHP + " Hit Points left.");
    }
}

// Character values
var charHP = 20;
var charDef = 7;
var charWeapRank = 4;
var charAttack = attackRoll()
var charDamage = parseFloat((profDmg()) + (charWeapRank));

Hey Michael,

Could you post your code? You can click on the Markdown Cheatsheet below the text area where you post your question to see how you can easily format your code.

4 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Michael Cane

Don't put your code away! Keeping working on it. You're doing great. I think this might be the solution for you:

function playerAttack() {
    var charAttack = attackRoll();
    document.write("<br>You roll " + charAttack + " for your attack.");
    if (charAttack > enemyDef || charAttack === enemyDef) {
        var message = "<br>Your weapon cuts deep into your enemy\'s abdomen, dealing " + charDamage + " damage. A good clean hit.";
        document.write(message);
        enemyHP -= charDamage;
        document.write("<br>Your enemy has " + enemyHP + " Hit Points left.");
    } else if (charAttack < enemyDef) {
        var message = "<br>Your swing goes wide and catches nothing but air.";
        document.write(message);
        document.write("<br>Your enemy has " + enemyHP + " Hit Points left.");
    }
}

My addition is var charAttack = attackRoll();. I added this INSIDE the playerAttack() function. Now, each time that function is called, a new random attack number is generated and stored in the charAttack variable. That variable will hold the same number the entire time the function runs, but it will change with each new call to the playerAttack() function.

Post if you have other questions.

Dave McFarland,

Thank you so much! I've been tweaking my code for days now, and I never thought to add the "var charAttack = attackRoll()" into the actual attackRoll function. That's really cool, I didn't know you could do that.

I'm sorry I can't type more right now, I'm actually just too ecstatic and happy for the response and because the answer is so simple to write anything really coherent.

Thank you guys for making this website and all these great Tracks and Courses.

Thanks again, -Michael Cane

OK, I see what's going on here. It's a tricky one, so I don't blame you for being a little baffled by it.

This is your problem child:

var charAttack = attackRoll()

Let's review what's happening here: You created a variable named "charAttack" and you assigned it to the result of the function, not the function itself. How? By adding the open and close parenthesis (). These parenthesis are calling your function, and returning a random number. And that resulting number is what is being stored in your variable, instead of the function itself.

So in that one line of code, you called attackRoll(), it returned a number, got stored in charAttack, and was never called again. Now every time you use charAttack, you are just displaying the number it has stored in it.

There are a few ways you can fix your problem. Here's the most straightforward one:

Forget the charAttack variable altogether, and just call attackRoll() wherever you need it.

document.write("<br>You roll " + attackRoll() + " for your attack.");

Here, the attackRoll() function will be called anew every time this line is executed.

But what if you want to still use your charAttack variable for any other reason (it could be just because it's easier to read like that for you, and that's a perfectly good reason), you can do it like this:

var charAttack = attackRoll
// notice there are no parenthesis. This means the function itself is being stored, without being called.

Now charAttack contains the function attackRoll. But you still need to call it. To call it, just add the ol' function call parenthesis.

document.write("<br>You roll " + charAttack() + " for your attack.");

Here's a plnk where you can see it in action. Let me know if you still have trouble getting a grip on this after looking at that code. It can be pretty confusing.

Nick,

Thanks for getting back to me so quickly. I tried the second solution, "charAttack(), " and it worked perfectly. I went back and checked my notes for the JavaScript Basics course and I'm definitely treading into water I don't know how to swim in yet. But that's okay and really really awesome in my opinion because I want to learn why my code doesn't work and how to not make that mistake in the future. So, thanks.

But now to why I'm replying, now that I have the charAttack() variable-function combo the next line of code is not working so hot. Since charAttack() no longer stores the result of attackRoll(), just generates it anew, it rerolls a random number in the next line of code, the "if" statement as well, and the message often does not show.

I've done a little research into how to fix the problem but I don't know if I should just put the code away for a while and come back when I have a greater grasp on JavaScript or push on forward and learn by Treehouse course and by just doing.

Thank you again, -Michael

Hi Michael,

Could you please put your code generating random number here in order to discuss with you? I guess different seeds should be used for getting random number when using other programming language, but I think Math.random() in Javascript should produce different random number.

Regards!

I added the JavaScript to the post. Again, I'm about two days into knowing any JavaScript at all, I just finished the JavaScript Basics course on Tuesday, so if the code looks sloppy or just plain wrong please bear with me.

Thank you again to anyone who decides to assist. Coding is a real blast and I never thought I could until I came to TeamTreeHouse.

-Michael