Play Rock Paper Scissors Online - Simple Game Code
// Intro console.log('Welcome to the Rock Paper Scissors game!');
// Create an infinite loop while (true) {
// Prompt the user to select Rock, Paper, or Scissors let userSelection = prompt('Please choose either Rock, Paper, or Scissors.');
// Convert the user's selection to lower case userSelection = userSelection.toLowerCase();
// Create a random number let randomNumber = Math.floor(Math.random() * 3);
// Create a variable to store the computer's selection let computerSelection = '';
// Assign the computer's selection based on the random number if (randomNumber === 0) { computerSelection = 'rock'; } else if (randomNumber === 1) { computerSelection = 'paper'; } else { computerSelection = 'scissors'; }
// Log the computer's selection console.log('Computer selection: ' + computerSelection);
// Check for a tie if (userSelection === computerSelection) { console.log('It's a tie!'); } else if (userSelection === 'rock') { if (computerSelection === 'paper') { console.log('Computer wins!'); } else { console.log('You win!'); } } else if (userSelection === 'paper') { if (computerSelection === 'scissors') { console.log('Computer wins!'); } else { console.log('You win!'); } } else if (userSelection === 'scissors') { if (computerSelection === 'rock') { console.log('Computer wins!'); } else { console.log('You win!'); } } else { console.log('Invalid selection. Please try again.'); } }
原文地址: https://www.cveoy.top/t/topic/lmnu 著作权归作者所有。请勿转载和采集!