Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question scissors cant win vs paper :(

    Code:
    
    function ppt()
    {
    	
    
    var userChoice = prompt("Escoje piedra, papel o tijeras");
    
    var computerChoice = Math.random();
    
    if (computerChoice <0.34){
    
    computerChoice = "piedra";
    }else if(computerChoice <=0.67){
    computerChoice = "papel";
    }else{
    computerChoice = "tijeras";
    }
    
    
    
    function comparar(choice1,choice2){
    
    if(choice1===choice2){return alert("EMPATE!");}//empate
    
    
    if (choice1==="piedra"&&choice2=== "tijeras"){return alert("GANASTE");}
    else{return alert("PERDISTE!");}
    
    
    if (choice1==="papel"&&choice2=== "tijeras"){return alert("PERDISTE");}
    else{return alert("GANASTE!");}
    
    
    if (choice1==="tijeras"&&choice2=== "piedra"){return alert("PERDISTE!");}
    else{return alert("GANASTE");}
    
    
    
    }//fin funcion comparar
    
    comparar(userChoice,computerChoice);
    
    alert("Tu elejiste " +userChoice +" y la computadora elijio " +computerChoice); 
    
    
    
    
    }//fin de funcion ppt

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Your very first comparison
    Code:
    if (choice1==="piedra"&&choice2=== "tijeras"){return alert("GANASTE");}
    else{return alert("PERDISTE!");}
    means: Only return "GANASTE", if choice1 is piedra and choice2 is tijeras, in EVERY OTHER CASE it will return "PERDISTE" and will never do the other checks.

    Try this
    Code:
    if ((choice1==="piedra"&&choice2=== "tijeras")||(choice1==="papel"&&choice2=== "tijeras")||(choice1==="tijeras"&&choice2=== "piedra")){return alert("GANASTE");}
    else{return alert("PERDISTE!");}


  •  

    Posting Permissions

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