css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">
    <h1>Rock, Paper, Scissors, Lizard, Spock</h1>

    <h2 class="text-center">Make your choice:</h2>
    <div class="choices row">
        <div class="col-xs-4 col-md-2 col-md-offset-1">
            <div id="rock" class="choice">
                Rock
            </div>
        </div>
        <div class="col-xs-4 col-md-2">
            <div id="paper" class="choice">
                Paper
            </div>
        </div>
        <div class="col-xs-4 col-md-2">
            <div id="scissors" class="choice">
                Scissors
            </div>
        </div>
        <div class="col-xs-4 col-xs-offset-2 col-md-2 col-md-offset-0">
            <div id="lizard" class="choice">
                Lizard
            </div>
        </div>
        <div class="col-xs-4 col-md-2">
            <div id="spock" class="choice">
                Spock
            </div>
        </div>
    </div>

    <div id="choices" class="row" style="display:none;">
        <div class="col-md-6 col-md-offset-3">
            <p><strong>Your Choice:</strong> <span id="your-choice"></span></p>
            <p><strong>Computer's Choice:</strong> <span id="comp-choice"></span></p>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="alert" id="last-result" style="display:none;">
                <h2 class="text-center"><strong id="result"></strong></h2>
                <p class="text-center"><span id="explanation"></span></p>
            </div>
        </div>
    </div>


    <h2 id="score" style="display:none;">Overall Score:<br/><span id="your-score">0</span> - <span id="computer-score">0</span> - <span id="tie-score">0</span></h2>
</div>
            
          
!
            
              * {
    box-sizing:border-box;
}
body {
    font-size:16px;
    font-family:Arial, sans-serif;
    text-align:center;
}
.container {
    padding-top:30px;
}
h1 {
    margin: 0 0 20px;
  	font-weight:bold;
}
h2 {
    margin:0 0 1em;
    line-height:1.5em;
}
h2 span {
    font-weight:normal;
}
.choices {
    margin-bottom:20px;
}
.choice {
    width:100%;
    height:150px;
    line-height:150px;
    border:1px solid #aaa;
    background:#eee;
    border-radius:4px;
    cursor:pointer;
}
.choice:hover {
    background:#ddd;
}
.choice:active {
    background:#ccc;
    border-color:#ccc;
}

dt {
    margin-bottom:15px;
}

#last-result {
    margin:20px 0;
}
#last-result h2 {
  line-height:1em;
  margin-bottom:15px;
}
@media screen and (max-width: 992px) {
  .choice {
  	width:75px;
    height:75px;
    line-height:75px;
    border-radius:37.5px;
    margin:0 auto 20px;
  }
}
            
          
!
            
              var userChoice;
var computerChoice;
var yourScore = 0;
var computerScore = 0;
var tieScore = 0;
var outcome = '';

function computer(){
    computerChoice = Math.random();
    if (computerChoice < 0.2) {
        computerChoice = "rock";
    } else if(computerChoice <= 0.4) {
        computerChoice = "paper";
    } else if(computerChoice <= 0.6) {
        computerChoice = "scissors";
    } else if(computerChoice <= 0.8) {
        computerChoice = "lizard";
    }else {
        computerChoice = "spock";
    } console.log("Computer: " + computerChoice);
}

function winner() {
    document.getElementById('result').innerHTML = "You win!";
    $('#last-result').addClass('alert-success');
    $('#last-result').removeClass('alert-danger');
    $('#last-result').removeClass('alert-warning');
    yourScore++;
}
function loser() {
    document.getElementById('result').innerHTML = "Computer wins...";
    $('#last-result').addClass('alert-danger');
    $('#last-result').removeClass('alert-success');
    $('#last-result').removeClass('alert-warning');
    computerScore++;
}
function tie() {
    document.getElementById('result').innerHTML = "The result is a tie!";
    $('#last-result').addClass('alert-warning');
    $('#last-result').removeClass('alert-danger');
    $('#last-result').removeClass('alert-success');
    tieScore++
}

function compare(choice1, choice2){

    if( choice1 === choice2 ){
        tie();
        outcome = 'Gee, maybe you should be more original...';
    }
    else if( choice1 === "rock" ){
        if( choice2 ==="scissors" ){
            outcome = 'Rock crushes Scissors';
            winner();
        } else if(choice2 === "lizard") {
            outcome ='Rock crushes Lizard';
            winner();
        } else if(choice2 === "paper"){
            outcome = 'Rock is covered by Paper';
            loser();
        } else if(choice2 === "spock"){
            outcome = 'Rock is vaporized by Spock';
            loser();
        }
    }
    else if ( choice1 === "paper" ){
        if( choice2 === "rock" ) {
            outcome = 'Paper covers Rock';
            winner();
        } else if(choice2 === "spock") {
            outcome = 'Paper disproves Spock';
            winner();
        } else if(choice2 === "scissors"){
            outcome = 'Paper is cut by Scissors';
            loser();
        } else if(choice2 === "lizard"){
            outcome = 'Paper get eaten by Lizard';
            loser();
        }
    }
    else if ( choice1 === "scissors" ){
        if( choice2 === "paper" ) {
            outcome = 'Scissors cut Paper';
            winner();
        } else if(choice2 === "lizard") {
            outcome = 'Scissors decapitate Lizard';
            winner();
        } else if(choice2 === "rock"){
            outcome = 'Scissors get crushed by Rock';
            loser();
        }  else if(choice2 === "spock"){
            outcome = 'Scissors get smashed by Spock';
            loser();
        }
    }
    else if ( choice1 === "lizard" ){
        if( choice2 === "paper" ) {
            outcome = 'Lizard eats Paper';
            winner();
        } else if(choice2 === "spock") {
            outcome = 'Lizard poisons Spock';
            winner();
        } else if(choice2 === "scissors") {
            outcome = 'Lizard gets a little off the top from Scissors';
            loser();
        } else if(choice2 === "rock") {
            outcome ='Lizard gets smooshed. Poor Lizard...';
            loser();
        }
    }
    else if ( choice1 === "spock" ){
        if( choice2 === "scissors" ) {
            outcome = 'Spock smashes Scissors';
            winner();
        } else if(choice2 === "rock") {
            outcome = 'Spock vaporizes Rock';
            winner();
        } else if(choice2 === "lizard") {
            outcome = 'Spock get poisoned by the Lizard';
            loser();
        } else if(choice2 === "paper") {
            outcome = 'Spock gets disproven by Paper';
            loser();
        }
    } else {
        document.getElementById('result').innerHTML = "Nice try wise guy. How about you enter a valid choice?";
    }
}


$(document).on('click','.choice', function(event) {
    userChoice = $(this).attr('id');
    makeChoice();
});

function makeChoice(){
    computer();
    compare(userChoice, computerChoice);
    document.getElementById('your-choice').innerHTML = userChoice;
    document.getElementById('comp-choice').innerHTML = computerChoice;
    document.getElementById('explanation').innerHTML = outcome;
    document.getElementById('choices').style.display = 'block';
    document.getElementById('last-result').style.display = 'block';
    document.getElementById('score').style.display = 'block';
    document.getElementById('your-score').innerHTML = yourScore;
    document.getElementById('computer-score').innerHTML = computerScore;
    document.getElementById('tie-score').innerHTML = tieScore;
}




            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console