Problem with variable


#1
var myChoice = prompt('Pedra, Papel ou Tesoura?');

var options = [
  'Pedra',
  'Tesoura',
  'Papel',
  ];

var limit = options.length - 1
              
var computerChoice = computerChoice[Math.floor(Math.random() * limit)];


if (myChoice !== 'Papel' && myChoice !== 'Pedra' && myChoice !== 'Tesoura') {
	alert('Ops! Opção inválida, você deve ter escrito errado!');
} 



function Compare() {
  if (myChoice === computerChoice) {
    console.log('EMPATE!');
  } else if (myChoice === 'Tesoura' && computerChoice === 'Papel') {
    console.log('Você ganhou!');
  } else if (myChoice === 'Tesoura' && computerChoice === 'Pedra') {
    console.log("Ganhei haha!");
  } else if (myChoice === 'Pedra' && computerChoice === 'Papel') {
    console.log('Ganhei haha!');
  } else if (myChoice === 'Pedra' && computerChoice === 'Tesoura') {
    console.log('Você ganhou!');
  } else if (myChoice === 'Papel' && computerChoice === 'Tesoura') {
    console.log("Ganhei haha!"); 
  } else if (myChoice === 'Papel' && computerChoice === 'Pedra') {
    console.log("Você ganhou!");
  }
}

console.log('escolha do Usuário: ' + myChoice)
console.log('Escolha do PC: ' + computerChoice)
Compare()

Why am I having problem with computerChoice?


#2

Did you mean to write,

var computerChoice = options[ ... ];

?


#3

Wow, what a silly mistake, I had not noticed, thank you!


#4

Naw… Common. We never stop making mistakes, though we can relish in them if they lead to a leg up. I actually like finding mistakes for the retrospective view of the code we gain from them.

Preventing mistakes is a proactive vigilance that can stymy creativity. Finding mistakes is a skill that can only be developed from the practice of actually finding mistakes, then examining the root cause.

I have dyslexia so make a tonne of typing mistakes. Letters get typed that one would never have intended, ‘s’ for ‘f’ or ‘f’ for ‘s’, and so many like this that occur repeatedly.

The coolest thing about programming is how we are the designer, the helmkeeper, the tester and debugger. We set the rigor so control the expectations. Bottom line, we learn to find mistakes by making lots of them. Smile and pat yourself on the back every time you make a new stride or discover a new idea as the result of a mistake. If you are a note taker, then NB and document as you go.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.