Click here to Skip to main content
15,888,315 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: Inheriting a Javascript Prototype for Web Usercontrol Pin
HemeshSoni13-Feb-17 17:18
HemeshSoni13-Feb-17 17:18 
AnswerRe: Inheriting a Javascript Prototype for Web Usercontrol Pin
HemeshSoni13-Feb-17 17:25
HemeshSoni13-Feb-17 17:25 
Questionmvc app with angular not working in IIS on 80 port Pin
Member 1103130413-Feb-17 0:17
Member 1103130413-Feb-17 0:17 
AnswerRe: mvc app with angular not working in IIS on 80 port Pin
Nathan Minier13-Feb-17 0:54
professionalNathan Minier13-Feb-17 0:54 
GeneralRe: mvc app with angular not working in IIS on 80 port Pin
Member 1103130413-Feb-17 1:04
Member 1103130413-Feb-17 1:04 
GeneralRe: mvc app with angular not working in IIS on 80 port Pin
Nathan Minier13-Feb-17 1:27
professionalNathan Minier13-Feb-17 1:27 
GeneralRe: mvc app with angular not working in IIS on 80 port Pin
Member 1103130413-Feb-17 2:02
Member 1103130413-Feb-17 2:02 
Questionlooping through an array that has value based on a dice roll. Need to properly calculate the rolls. Pin
Member 1299542011-Feb-17 9:25
Member 1299542011-Feb-17 9:25 
Created a loop to loop through an array based on the values from the dice roll. Trying to get all the possible scores for a yahtzee game. so far I have it working to that it does add the scores based on the roll example1
I also have it working so that it does not continue to add the score if the a certain die is selected

However my issue is that it keeps increasing the score instead of starting over on subsequent rolls example2

JavaScript
<pre>$(document).ready(function(){
  var die1=$('.die1');
  var die2=$('.die2');
  var die3=$('.die3');
  var die4=$('.die4');
  var die5=$('.die5');
  var turns=3;
  var dice;
  var dice_value = new Array(5);
  var ones=0;
  var twos=0;
  var threes=0;
  var fours=0;
  var fives=0;
  var sixes=0;
  var die;
  //roll function
	function roll(die,i){
	    rando = Math.floor(Math.random()*6)+1;
	    
	     die.html("<img  src=images/die"+rando+".png>");
	      die.find('img').attr('class', rando);
	      
          dice_value[i]=rando;
         
	   $('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){
		    dice =[die1,die2,die3,die4,die5];

			  for(i=0; i<dice.length;i++){
			  	   	
			  	    
				  	 if (!dice[i].hasClass('selected')){ 
				  	 	  roll(dice[i],i);

                        

                       die= dice[i].find('img').attr('class');

	              }
                   
				 

	              }

            }
	   
		  else{
			$('.warning').html('Pick a category!');
		}

      
	}
	  for(var i=0; i< dice_value.length;i++){
     	if(!dice[i].hasClass('selected') && turns > 0){
     	
	   if( dice_value[i] == 1){
	   	 ones= ones+1;
	   	  
	   	 $('.aceScore').html(ones);
           
	   } 
	    if( dice_value[i] == 2){
	   	 twos= twos+2;
	   	  $('.twoScore').html(twos);
	   	  
	   } 
	    if( dice_value[i] == 3){
	   	 threes= threes+3;
	   	  $('.threeScore').html(threes);
	   	 
	   } 
	    if( dice_value[i] == 4){
	   	 fours= fours+4;
	   	  $('.fourScore').html(fours);
	   	  
	   } 
	    if( dice_value[i] == 5){
	   	 fives= fives+5;
	   	  $('.fiveScore').html(fives);
	   	 
	   } 
	    if( dice_value[i] == 6){
	   	 sixes= sixes+6;
	   	  $('.sixScore').html(sixes);
	   	  
	   } 
	   
	
	   	 
}
	
}
 
 

  });   //button function

 

console.log(ones);
console.log(twos);

console.log(threes);

console.log(fours);
console.log(fives);

console.log(sixes);

 
});


