Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const rock = document.getElementById('rock');
- const paper = document.getElementById('paper');
- const scissors = document.getElementById('scissors');
- const reload1 = document.getElementById('reload');
- reload1.addEventListener('click',(e) => {
- location.reload();
- })
- let gameRound = 0;
- let ComputerScore = 0;
- let PlayerScore = 0;
- let tie = 0;
- const computerChoices = [];
- const array1 = ['rock', 'paper', 'scissors'];
- //function for computer to guess
- function computerPlay(array1) {
- for( i = 0 ; i < 5 ; i ++ ){
- let randomElement = Math.floor(Math.random() * array1.length);
- computerChoices.push(array1[randomElement]);
- }}
- computerPlay(array1);
- computerChoices.forEach(element => {
- console.log(element);
- });
- //function to get the player choice
- function playerChoice(){
- for ( let i = 0 ; i < 5 ; i++ )
- {
- rock.addEventListener('click',function(){
- playRound('rock',computerChoices);
- });
- paper.addEventListener('click',function(){
- playRound('paper',computerChoices);
- });
- scissors.addEventListener('click',function(){
- playRound('scissors',computerChoices);
- });
- }
- }
- // const computerSelection = computerPlay();
- function playRound(string1,computerChoices){
- for ( j = 0 ; j < 5 ; j++ ){
- switch (string1) {
- case 'rock': if (computerChoices[j] == 'rock') {
- document.getElementById('tie').textContent= ++tie;
- Output();
- }
- else if (computerChoices[j] == 'paper') {
- document.getElementById('computerScore').textContent= ++ComputerScore;
- Output();
- }
- else {
- document.getElementById('playerScore').textContent= ++PlayerScore;
- Output();
- }
- break;
- case 'paper': if(computerChoices[j] == 'rock'){
- document.getElementById('playerScore').textContent= ++PlayerScore;
- Output();
- }
- else if (computerChoices[j] == 'paper'){
- document.getElementById('tie').textContent= ++tie;
- Output();
- }
- else {
- document.getElementById('computerScore').textContent= ++ComputerScore;
- Output();
- }
- break;
- case 'scissors': if(computerChoices[j] == 'rock'){
- document.getElementById('computerScore').textContent= ++ComputerScore;
- Output();
- }
- else if (computerChoices[j] == 'paper'){
- document.getElementById('playerScore').textContent= ++PlayerScore;
- Output();
- }
- else {
- document.getElementById('tie').textContent= ++tie;
- Output();
- }
- break;
- default : document.getElementById('result').textContent="error, please try again later";
- }
- }}
- // console.log("the computer had",computerSelection);
- function Output(){
- if(ComputerScore + PlayerScore + tie == 5){
- if(ComputerScore > PlayerScore){
- document.getElementById('output').textContent += "The computer wins";
- }
- else if (PlayerScore > ComputerScore){
- document.getElementById('output').textContent += "you win";
- }
- else {
- document.getElementById('output').textContent += "the game ends with a tie";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement