4/9 Computer Choice: Part 2


#1

It says SyntaxError: Unexpected token {

But I am not sure what I am doing wrong. Here is the code so far:

var userChoice=prompt("Do you choose rock, paper or scissors?")

var computerChoice= Math.random()
console.log(computerChoice)

if (0 < computerChoice <0.33) {
computerChoice="rock";
} else if (0.34 < computerChoice < 0.66) {
computerChoice="paper";
} else (0.67 computerChoice="scissors";
}

EDIT: I figured it out, but I have not figured out how to delete a thread.


#2

You forgot to put a '{' here:

Also no requirements should be put in an else statement.

else{

(Right version)


#3

Hi @rhaegerys

Check this out :

if(computerChoice < 0.33){
    computerChoice = "rock";
}else if(computerChoice < 0.66){
    computerChoice = "paper";
}else{
    computerChoice = "scissors";
}

Hold on :wink:


#4

This is Correct and really works

var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random();
console.log(computerChoice);

if (0 < computerChoice < 0.33)
{
computerChoice="rock";
}
else if (0.34 < computerChoice < 0.66)
{
computerChoice="paper";
}
else (0.67 > computerChoice > 1)
{
computerChoice="scissors";
}


#5

if ( computerChoice < 0.33) {
console.log("Rock");
}
else if{ computerChoice < 0.66)
{
console.log("paper");
} else{
console.log("scissors");
}

use this cod rohan


#6

I don't want to create new thread so I'll bump this.. I just cannot figure this out, what am I doing wrong??

I FIGURED IT OUT THANKS!

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice)

if (computerChoice <= 0.33)
{
computerChoice="rock";
}
else if (computerChoice 0.34 < 0.66)
{
computerChoice="paper";
}
else
{
computerChoice="scissors";
}


#7

Better create an own one and best with the exercise number and the error message in the title. Or bumb in one that has exactly that title and not a solution to your problem yet.

To your actual problem:

computerChoice 0.34 < 0.66

this does not work. You have 2 values here: computerChoice and 0.34 < 0.66 which is a synonom for true. And these values of no connection via an operator or something like this. @onepunch_man has a working example and @cssrunner35185 has a logic problem where I'd recommend to read this explanation:

https://www.codecademy.com/forum_questions/51fc4178f10c60f4eb001233#answer-529f101f7c82ca3ce100308d


#8

Hi I tried your code: it does generate any random number, but it always prints out "scissors" and not any other option


#9

The reason it always prints out scissors is because the else statement has the carrots backwards. However, since it's an else statement, there shouldn't be any conditions.
Current code:

 else (0.67 > computerChoice > 1)
    {
    computerChoice="scissors";
    }

Corrected code:

 else 
        {
        computerChoice="scissors";
        }

#10

All, I googled this and found more reliable code that logically makes more sense and does not keep returning scissors. This is not something that codeacademy goes over in the lesson but works better than what they did go over.

if (computerChoice > 0 && computerChoice < 0.33)
{
computerChoice="rock";
}
else if (computerChoice > 0.33 && computerChoice < 0.66)
{
computerChoice="paper";
}
else
{
computerChoice="scissors";
}


#11

Although you had a problem with your solution, I used part of it that was right to help me. So Thank You, and I hope you got the correct answer!!!


#12

Can someone tell me what I am doing wrong here:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log (computerChoice);

if (computerChoice >= 0, <= 0.33) {
computerChoice = "rock";
}

else if (computerChoice >= 0.34, <= 0.66) {
computerChoice = "paper";
}

else (computerChoice >= 0.67,<= 1) {
computerChoice = "scissors";
};


#13

This is what I did

Spoiler Alert!

var userChoice = prompt ("Do you choose rock, paper of scissors?");
var computerChoice = Math.random();
console.log (computerChoice);
if (computerChoice <= .33){
    computerChoice = "rock";
} else if (computerChoice > .33 && computerChoice <= .66){
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
};


#14

First of all else never takes a condition so get rid of this:

else { // (computerChoice >= 0.67,<= 1) {

And the other this is that this doesn't work:

else if (computerChoice >= 0.34, <= 0.66) {

First of all the comma operator is meant to chain to statements where the value of the whole statement is determined by the last statement e.g. console.log(computerChoice),computerChoice<0.33 would have the value of computerChoice<0.33 but would execute both statements. This is rather something advanced that you can use in positions where only one statement is allowed but more are needed. So rather skip this for later that is not what you need here. Which needs to the next statement what is the value of this statement? Well <= 0.33. Yes computers are dumb and mostly don't get context so you really need to tell them again that you want to look at computerChoice otherwise you compare it to the void which generates an error. Last but not least again the comma operator combines to statements to one but it doesn't mean that conditions get chained as only the last part of it is important rather use the boolean operators && and || (AND and OR) to do the job. Or even better think if you truly need more than one condition at once. Sure you want to make sure that it is in a certain range but isn't one of the boundaries always fixed anyway?


#15

i think just what if its exactly 0.33 or 0.66 what then?

if (computerChoice > 0 && computerChoice <= 0.33) {
computerChoice = "rock";
} else if (computerChoice > 0.33 && computerChoice <= 0.66) {
computerChoice = "paper";
} else {
(computerChoice > 0.67 && computerChoice <= 1) {
computerChoice = "scissors";

thats how i did it straight without a lot thinking but it doesnt work for me...
i tried to be specific to encounter every 0.01 to have answer for it is it paper or scissors or rock.. but still got syntax error


#16

ok sorry if i read your comment it would be clear to me that only problem i had ( with syntax in my example under your comment) was that i actually dont need condition @else statement. thanks haxor...