Link to home
Create AccountLog in
Avatar of Dolamite Jenkins
Dolamite JenkinsFlag for United States of America

asked on

reset my variables to 0

I'm trying to reset my variables to 0 after a certain condition has been met... but it just keeps on counting without resetting to 0 after the condition is met ... what am I doing wrong ?



var Player1SetScore= 0;
var Player2SetScore= 0;
var PW =0;
var PointsWonII = 0;

PointWonBtn.addEventListener("click", function(e){
	PW =PW;
	PointsWonII = PointsWonII + 1;
			
	PointsWinner( PW, PointsWonII );
});
//	
PointLostBtn.addEventListener("click", function(e){
	PW = PW + 1;
	PointsWonII = PointsWonII;
	PointsWinner(PW, PointsWonII);
});

var Player1 =  PointsWonII;
	var Player2 = PW;

}else if (Player1 >=4 && Player2 <=2)
		{ScoreSelf.text='game';
		ScoreOpp.text ='Lost';
		
		var Player1=0;
		var Player2=0;
		Ti.API.info(Player1);
		PW =0;
		PointsWonII=0;
	

Open in new window

Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

There is code pertinent to debugging missing.
Avatar of Dolamite Jenkins

ASKER

Line 29 & 30 is where I am trying to reset line 7 & 14 after that particular condition is met ... why isn't line 29 & 30 resetting 7&14 to 0 ?
Avatar of leakim971
just before your test (the if, not the else if of course!) put this alert :
 
alert("Player1 :" + Player1 + "\nPlayer2 :" + Player2 + "\nPlayer1SetScore :" + Player1SetScore + "\nPlayer2SetScore :" + Player2SetScore + "\nPW :" + PW);
I did all of that. And it resets. The variable there but when I click the button again. It. Continues from 4...5...6..7... etc
Line 26, 27 you're creating local variable, not using the global
so remove the var keyword
I tried this code  and it did not work


}else if (Player2 > 3 && Player1 <=2)
		{ScoreSelf.text ="lost";
		ScoreOpp.text= "game";
		PW = 0 ;
		PointsWonII = 0;

Open in new window

I said remove the var keyword
You removed the full lines...
let :
		Player1=0;
		Player2=0;

Open in new window

I did that because I realized I didn't need those resetting and where left over from an earlier attempt ... I need PW = 0; && PointsWonII=0;
so how do you it stop looping/increase/decrease?
you just remove the only lines able to stop your increase/decrease, say nothing about that, so where are we going now?

>:o(
Here is the whole section... if you push the PointWonBtn (Button) then PointsWonII increases.. and Points Lost Button PW increases ...once the score reaches 4 for one or the other I need it to start  the score over hence PW = 0 and PointsWonII = 0;




var PW =0;
var PointsWonII =0;

// Create UI i this area


//Logic area 
PointWonBtn.addEventListener("click", function(e){
	Ti.API.info('Pw=: ' + PW);
	PW =PW;
	PointsWonII = PointsWonII + 1;
		
	PointsWinner( PW, PointsWonII );
});
//	
PointLostBtn.addEventListener("click", function(e){
	Ti.API.info('Lost ButPW= : ' + PW);
	PW = PW + 1;
	PointsWonII = PointsWonII;
	PointsWinner(PW, PointsWonII);
});
var PointsWinner = function(PW, PointsWonII){
	
	var Player1 =  PointsWonII;
	var Player2 = PW;
	var Adv=0;
	Ti.API.info('P1 ' + Player1);
	Ti.API.info('P2 ' + Player2);
	Ti.API.info('Space');
	if (Player2==1 && Player1 == 0){
			ScoreSelf.text ='';
		ScoreOpp.text = '15';
			
		}else if (Player2 == 0 && Player1 == 0){
			ScoreSelf.text ='';
			ScoreOpp.text = '';
			
		}else if (Player1==1 && Player2 == 0){
			ScoreSelf.text ='15';
		ScoreOpp.text = '';
			
		}else if(Player1 ==2 && Player2 ==0){
			ScoreSelf.text ='30';
		ScoreOpp.text = '';
		}else if (Player1 ==3 && Player2==0){
			ScoreSelf.text ='40';
		ScoreOpp.text = '';
		}
		else if(Player2 == 1 && Player1 == 2){
			ScoreSelf.text ='30';
		ScoreOpp.text = '15';
		}else if (Player2 == 1 && Player1 == 1)
		{ScoreSelf.text = "15";
		ScoreOpp.text ='15';
		}else if (Player2 ==2 && Player1 == 2)
		{ScoreSelf.text = "30";
		ScoreOpp.text ='30';
		}else if (Player2==2 && Player1 == 3)
		{ScoreSelf.text ='40';
		ScoreOpp.text = '30';
		}else if (Player1==2 && Player2 == 3)
		{ScoreSelf.text ='30';
		ScoreOpp.text = '40';
		}else if (Player2==3 && Player1 == 3)
		{ScoreSelf.text = "Duece";
		ScoreOpp.text ='Duece';
		}else if (Player2==2 && Player1 == 0)
		{ScoreSelf.text ='0';
		ScoreOpp.text = '30';
		}else if (Player1==0 && Player2 == 2)
		{ScoreSelf.text ='love';
		ScoreOpp.text = '30';
		}else if (Player2==3 && Player1 == 0)
		{ScoreSelf.text ='0';
		ScoreOpp.text = '40';
		}else if (Player1== 0 && Player2 == 3)
		{ScoreSelf.text ='love';
		ScoreOpp.text = '40';
		}else if (Player2==2 && Player1 == 1)
		{ScoreSelf.text ='15';
		ScoreOpp.text = '30';
		}else if (Player1==1 && Player2 == 2)
		{ScoreSelf.text ='15';
		ScoreOpp.text = '30';
		}else if (Player2==3 && Player1 == 1)
		{ScoreSelf.text ='15';
		ScoreOpp.text = '40';
		}else if (Player1 ==3 && Player2==1){
			ScoreSelf.text ='40';
		ScoreOpp.text = '15';
		}else if (Player1==1 && Player2 == 3)
		{ScoreSelf.text ='15';
		ScoreOpp.text = '40';
		}else if (Player1 >=4 && Player2 <=2)
		{ScoreSelf.text='game';
		ScoreOpp.text ='Lost';
		PW = (PW=0);
		PointsWonII==0;
		Player1SetScore= Player1SetScore + 1;
		Player2SetScore= Player2SetScore;
		GameScore(Player1SetScore , Player2SetScore);
		
		}else if (Player2 > 3 && Player1 <=2)
		{ScoreSelf.text ="lost";
		ScoreOpp.text= "game";
		PW = (PW=0);
		PointsWonII == 0;
		
		Ti.API.info('PW= ' + PW);
		}else if (Player2 ==4 && Player1 ==3 ){
			ScoreSelf.text="";
			ScoreOpp.text='AD';
			
			
		}else if (Player2==5 && Player1 ==3  ){
			ScoreSelf.text='';
			ScoreOpp.text = "Game";
		 	PW =0;
			PointsWonII=0;
	}else if (Player1 == 4 && Player2 == 4){
		ScoreSelf.text="Duece";
			ScoreOpp.text='Duece';
			Player1=3;
			Player2=3;
			PW =3;
			PointsWonII=3;
		
	}else if (Player2 ==3 && Player1 ==4 ){
			ScoreSelf.text="AD";
			ScoreOpp.text='';
			
		}else if (Player2==3 && Player1 ==5  ){
			ScoreSelf.text='Game';
			ScoreOpp.text = "";
		 	PW =0;
			PointsWonII=0;
	}else if (Player2==3 && Player1 == 2)
		{ScoreSelf.text ='40';
		ScoreOpp.text = '30';
		}
		

}

Open in new window

;
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
is there another way to handle it because it's a tennis scoring so sometime in certain condition the score can equal 5 or 6
Do it work for 4? It was your previous request...
does not work... keep counting on from 4
And this one ?
var PointsWinner = function(PW, PointsWonII){
       if( (PW+1) == 4 ) {
             PW = 0;
             PointsWonII = 0;
             return;
       } 

Open in new window

Thanks you got me thinking in a new direction... I incremented in the function
var PointsWinner = function(PW, PointsWonII){
       if( ((PW+1) >= 4) && (PW - PointsWonII)>=2  ) {
             PW = 0;
             PointsWonII = 0;
             return;
       } 

Open in new window