Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
  <h1>Rock Paper Scissors</h1>
  <h2>First to score 5 points wins the game!</h2>

  <main>
    <div id="ply-score-container">
      <h3>Player</h3>
      <div class="player-score"></div>
    </div>

    <div id="comtr-score-container">
      <h3>Computer</h3>
      <div class="computer-score"></div>
    </div>
    <h3>choose your weapon</h3>

    <div class="option">
      <button id="rock">rock</button>
      <button id="paper">paper</button>
      <button id="scissors">Scissors</button>
    </div>

    <section id="choice">
      <div>
        <h3>player's choice</h3>
        <div id="player-choice"></div>
      </div>
      <div>
        <h3>Computer's choice</h3>
        <div id="comp-choice"></div>
      </div>
    </section>
    <p id="show-text"></p>
  </main>
</body>
              
            
!

CSS

              
                body {
  margin: 0;
  padding: 0;
  background-color: peru;
  text-align: center;
}

h1,
h2,
h3 {
  text-transform: capitalize;
}

#ply-score-container,
#comtr-score-container {
  display: inline-block;
  margin: 10px;
}

button {
  font-size: 1.5rem;
}

#choice div {
  display: inline-block;
  margin: 10px;
}

              
            
!

JS

              
                let playerOptions = document.querySelectorAll(".option button");
let playerChoice = document.querySelector("#player-choice");
let compChoice = document.querySelector("#comp-choice");
let inform = document.querySelector("#show-text");
let playerPoint = document.querySelector(".player-score");
let compPoint = document.querySelector(".computer-score");

// to save the score
let playerScore = 0;
let computerScore = 0;

// make a function to get random choice from computer
function computerPlay() {
  let choice = ["rock", "paper", "scissors"];
  return choice[Math.floor(Math.random() * choice.length)];
}

// this will play the game till the score reaches 5 points and declare the final winner at the end
function playGame(e) {
  computerSelection = computerPlay();
  compChoice.textContent = computerSelection;
  playerChoice.textContent = e.target.id;

  if (playerScore < 5 && computerScore < 5) {
    switch (true) {
      case e.target.id === "rock" && computerSelection === "paper":
        return (
          playerScore,
          ++computerScore,
          (playerPoint.textContent = playerScore),
          (compPoint.textContent = computerScore),
          (inform.textContent = "You loose! paper beats rock")
        );

      case e.target.id === "rock" && computerSelection === "scissors":
        return (
          ++playerScore,
          computerScore,
          (playerPoint.textContent = playerScore),
          (compPoint.textContent = computerScore),
          (inform.textContent = "You win! rock beats scissors")
        );

      case e.target.id === "paper" && computerSelection === "rock":
        return (
          ++playerScore,
          computerScore,
          (playerPoint.textContent = playerScore),
          (compPoint.textContent = computerScore),
          (inform.textContent = "You win! paper beats rock")
        );

      case e.target.id === "paper" && computerSelection === "scissors":
        return (
          playerScore,
          ++computerScore,
          (playerPoint.textContent = playerScore),
          (compPoint.textContent = computerScore),
          (inform.textContent = "You loose! scissors beats paper")
        );

      case e.target.id === "scissors" && computerSelection === "rock":
        return (
          playerScore,
          ++computerScore,
          (playerPoint.textContent = playerScore),
          (compPoint.textContent = computerScore),
          (inform.textContent = "You loose! rock beats scissors")
        );

      case e.target.id === "scissors" && computerSelection === "paper":
        return (
          ++playerScore,
          computerScore,
          (playerPoint.textContent = playerScore),
          (compPoint.textContent = computerScore),
          (inform.textContent = "You win! scissors beats paper")
        );

      case e.target.id === computerSelection:
        return (
          playerScore,
          computerScore,
          (playerPoint.textContent = playerScore),
          (compPoint.textContent = computerScore),
          (inform.textContent = "it's a tie")
        );
    }
  }
  return getWinner();
}

playerOptions.forEach((option) => {
  option.addEventListener("click", playGame);
});

// declare the final winner
function getWinner() {
  if (playerScore > computerScore) {
    inform.textContent = "YEY!! you won the game";
  } else {
    inform.textContent = "OOH!! you lost the game";
  }
}

              
            
!
999px

Console