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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Mirko Nobelen](https://secure.gravatar.com/avatar/afd6c734983fa7d4f54212fb9b861f39?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Mirko Nobelen
7,507 Pointscapturing the highest nr of a random value in javascript
Dear Sir or Madam,
I am trying to capture the highest value of a randomly generated nr. The code runs but the answer I get is wrong. Could you be so kind to find what I am doing wrong.
my code below is:
//calculate the attacks of a warrior var nrAttacks=prompt("How many attacks does your opponent have ?"); var nrAt= parseInt(nrAttacks); var nrOfAttackers= prompt ("The attacker is attacker number?"); var nrOfAt= parseInt(nrOfAttackers); var totalAttacks= nrOfAt+nrAt;
// for forloop var attackDiceRolls=" " ; var maxDice;
//function for a random D6 roll
function randomDiceRoll() { return randAt = Math.floor (Math.random()*6)+1; }
//you count how many times a random value between 1 and 6 need to be calculated for ( var i = 1; i <= totalAttacks; i += 1 ) {
randomDiceRoll();
//to measure which of the rolls is the highest nr maxDiceOponent = Math.max (randomDiceRoll());
//defining the variable "dice rolls"
attackDiceRolls = "The enemy rolled a " + randomDiceRoll() + "<br>" ;
document.write(attackDiceRolls); }
document.write("the highest nr of your oponent's roll is" + maxDiceOponent);
many thanks
2 Answers
![Evan Demaris](https://uploads.teamtreehouse.com/production/profile-photos/4841532/micro_20150919_163800.jpg)
Evan Demaris
64,262 PointsHi Mirko,
When you're running Math.max(randomDiceRoll()), you're only checking the max of one number. Also, every time randomDiceRoll is called it creates a new number. You also don't need to assign as many variables as you've included here.
var totalAttacks= parseInt(prompt("How many attacks does your opponent have ?"))
+ parseInt(prompt ("The attacker is attacker number?")); /*This question is perplexing.
Does attacker number refer to how many attackers there are?
Or does it refer to the strength of the attacks?
Either way, we would multiply instead of adding here.*/
var highestDiceRoll = 0;
if(!isNaN(totalAttacks) && totalAttacks>0) {
for (var i = 1; i <= totalAttacks; i++ ) {
var currentDiceRoll = Math.floor(Math.random()*6)+1;
highestDiceRoll = Math.max(highestDiceRoll, currentDiceRoll);
if(totalAttacks > 1) { document.write("The enemy rolled a " + currentDiceRoll + "!<br>"); }
}
document.write("Your opponent's highest roll was " + highestDiceRoll + ".");
}
![Mirko Nobelen](https://secure.gravatar.com/avatar/afd6c734983fa7d4f54212fb9b861f39?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Mirko Nobelen
7,507 PointsHi Evan,
I think I finally got it, thanks again for your time and help.
Best regards,
Mirko
Mirko Nobelen
7,507 PointsMirko Nobelen
7,507 PointsHi Evan,
Many thanks for your answer it is much appreciated. I am still very much a beginner as you can see.
If I may, in regard to the piece of code below how does the computer behave for the variable "highestDiceRoll"?
<as long as the totalAttacks is not Nan and greater than 1 then run the for loop carrying out "currentDiceRoll".
Then determine the "highestDiceRoll" which is the highest numberr between the "highestDiceRoll" and "currentDiceRoll"?
The "highestDiceRoll" being 0?>
var highestDiceRoll = 0; if(!isNaN(totalAttacks) && totalAttacks>0) { for (var i = 1; i <= totalAttacks; i++ ) { var currentDiceRoll = Math.floor(Math.random()*6)+1;
highestDiceRoll = Math.max(highestDiceRoll, currentDiceRoll);
Kind regards,
Mirko
Evan Demaris
64,262 PointsEvan Demaris
64,262 PointsHi Mirko,
The code would read in English as follows:
You start off with 0 as your highest dice roll because you haven't rolled any dice yet.
If totalAttacks is a number, and that number isn't 0 or negative, then roll a die for the number of totalAttacks. Each roll will be recorded, but only the highest roll is kept after each iteration through the loop. If there's only one roll, then don't bother writing out that individual roll, just write the highest roll. Otherwise, write the highest roll once you're finished with all the rolls.
The variable highestDiceRoll is kept out of the loop because we don't want to assign the 0 value to it on each iteration, just when it's first declared. If you moved that assignment into the
for
closure, then you would instead be assigning highestDiceRoll to 0 on each iteration instead of saving the previous roll as intended. However, because the assignment is outside the loop, this code preserves whatever the previous highestDiceRoll was in the last iteration of the loop, so you're comparing roll to roll.