Doubt exercise paper stone and scissors

Asked

Viewed 205 times

-5

I’m a beginner in programming and discovered a site play to start the codeacademy, but they have some errors and maybe making my understanding difficult.

I am in the following javascript exercise:

Computer Choice: Part 2 We have computerChoice, but it is now equal to a random number between 0 and 1. We need to somehow translate this random number into a choice between stone, paper and scissors. How do we do this?!

If computerChoice is between 0 and 0.33, computerChoice equal to "stone". If computerChoice is between 0.34 and 0.66, do computerChoice equals "paper". If computerChoice is between 0.67 and 1, computerChoice equal to "scissors". But there are three results! If / Else allows us only two results. And now?! We need to use if / Else if / Else. Read the hint for the full syntax. You will find grace so easy she is.

Instructions Under your existing code, write the if / Else statement if / Else In the respective code blocks, change the value of computerChoice based on the above rules. Remember, you DO NOT need use var when changing the value of an existing variable.

Detail: They didn’t teach me to do if else along.

I have to play a little game of rock, paper and scissors following these instructions.

Follow how I wrote the code:

var userChoice = prompt("Você escolhe pedra, papel ou tesoura?");
var computerChoice = Math.random();
console.log (computerChoice);

if (computerChoice <= 0.33) {
  console.log("Pedra");
} else if (computerChoice Não sei o que colocar aqui (tem alguma função "entre") {
  console.log("Papel");
} else (computerChoice > 0.67) {
  console.log("Tesoura");
}

If you can help me to advance my course!

  • The "between" condition in your case is: greater than 0.34 and less than 0.66.

  • 4

    This question has two votes closing as wide as too. Although this is certainly not one of our better questions, I disagree with the closing votes. This question has nothing broad in it (on the contrary, it is very restricted, since it is a question about a single line of code). In addition, I believe that the other reasons for closure do not apply (it is not duplicate, it is about programming, it is not opinionated, the question is clear, it is not a typo, it is reproducible and it is in Portuguese).

3 answers

2

Acetyl, you can use computerChoice > 0.33 && computerChoice <= 0.67

var userChoice = prompt("Você escolhe pedra, papel ou tesoura?");
var computerChoice = Math.random();
console.log (computerChoice);

if (computerChoice <= 0.33) {
  console.log("Pedra");
} else if (computerChoice > 0.33 && computerChoice <= 0.67) {
  console.log("Papel");
} else {
  console.log("Tesoura");
}

but I believe that a better solution would be to generate a random number between 1 and 3 and make a switch.

function getRandom(min, max) {
  return Math.floor(Math.random() * max) + min  
}

var num = getRandom(1, 3);
switch (num) {
  case 1: console.log("Pedra"); break;
  case 2: console.log("Papel"); break;
  case 3: console.log("Tesoura"); break;
}

2


I suggest you study a little more, I think you need to "go back" a little and learn more about the fundamentals of programming (logic) and language, which is more or less where you will learn about comparison and logical operators. This link has a complete table with all operators and description of what they are.

In your case you will need the operators greater-than > and minor <:

if (computerChoice <= 0.33) {
  console.log("Pedra");
} else if (computerChoice>0.33 && computerChoice<0.67) {
  console.log("Papel");
} else (computerChoice >= 0.67) {
  console.log("Tesoura");
}

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.