www.webdeveloper.com
Results 1 to 5 of 5

Thread: If..else statement doesn't work correctly

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    7

    If..else statement doesn't work correctly

    Hello,

    I have a problem with an if.. else statement:
    Code:
    if (puntenspeler1 > puntenspeler2, puntenspeler3, puntenspeler4){
    document.getElementById("verliezer").innerHTML="<b>"+speler1+"</b> heeft verloren!";
    }
    else{
    	if((puntenspeler1 == puntenspeler2) && (puntenspeler1 > puntenspeler4, puntenspeler3 )){
    	document.getElementById("verliezer").innerHTML="<b>"+speler1+"</b> en <b>"+speler2+"</b> hebben verloren, jullie mogen samen de taak uitvoeren.";
    	}
    	else{
    		if((puntenspeler1 == puntenspeler3) && (puntenspeler1 > puntenspeler4, puntenspeler2)){
    		document.getElementById("verliezer").innerHTML="<b>"+speler1+"</b> en <b>"+speler3+ "</b> hebben verloren, jullie mogen samen de taak uitvoeren.";
    		}
    		else{
    			if((puntenspeler1 == puntenspeler4) && (puntenspeler1 > puntenspeler3, puntenspeler2)){
    			document.getElementById("verliezer").innerHTML="<b>"+speler1+"</b> en <b>"+speler4+ "</b> hebben verloren, jullie mogen samen de taak uitvoeren.";
    			}
    			else{
    				if (puntenspeler2 > puntenspeler1, puntenspeler3, puntenspeler4){
    				document.getElementById("verliezer").innerHTML="<b>"+speler2+"</b> heeft verloren!";
    				}
    				else{
    					if((puntenspeler2 == puntenspeler3) && (puntenspeler2 > puntenspeler1, puntenspeler4)){
    					document.getElementById("verliezer").innerHTML="<b>"+speler2+"</b> en <b>"+speler3+"</b> hebben verloren, jullie mogen samen de taak uitvoeren.";
    					}
    					else{
    						if((puntenspeler2 == puntenspeler4) && (puntenspeler2 > puntenspeler1, puntenspeler3)){
    						document.getElementById("verliezer").innerHTML="<b>"+speler2+"</b> en <b>"+speler4+ "</b> hebben verloren, jullie mogen samen de taak uitvoeren.";
    						}
    						else{
    							if (puntenspeler3 > puntenspeler1, puntenspeler2, puntenspeler4){
    							document.getElementById("verliezer").innerHTML="<b>"+speler3+"</b> heeft verloren!";
    							}
    							else{
    								if(puntenspeler3 == puntenspeler4 && (puntenspeler3 > puntenspeler1, puntenspeler2)){
    								document.getElementById("verliezer").innerHTML="<b>"+speler3+"</b> en <b>"+speler4+"</b> hebben verloren, jullie mogen samen de taak uitvoeren.";
    								}
    								else{
    									document.getElementById("verliezer").innerHTML="<b>"+speler4+"</b> heeft verloren!";
    									
    									}
    								}																	
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    puntenspeler1, puntenspeler2, puntenspeler3 and puntenspeler4 are variables, they contain a certain score. With this statement, I want to print the name(variables: speler1, speler2, etc.) into a <p>tag. The printing works fine but the statement doesn't choose the right one, it should be the one with the highest score, but most of the time it just picks player one..

    I already tried to turn
    Code:
    if((puntenspeler1 == puntenspeler2) && (puntenspeler1 > puntenspeler4, puntenspeler3 ))
    into:
    Code:
    if((puntenspeler1 == puntenspeler2) && (puntenspeler1 > puntenspeler4 || puntenspeler1 > puntenspeler3 ))
    That didn't work either. I hope someone can help me with this.

  2. #2
    Join Date
    Jun 2010
    Location
    New York
    Posts
    2
    eduard,

    i haven't reviewed your whole code to try to troubleshoot it, but i believe you could go a long way by utilizng the 'sort' function. take a look at this simple use of the sort function - it takes the input from 4 text fields, places them in an array, sorts them, and outputs the sorted array ( from highest to lowest) in the p tag below it.
    see example

    you can view the source to get it all, but the javascript is this:
    Code:
     function sorts() {
    	
    	var aaa = document.getElementById("aa").value;
    	var bbb = document.getElementById("bb").value;
    	var ccc = document.getElementById("cc").value;
    	var ddd = document.getElementById("dd").value;
    	var points = [aaa,bbb,ccc,ddd];
    	points.sort(function(a,b){return b-a});
    	var x=document.getElementById("pp");
    	x.innerHTML=points;
     }
    the text fields and p tag look like this:
    Code:
    a: <input type="text" id="aa"><br />
    b: <input type="text" id="bb"><br />
    c: <input type="text" id="cc"><br />
    d: <input type="text" id="dd"><br />
    <input type="submit" value="Submit" onclick="sorts()">
    
    <p id="pp"> see the results here</p>
    i hope that can help you on your way.

  3. #3
    Join Date
    Nov 2012
    Posts
    7
    Thank you for you reply, tdemetri.

    Unfortunately, I still can't make it show the player(Or players) with the highest score.. It seems to me that it would take the same ifelse after this.. Or is there a simple way to show only the one on the left? I don't want to display any scores further..

  4. #4
    Join Date
    Jun 2010
    Location
    New York
    Posts
    2
    i truly don't understand where you are getting the players, or their values(scores), from.

    however, you can play with the follwing code ( based on your original question's conditions), and see that these statments will trace out correctly depending onthe variable values. change the values around to see

    Code:
    var p1 = 5;
    var p2 = 6;
    var p3 = 4;
    var p4 = 7;
    
    if((p1 == p2) && (p1 > p4 )) {alert("1")};
    if((p1 == p2) && (p1 > p4 || p1 > p3 )) {alert("2")};
    if((p1 == p2) || (p1 > p4 || p1 > p3 )) {alert("3")};
    also, check your original code:
    Code:
    if((puntenspeler1 == puntenspeler2) && (puntenspeler1 > puntenspeler4, puntenspeler3 ))
    , puntenspeler3 inthe statement is wrong coding. it could still return a value, but it could also cause problems

  5. #5
    Join Date
    Nov 2012
    Posts
    14
    You have

    if() {
    } else {
    if() {
    } else
    ...

    It should be

    if () {
    } else if () {
    } else if () {
    } etc.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles