mardi 17 septembre 2019

Javascript if/else not working. Odin project's Rock, paper, scissors

I’ve been trying to solve the Rock, Paper and Scissors project but I can’t figure out how to make the if/else statement. I did it a lot of times and finally I think I’m near to solve the issue, but the thing is, every time I run the program I get the wrong output.

For example, I used ‘Paper’ and the computer used ‘Rock’, but the console showed that ‘It’s a tie’, but in the code I wrote that if player chose paper and computer chose rock the message should have been ‘You win. Paper beats Rock’. The same happens when I choose ‘Paper’ and the computer chooses ‘Scissors’.

I used toLowerCase() method but after many changes, even when remove it and write exactly the words in the if/else this does not appear to be the problem.

What do I need to correct? Thank you so much!!!

This is the code:

<!DOCTYPE html>
<html>
    <head>
        <title>Rock Paper Scissors!</title>
    </head>

    <body>
        <script>

            //Computer's selection



            function computerPlay(){


                let selectRandom = Math.floor(Math.random()*3);
                if(selectRandom === 0){
                    return 'Rock';
                }else if (selectRandom === 1){
                    return 'Paper';
                }else{
                    return 'Scissors';
                }


            }


            console.log('Computer chose: '+ computerPlay());


            //Play round between humand and computer

            function playRound(playerSelection, computerSelection) {

                //Change to lowercase
                let player = playerSelection.toLowerCase();
                let computer = computerSelection.toLowerCase();


                //If player chooses rock
                if (player === 'rock'){
                    if(computer === 'rock'){return 'It\'s a tie. Try again'}
                    else if(computer === 'paper'){return 'You loose. Paper beats Rock'}
                    else{return 'You win. Rock beats scissors'}

                }
                //If player chooses paper
                else if (player === 'paper'){
                    if(computer === 'paper'){return 'It\'s a tie. Try again'}
                    if(computer === 'scissors'){return 'You loose. Scissors beats Paper'}
                    else{return 'You win. Paper beats Rock'}
                }

                //If player chooses scissors
                else{ 
                    if(computer === 'scissors'){return 'It\'s a tie. Try again'}
                    else if(computer === 'rock'){return 'You loose. Rock beats Scissors'}
                    else{return 'You win. Scissors beats Paper'}
                }

            }

            const playerSelection = 'Paper'; 
            const computerSelection = computerPlay();
            console.log(playRound(playerSelection, computerSelection));




        </script>


    </body>
</html>

Aucun commentaire:

Enregistrer un commentaire