Help with Javascript 101 Project 1: Rock, Paper, Scissors


#1

I have been trying many things and with the help of google this is what I have now. I don’t get any errors anymore in the JS Console but I wont get any output when I put in my player selection.

I prefer to get some hints if I am in the right direction and where I should look at in order to get this working.

Code:

//FUNCTIONS
//Kiest random 1 van de drie opties
function computerPlay(){
    let computerChoice = Math.random();
    if (computerChoice < 0.34){
        computerChoice = 'rock';
  } else if (computerChoice < 0.68){
          computerChoice = 'paper';
  } else {
          computerChoice = 'scissors';
        } 
      }
//Speelt een ronde Rock, Paper, Scissors. Gebruikt computerPlay() voor CPU input
function playRound(round_actions){
  //CONST
  const computerSelection = computerPlay();
  let playerSelection = prompt ('Rock, Papers or Scissors?')
   if (playerSelection===computerSelection){
      return('It is a tie! Try again');
        round_actions.appendChild(round_actions);
            return -1;
    }
    if (playerSelection==='rock'){
        if (computerSelection==='paper'){
            return('You lose! Papers beats Rock');
            round_actions.appendChild(round_actions);
            return 0;
        }
        else if (computerSelection==='scissors'){
            return('You win! Rock beats Scissors');
            round_actions.appendChild(round_actions);
            return 1;
        }
    }
    if (playerSelection==='paper'){
        if (computerSelection==='rock'){
            return('You win! Papers beats Rock');
            round_actions.appendChild(round_actions);
            return 1;
        }
        else if (computerSelection==='scissors'){
            return('You lose! Scissors beats Paper');
            round_actions.appendChild(round_actions);
            return 0;
        }
    }
    if (playerSelection==='scissors'){
        if (computerSelection==='rock'){
            return('You lose! Rock beats Scissors');
            round_actions.appendChild(round_actions);
            return 0;
        }
        else if (computerSelection==='paper'){
            return('You win! Scissors beats Paper');
            round_actions.appendChild(round_actions);
            return 1;
        }
    }
    else {
      return('Something went wrong')
      round_actions.appendChild(round_result);
                return NaN;
    }
}
function game(){
  let playerSelection = prompt('Rock, paper, scissors?').toLowerCase();
  let computerSelection = computerPlay();
  //Win counter
  let playerCounter=0;
  let computerCounter=0;
  //Round counter
  let roundCounter=0;
  //Functie die de actie ontdekt van de speler of cpu
  document.addEventListener("click", function (event) {
      let current_round 
      if (event.target.matches('#rock')){
      current_round=playRound('rock', computerPlay(), round_actions);
      }else if (event.target.matches('#paper')){
        current_round=playRound('paper', computerPlay(), round_actions);
      }else if (event.target.matches('#scissors')){
        current_round=playRound('scissors', computerPlay(), round_actions);
      }else if (event.target.matches('#reset')){
        location.reload(true);
      }
      //Update de ronde. Bepaald de winnaar
      if (current_round === 1){
        playerCounter++;
        roundCounter++;
      }else if (current_round === 0){
        computerCounter++;
        roundCounter++;
      }
      console.log(playRound(playerSelection, computerSelection))
    });
}
  game();

#2

No one? :frowning:


#3

Hey @DennisKr

You don’t return anything from your computerPlay() function which means it’ll return undefined. You should return computerChoice before the function closing bracket