Doubt exercise stone paper and scissors

-5

I'm a beginner in programming and I discovered a web site to start such codeacademy, but they have some errors and tals making it difficult to understand.

I'm 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 some way   translate this random number into a choice between stone, paper and   scissors. How will we do this?!

     

If computerChoice is between 0 and 0.33, make computerChoice equal to   "stone". If computerChoice is between 0.34 and 0.66,   computerChoice equal to "paper". If computerChoice is between 0.67   and 1, make computerChoice equal to "scissors". But there are three results! If   / else allows only two results. And now?! We need to use   if / else if / else. Read the tip for the complete syntax. You go   find grace so easy it 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 rules above. Remember, you do NOT need   use var when you are changing the value of an existing variable.

Detail: They did not teach me how to if else together.

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

Here's 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 the course!

    
asked by anonymous 30.08.2016 / 14:16

3 answers

2

I suggest you study a little more, I think you need to "go back" a bit 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 . In this link you have a complete table with all the operators and description of what they are.

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

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");
}
    
30.08.2016 / 14:32
2

Acceptable, 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;
}
    
30.08.2016 / 14:27
1

Just do so on the "paper" conditional

else if (computerChoice > 0.33 && computerChoice < 0.67) {

 console.log("Papel");

There is usually no feature like

if (valor entre x e y) {

}

You have to compare explicitly

if (variavel > x && variavel < y) {

}

Learn about the logical operators of the language:

link

link

    
30.08.2016 / 14:29