<pre lang="HTML"><pre><table border="1" height='200' >
	    <tr>

	 		<th colspan="3">Upper Section</th>
	 	</tr>
	 	<tr>
	 		<td>Aces</td>
	 		<td>Sum of all the ones</td>
	 		<td class="score aceScore"></td>
	 	</tr>
	 		<tr>
	 		<td>Twos</td>
	 		<td>Sum of all the twos</td>
	 		<td class="score twoScore"></td>
	 	</tr>
	 		<tr>
	 		<td>Threes</td>
	 		<td>Sum of all the threes</td>
	 		<td class="score threeScore"></td>
	 	</tr>
	 		<tr>
	 		<td>Fours</td>
	 		<td>Sum of all the fours</td>
	 		<td class="score fourScore"></td>
	 	</tr>
	 		<tr>
	 		<td>Fives</td>
	 		<td>Sum of all the fives</td>
	 		<td class="score fiveScore"></td>

	 	</tr>
	 	<tr>
	 		<td>Sixes</td>
	 		<td>Sum of all the sixes</td>
	 		<td class="score sixScore"></td>
	 	</tr>
	 	 <tr>
	 		<td colspan ='2'>Total</td>
	 	
	 		<td class="totalScore"></td>

	 	</tr>
	 	 <tr>
	 		<td>Bonus</td>
	 		<td>Score 35 </td>
	 		<td class="bonus"></td>
	 	</tr>
	 	<tr>
	 		<td colspan ='2'>Upper Total</td>
	 	
	 		<td class="upperScore"></td>
	 	</tr>
	 	 <tr>
	 		<th colspan="3">Lower Section</th>
	 	</tr>
	 	<tr>
	 		<td>3 of a Kind</td>
	 		<td>SUm of all dice</td>
	 		<td class="three_of_kind"></td>
	 	</tr>
	 		<tr>
	 		<td>4 of a kind</td>
	 		<td>Sum of all dice</td>
	 		<td class="four_of_kind"></td>
	 	</tr>
	 		<tr>
	 		<td>Full House</td>
	 		<td>Score 35</td>
	 		<td class="fullhouse"></td>
	 	</tr>
	 		<tr>
	 		<td>sm. straight</td>
	 		<td>Score 30</td>
	 		<td class="sm"></td>
	 	</tr>
	 		<tr>
	 		<td>lg. Straight</td>
	 		<td>score 40</td>
	 		<td class="lg"></td>

	 	</tr>
	 	<tr>
	 		<td>Yahtzee</td>
	 		<td>Score yatzee</td>
	 		<td class="yatzee"></td>
	 	</tr>
	 	<tr>
	 		<td>Chance</td>
	 		<td>sum of all dice</td>
	 		<td class="chance"></td>
	 	</tr>
	 	 <tr>
	 		<td colspan ='2'>Lower Total</td>
	 	
	 		<td class="lowerScore"></td>
	 	</tr>
	 	 <tr>
	 		<td colspan ='2'>Upper Total</td>
	 	
	 		<td class="UpperScore"></td>
	 	</tr>
	 	 <tr>
	 		<td colspan ='2'>Combined Total</td>
	 	
	 		<td class="combinedScore"></td>
	 	</tr>
	 	<section class="dice">
		 	<div class="die die1"></div>
		    <div class="die die2"></div>
		    <div class="die die3"></div>
		 	<div class="die die4"></div>
		 	<div class="die die5"></div>
		 	
		 		
	 	</section>
	 	<button class="button">ROll</button>
	 	
	


	 </table>
</div> <!--  board div -->

</body>
</html>

AnswerRe: looping through an array that has value based on a dice roll. Need to properly calculate the rolls. Pin
Richard MacCutchan11-Feb-17 20:45
mveRichard MacCutchan11-Feb-17 20:45 
GeneralRe: looping through an array that has value based on a dice roll. Need to properly calculate the rolls. Pin
Member 1299542012-Feb-17 12:05
Member 1299542012-Feb-17 12:05 
GeneralRe: looping through an array that has value based on a dice roll. Need to properly calculate the rolls. Pin
Richard MacCutchan12-Feb-17 23:05
mveRichard MacCutchan12-Feb-17 23:05 
Questionif (window.location.href.toLowerCase is not working in a Sharepoint site. Pin
RameshLuked9-Feb-17 23:08
RameshLuked9-Feb-17 23:08 
AnswerRe: if (window.location.href.toLowerCase is not working in a Sharepoint site. Pin
Afzaal Ahmad Zeeshan9-Feb-17 23:33
professionalAfzaal Ahmad Zeeshan9-Feb-17 23:33 
AnswerRe: if (window.location.href.toLowerCase is not working in a Sharepoint site. Pin
ZurdoDev10-Feb-17 1:17
professionalZurdoDev10-Feb-17 1:17 
GeneralRe: if (window.location.href.toLowerCase is not working in a Sharepoint site. Pin
Richard Deeming10-Feb-17 2:37
mveRichard Deeming10-Feb-17 2:37 
GeneralRe: if (window.location.href.toLowerCase is not working in a Sharepoint site. Pin
ZurdoDev10-Feb-17 3:04
professionalZurdoDev10-Feb-17 3:04 
GeneralRe: if (window.location.href.toLowerCase is not working in a Sharepoint site. Pin
Karthik_Mahalingam10-Feb-17 4:53
professionalKarthik_Mahalingam10-Feb-17 4:53 
AnswerRe: if (window.location.href.toLowerCase is not working in a Sharepoint site. Pin
Richard Deeming10-Feb-17 2:42
mveRichard Deeming10-Feb-17 2:42 
QuestionNeed Help Please:Trim function Pin
SpeedxLeopardPro7-Feb-17 11:53
SpeedxLeopardPro7-Feb-17 11:53 
AnswerRe: Need Help Please:Trim function Pin
Richard MacCutchan7-Feb-17 21:55
mveRichard MacCutchan7-Feb-17 21:55 
AnswerRe: Need Help Please:Trim function Pin
Karthik_Mahalingam10-Feb-17 4:56
professionalKarthik_Mahalingam10-Feb-17 4:56 
QuestionBSON Pin
Nathan Minier6-Feb-17 11:32
professionalNathan Minier6-Feb-17 11:32 
Questiontrying to calculate a retirement date by adding 66 years on to a Date of birth value Pin
Barnsley71-Feb-17 5:50
Barnsley71-Feb-17 5:50 
AnswerRe: trying to calculate a retirement date by adding 66 years on to a Date of birth value Pin
Richard MacCutchan1-Feb-17 6:40
mveRichard MacCutchan1-Feb-17 6:40 
AnswerRe: trying to calculate a retirement date by adding 66 years on to a Date of birth value Pin
bVagadishnu1-Feb-17 6:42
bVagadishnu1-Feb-17 6:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.