Syntax error {


#1

Why am a getting an error? Also, is my formatting correct?
Thank you in advance for your help!

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/0/4?curriculum_id=506324b3a7dffd00020bf661#

SyntaxError: Unexpected token {

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
if(0 < computerChoice < .33) {
   computerChoice = ("rock");
} else if(.34 < computerChoice < .66) {
   computerChoice = ("paper");
} else(.67 < computerChoice < 1) {
   computerChoice = ("scissors");
}

#2

you don't need the parenthesis around the computerChoice values in your if statements and in the else statement you normally do not declare an argument.

var age = 60;
var hobbies = "age dependent";
if  (age == 100) {
 hobbies = "staying alive";
}
else if (30 < age < 100) {
 hobbies = "trying to act young";
}
else {
 hobies = "having a blast ";
};

#3

Keep going with your explanation Chris, there is one more thing. That isn't how you would do between in JavaScript:


#4

Thanks didn't realize that not that great with JavaScript yet. I suppose it would be better if I did it as

age > 30 && age < 100

#5

Thanks for your help! I'm still not clear on how to do between...I don't understand that last post...?


#6

You're comparing three values, how many comparisons have to happen for that?

a < b < c describes that this relation is, but javascript isn't about describing how things are, but instead how to execute actions. That doesn't describe an executable action.. Well it does, but doesn't do what you think it does. What order is that evaluated in?


#7

Thank you, I got it to work like this:

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

There must be a way to do between that I would have learned in the course already though, no? I've never seen the "&&" before!


#8

To determine "betweenness" you need to do two comparisons. That's how you'd do it.

Python allows for a < b < c to test if b is greater than a and less than c, but it's syntactical sugar that gets translated into two comparisons before it is executed.

Python can also do:

if a in range(5, 100):
    print 'a is greater than 4 and less than 100

Python is just better, let's do that instead.

You could create a function for testing if a value is between two others..kind of not really worth its own function though.


#9

Lol thanks, but I'm still confused. Do you mind literally writing out the answer for me based on the answer I posted?


#10

You already did.


#11

Cool, so what I posted is correct?
I'm not sure how I would have known how to do that based on the course so far since they never showed me &&...


#12

Do you need to though?

if(0 < computerChoice < .33) {
   computerChoice = ("rock");
} else if(.34 < computerChoice < .66) {

You've already determined that it's greater than or equal to 0.33

Same goes with the 0, why would it be less than 0, how was it created, would that produce something negative?

(1/3 would be a better approximation but that's beside the point)


#13

Oh, thank you, okay. Wasn't sure if that was syntactically correct.
Should technically be .33 <= computerChoice <.66 though, right? (unless the program assumes rounding?)


#14

floats can't represent all values, so whether it's rounded (or rather, a nearby number that can be represented) depends on whether those numbers can be represented.

For example, 1/3 can't be written in base10 exactly, because it requires an infinite number of digits.
But in base3, it can be written as 0.1 (in base10 that would be one tenth, in base3 that's one third)
Floats are represented in base2 because of our hardware, so it can represent one half exactly (0.1) but can't represent one tenth exactly.

JavaScript's Number are floats, even for integers. Whole numbers can all be represented in the range ±253-1, after that accuracy starts suffering.


#15

you could have done

if (computerChoice < .33) {
computerChoice = "rock";
}
else if (computerChoice > .66) {
computerChoice = "scissors";
}
else {
computerChoice = "paper"; 
}

since you have already defined the other two comparisons, the computer will take anything not passing those arguments and throw it into the else block. that way you would not have to worry about figuring out how to do a between comparison. This also would follow what you have already learned so far I believe. in this anything from .33 to .66 would return paper.
sorry I didn't even catch that last night


#16

Haha well, I get the general idea :wink: Thanks so much!!


#17

Oh duh! Haha thanks!!


#18

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