Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

capturing 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

Hi 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 + ".");
}

Hi 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

Hi 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.

Hi Evan,

I think I finally got it, thanks again for your time and help.

Best regards,

Mirko