Jump to content

Help with a part of code - working with strings

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Joeyeti

Joeyeti

    Newbie

  • Members
  • Pip
  • 4 posts
Hi all,

I trying to get the following code working... no success yet, so I suspect a formal error in the code somewhere.

<html> 


<head> 

<script> 

var czUspresence as int 

var czUscontrol as int 

var czUssrpresence as int 

var czUssrcontrol as int 


function euGetstatus(){ 

   var czUs = document.getElementById("cze_us").value; 

   var czUssr = document.getElementById("cze_ussr").value; 

   var sumCz = parseInt(czUs,10) - parseInt(czUssr,10) 

    

   if (sumCz < -2) 

   {czUssrcontrol = 1 ; 

   czUscontrol = 0; 

   czUssrpresence = 1; 

      if (czUs > 0) 

      {czUspresence = 1; 

      } 

   } 

   else if (sumCz > 2) 

   {czUscontrol = 1; 

   czUssrcontrol = 0; 

   czUspresence = 1; 

      if (czUssr > 0) 

      {czUssrpresence = 1; 

      } 

   } 

   else if (sumCz <> 0) 

      {if (czUs>czUssr) 

      {czUspresence = 1; 

      } 

      else 

      czUssrpresence = 1; 

      } 

   } 

    

   document.getElementById("euUsregular").value = sumUspresence; 

   document.getElementById("euUsbattleground").value = czUscontrol; 

   document.getElementById("euUssrregular").value = czUssrpresence; 

   document.getElementById("euUssrbattleground").value = czUssrcontrol; 

} 


</script> 

</head> 


<body> 


<select style="position:absolute; left:919px; top:286px; background-color: blue; color: white;" id="cze_us" class="us" onchange="euGetstatus();"> 

<option selected>0 <option>1 <option>2 <option>3 <option>4 <option>5 <option>6 <option>7 <option>8 <option>9 <option>10 <option>11 

</select> 


<select style="position:absolute; left:956px; top:286px; background-color: red; color: white;" id="cze_ussr" class="ussr" onchange="euGetstatus();"> 

<option selected>0 <option>1 <option>2 <option>3 <option>4 <option>5 <option>6 <option>7 <option>8 <option>9 <option>10 <option>11 

</select> 


<input type="text" id="euUsregular" style="position:absolute; left:550px; top:150px;  background-color: cyan; color: black" readonly=true size="2" value="" align="center"/> 

<input type="text" id="euUsbattleground" style="position:absolute; left:550px; top:180px;  background-color: cyan; color: black" readonly=true size="2" value="" align="center"/> 

<input type="text" id="euUssrregular" style="position:absolute; left:600px; top:150px;  background-color: pink; color: black" readonly=true size="2" value="" align="center"/> 

<input type="text" id="euUssrbattleground" style="position:absolute; left:600px; top:180px;  background-color: pink; color: black" readonly=true size="2" value="" align="center"/> 


</body> 

</html> 



Can someone help with finding the error and/or correcting any mistake in there?

What I am trying to do is to check the two values (cze_us and cze_ussr) to see, which one is bigger and also their difference. One of the breaking points is (-)3, where the respective country would get cz****control and also cz****presence equal to 1.

When the difference is less then 3 and a country has more than 0 (cze_us or cze_ussr), that country gets only cz****presence equal to 1.

If a country has its cze_us(sr) value equal to 0, it does not get any cz****presence or control.

Hope I am clear ;)

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Moved to the correct section. Please don't post questions in the tutorial areas.

It looks like your else is missing an opening bracket:


   else if (sumCz <> 0) 

      {if (czUs>czUssr) 

      {czUspresence = 1; 

      } 

      else 

      czUssrpresence = 1; 

      } 

   }


Notice the last else there? Your code is hard to read, you may want to rewrite it using a little more whitespace:


   else if (sumCz <> 0) 

   {

          if (czUs>czUssr) 

          {

               czUspresence = 1; 

           } 

           else

           { 

                czUssrpresence = 1; 

           } 

   }



#3
Joeyeti

Joeyeti

    Newbie

  • Members
  • Pip
  • 4 posts
Thx Jordan and sorry for posting wrong...

I played a little with my code, removed the unnecessary declarations and some other minor things and basically rewrote the IF..THEN part to simplify it.

It is below, just for the sake of the thing.

I will continue and ask whenever I get to something unknown ;)


function euGetstatus(){ 


	var czUs = document.getElementById("cze_us").value; 

	var czUssr = document.getElementById("cze_ussr").value; 

	var sumCz = parseInt(czUs) - parseInt(czUssr);

	

	if (sumCz < -2)

		{

		czUssrcontrol = 1 ;

		}

	else

		{

		czUssrcontrol = 0;

		}

	

	if (sumCz > 2)

		{

		czUscontrol = 1 ;

		}

	else

		{

		czUscontrol = 0;

		}

	

	if (czUs > 0)

		{

		czUspresence = 1 ;

		}

	else

		{

		czUspresence = 0;

		}

	

	if (czUssr > 0)

		{

		czUssrpresence = 1 ;

		}

	else

		{

		czUssrpresence = 0;

		}

	

	document.getElementById("euUsregular").value = czUspresence; 

	document.getElementById("euUsbattleground").value = czUscontrol;

	document.getElementById("euUssrregular").value = czUssrpresence;

	document.getElementById("euUssrbattleground").value = czUssrcontrol;

}