0

i created my dice as images so just needing to figure out how to be able to count how many of each die there are in the array . Such as how many 1's or 2's there are so that im able to compare that to possible yahtzee combinations

$(document).ready(function(){
  var die1=$('.die1');
  var die2=$('.die2');
  var die3=$('.die3');
  var die4=$('.die4');
  var die5=$('.die5');
  var turns=3;

  
  //roll function
 function roll(die){
     var rando = Math.floor(Math.random()*6)+1;
      die.html("<img  src=images/die"+rando+".png>");
      
    $('img').height(50);
 };


 $('.die').click(function(){
         $(this).toggleClass('selected');   //adds border around die if clicked
  });

//attaches roll funcition to each die
$('.button').click(function(){
    for(i=0; i<=turns; turns--){
       if(turns>0){
      var dice =[die1,die2,die3,die4,die5];

     for(i=0; i<dice.length;i++){
        if (!dice[i].hasClass('selected')){ 
          roll(dice[i]);
        }
       
        }
       
    }
    else{
   $('.warning').html('Pick a category!');
  }
 }

  });   //button function



});
a.c
  • 23
  • 6
  • don't know how to play yatzee but if technically all you want to do is compare array values maybe this question might help http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – Sergio Alen Feb 08 '17 at 00:15

1 Answers1

0

You need to be saving the rando value you are creating.

var dice_value=new Array(5);
//dice1 value is in dice_value[0] etc...

then save it when you run the roll function

function roll(die,die_index){
    var rando = Math.floor(Math.random()*6)+1;
     die.html("<img  src=images/die"+rando+".png>");

     dice_value[die_index]=rando;

   $('img').height(50);
};

When you roll just pass along the index as well

roll(dice[i],i);

Now you have an array containing all the values of the dice.

CodeCabin
  • 166
  • 1
  • 11
  • a bit confused because i already have an array with all the dice. okay no I understand i think. – a.c Feb 08 '17 at 00:46
  • you have an array of the images, but with no value associated with them. – CodeCabin Feb 08 '17 at 00:52
  • you don't understand why you need the extra i? when you were first calling the roll function you were only passing the dice element. That information will not tell you which dice you are rolling. The i is the answer to which dice you are rolling. – CodeCabin Feb 08 '17 at 01:19
  • yeah i realized that and i felt dumb for asking. haha thanks again. – a.c Feb 08 '17 at 01:24