Computer choice part 2 Syntax error token else? Help


#1

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){
  computerChoice="paper";};
  
else{
   computerChoice="scissors";
    };

I cnat find what I did wrong Im just a beginner so its possible that I make a stupid mistake plz help


#2

@cwannabe

 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){
computerChoice = "paper"

} else (computerChoice <=0.66);{
computerChoice = "scissors"
};

Compare this code to yours. I find that it helps to take notes on a piece of paper as I go through the courses. It helps the lessons stick in my brain. It also helps if you space out your code more. It makes it much easier to debug. Good luck.


#3

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

var computerChoice = Math.random()
console.log(computerChoice)
if (computerChoice <= .33) {
    computerChoice = "rock";
} 
else if (computerChoice <= .34) {
    computerChoice = "paper";
}
else (computerChoice <= .67) {
    computerChoice = "scissors";
}
};

For some reason my eyes can't seem to see what I'm doing differently than yours. Did your code work?


#4

Yeah, mine did. I only noticed 1 thing with yours, on line 9. You used ".67" instead of ".66" Not sure if that really affects it, but it could be the issue. I'm as much of a newbie as you are when it comes to Javascript.


#5

Nah it's still telling me there is an error with my syntax- an unexpected {.

Oh well, thanks anyway!


#6

@jcrulez4 and @byteslayer06219
First of all be careful with semicolons in the structure of conditions:


And the other thing you should have a look at is that else never has a condition attached to it as else is the default case.


#7

thx man it worked appreciate it


#8

No problem. Us newbies gotta stick together, right?


#9

hahah yeah i guess so


#10

Here is the right answer...

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){
computerChoice = "paper"

} else (computerChoice <=0.66);{
computerChoice = "scissors"
};


#11

While that is appreciated @zareen.k people are encouraged to do the code themselves. If we were just supposed to give answers, this wouldn't really be a good learning website. Again, it is appreciated, just try not to do it very often. Thanks!


#12

As @jcrulez4 said posting "solutions" is apart from being against the rules, a dangerous thing. All code looks alike, at least for beginners and they are the audience here. So without explanation you might not be able to track the error. You can't tell how good a solution is just by the code. And if you just copy and paste code without understanding how it works you're likely to do the same mistake again. Also according to the quality of a solution you might just found a bug or workaround and not a valid solution.

Unfortunately this is as well the case for your code here.

Yes you don't have a syntax error, probably had one and fixed it according to error messages. But the logic has flaws, which are a kind of errors that are worse than syntax errors because nobody tells you about it, the code just doesn't do what it is supposed to do. I guess the author of this course had problems to check for randomness, still it should be obvious that your code choses "scissors" every single time. And if it is not add a console.log(computerChoice) :slightly_smiling:

So the problem is first of all that you used a condition after else, which probably lead to complaining about a missing semicolon so you added one between ) and {. That fixes this problem but leads to another. To keep it short I link to a topic where I explained it a little further but if you have questions about it just ask:

So if you want to post helpful things rather:

  • point out errors
  • explain them if you can
  • use short snippets of the code instead of whole programs

also you can use these to mask the code for giving spoilers if posting code is inevitable:

[spoiler]code[/spoiler]
[details=Spoiler Warning]code[/details]

code

Spoiler Warning

code

And to improve the readability of your posted code you can use

```
code
```

e.g.

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

Hope this helps and explains the reasoning why posting full solutions without text might not be a good idea and is against forum rules.


#13

i appreciate it a lot it made things lot easier lol i went back and looked back at it i understand what i did wrong thx ppl


#14

i disagree. i feel if i see the answer then i work backwards and figure out where i went wrong. I learn from my mistakes, but only when i can compare my wrong answer to the correct answer. most the time its simply a missplaced colon or semi-colon.