You are on page 1of 1

Rock, Paper, Scissors: code for webpage

index.html:

htmlCopy code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta
name="viewport" content="width=device-width, initial-scale=1.0"> <title> Rock, Paper,
Scissors </title> <link rel="stylesheet" href="styles.css"> </head> <body> <div
class="container"> <h1> Rock, Paper, Scissors </h1> <div class="choices"> <button
onclick="makeChoice('rock')"> Rock </button> <button
onclick="makeChoice('paper')"> Paper </button> <button
onclick="makeChoice('scissors')"> Scissors </button> </div> <div id="result"></div>
</div> <script src="script.js"></script> </body> </html>

styles.css:

cssCopy code
body { font-family : 'Arial' , sans-serif; background-color : #f4f4f4 ; margin : 0 ; display :
flex; align-items : center; justify-content : center; height : 100vh ; } .container { text-align :
center; background-color : #fff ; padding : 20px ; border-radius : 8px ; box-shadow : 0 0
10px rgba ( 0 , 0 , 0 , 0.1 ); } .choices button { padding : 10px ; margin : 5px ; cursor :
pointer; } #result { font-size : 18px ; margin-top : 20px ; }

script.js:

jsCopy code
const choices = [ 'rock' , 'paper' , 'scissors' ]; function makeChoice ( playerChoice ) { const
computerChoice = getRandomChoice (); const result = determineWinner (playerChoice,
computerChoice); displayResult (playerChoice, computerChoice, result); } function
getRandomChoice () { const randomIndex = Math . floor ( Math . random () *
choices. length ); return choices[randomIndex]; } function determineWinner ( playerChoice,
computerChoice ) { if (playerChoice === computerChoice) { return 'It\'s a tie!' ; } if
( (playerChoice === 'rock' && computerChoice === 'scissors' ) || (playerChoice === 'paper'
&& computerChoice === 'rock' ) || (playerChoice === 'scissors' && computerChoice ===
'paper' ) ) { return 'You win!' ; } return 'Computer wins!' ; } function
displayResult ( playerChoice, computerChoice, result ) { const resultElement =
document . getElementById ( 'result' ); resultElement. textContent = `You chose $
{playerChoice}. Computer chose ${computerChoice}. ${result}` ; }

This code sets up a basic webpage where the user can click buttons to choose
rock, paper, or scissors. The computer's choice is randomly generated, and the
winner is determined based on the game rules. The result is displayed below
the buttons.

You might also like