Back to Projects
4.3 - Rock, Paper, Scissors (Solution)
“Run” to see new changes
✕
Look inside
Run
main.js
index.html
style.css
×
×
Run
// Step 1: Setup Choices const choices = ["🪨", "📄", "✂️"]; // Step 2: Get Elements const rockBtn = document.getElementById("rockBtn"); const paperBtn = document.getElementById("paperBtn"); const scissorsBtn = document.getElementById("scissorsBtn"); const playerMove = document.getElementById("playerMove"); const computerMove = document.getElementById("computerMove"); const result = document.getElementById("result"); // Step 3: Create the Game Function function playGame(playerChoice) { const computerChoice = choices[Math.floor(Math.random() * 3)]; playerMove.textContent = "You chose: " + playerChoice; computerMove.textContent = "Computer chose: " + computerChoice; // Step 3: Compare choices and display result if ( (playerChoice === "🪨" && computerChoice === "✂️") || (playerChoice === "📄" && computerChoice === "🪨") || (playerChoice === "✂️" && computerChoice === "📄") ) { result.textContent = "You win! 🎉"; } else if (playerChoice === computerChoice) { result.textContent = "It's a tie! 🤝"; } else { result.textContent = "You lose! 😢"; } } // Step 4: Connect Buttons rockBtn.onclick = function() { playGame("🪨"); }; paperBtn.onclick = function() { playGame("📄"); }; scissorsBtn.onclick = function() { playGame("✂️"); };
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css"> <link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet"> <script src="main.js"></script> </head> <body> <div class="container"> <h2>Rock, Paper, Scissors</h2> <p>Choose your move:</p> <button id="rockBtn">🪨</button> <button id="paperBtn">📄</button> <button id="scissorsBtn">✂️</button> <p id="playerMove"></p> <p id="computerMove"></p> <p id="result"></p> </div> </body> </html>
body { background: linear-gradient(to right, #0f0c29, #302b63, #24243e); /* arcade night gradient */ font-family: 'Press Start 2P', cursive; margin: 0; padding: 0; color: #ffffff; text-shadow: 1px 1px 2px #000; } .container { background-color: #111; max-width: 500px; margin: 60px auto; padding: 40px 20px; border-radius: 12px; box-shadow: 0 0 20px #f72585, 0 0 40px #3a0ca3; text-align: center; border: 2px solid #f72585; } h2 { margin-bottom: 20px; font-size: 20px; color: #f72585; } p { font-size: 12px; margin-top: 20px; color: #ffe600; } button { font-size: 36px; padding: 12px 20px; margin: 12px; border: 2px solid #00f5d4; border-radius: 8px; background-color: #03045e; color: #00f5d4; cursor: pointer; transition: all 0.2s ease-in-out; box-shadow: 0 0 10px #00f5d4; } button:hover { background-color: #00f5d4; color: #03045e; transform: scale(1.1); box-shadow: 0 0 15px #90e0ef; }
Console
AI Tutor
Documentation
Ask AI Tutor
Click RUN to execute project