What is wrong with this code? Help needed.

Discussion in 'JavaScript' started by Lemon116, Apr 28, 2007.

  1. #1
    Well I have just written this code, Ill post it here and the question is after the code, please help me.

    <script type="text/javascript">
    var a1=(5)
    var a2=(3)
    if (a1=Math.max)
    {
    a1--
    document.write("google")
    }
    else if (a2=Math.max)
    {
    a2--
    document.write("yahoo")
    }
    else
    {
    document.write("DigitalPoint")
    }
    </script>


    There is some problem with this code, What I want this code to do?

    As you can see there are var' a1 and a2, both has values a1=5, a2=3
    I want the system to choose the var' with the highest value and show the text which is assigned to this var, If a1 chosen show "Google", if a2 "yahoo",else "Digitalpoint".
    Once the var is chosen and text is showen the var' value will be lowered by (one).

    At first it shall choose a1 with value (5) and show "google" than a1 value will be lowered to (4) and again show "google" than both values a1 and a2 will be (3) it will choose a random one since the values are same and no Maximum
    and so on... until both values get to (0) and than will be showen the (else) "DigitalPoint".

    Where is the problem here? :confused:
    Hope you read and understood it.
    Need your Help ASP . :(
     
    Lemon116, Apr 28, 2007 IP
  2. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #2
    
    <script type="text/javascript">
    	
    	var a1 = 5;
    	var a2 = 3;
    	
    	while(a1 && a2)
    	{
    		if(a1 > a2)
    		{
    			a1--;
    			document.write("google");
    		}
    		else if(a1 < a2)
    		{
    			a2--;
    			document.write("yahoo");
    		}
    		else
    		{
    			if(Math.round(Math.random()))
    			{
    				a1--;
    				document.write("google");
    			}
    			else
    			{
    				a2--;
    				document.write("yahoo");
    			}
    		}
    	}
    	
    	document.write("DigitalPoint");
    	
    </script>
    
    HTML:
     
    SoKickIt, Apr 28, 2007 IP
  3. Lemon116

    Lemon116 Active Member

    Messages:
    1,245
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    90
    #3
     
    Lemon116, Apr 28, 2007 IP
  4. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #4
    Math.max takes only two arguments so you can't use it. Create an array instead of using variables. Then you just have to find the maximum element in an array..
     
    SoKickIt, Apr 29, 2007 IP
  5. Lemon116

    Lemon116 Active Member

    Messages:
    1,245
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Ok Ill try thx:)
     
    Lemon116, Apr 29, 2007 IP