Syntax error; rock,paper,sci, #4


#1

Can someone explain to me why I am getting a syntax error?

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

var computerChoice = Math.random(c)
if (c <= 0.33) {
c === "rock"
};
else if (c <= 0.66) {
c === "paper"
};
else (c <= 1) {
c === "scissors"
};


#2

Hi Mikey,

I'm unsure if you are missing characters in the cod you posted since it's not properly formatted for forum use. Take a look at this to post code on the forum: Formatting Code for Forum Use

From what I can see at the moment, you're missing semi-colons where you are assigning a value to c and you're using === instead of = when you're assigning a value to a variable. Lastly, you're using a semi-colon after the brackets, which isn't needed.


#3

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

var computerChoice = Math.random(c)
    if (c <= 0.33) {
        c = "rock";
    } else if (c <= 0.66) {
        c = "paper";
    } else (c <= 1) {
        c ="scissors";
    }

I did what you said, thank you. I did not know. So what is wrong?


#4

Hi Mikey,

Thank you :smile:

On line 2, you're missing a semi-colon at the end.

on line 4, 6 and 8 you are using === instead of = to assign a variable a new value. We use === when comparing a string and it's strict, so they have to be of the same type.

JavaScript has two operators for determining whether two values are equal:

The strict equality operator === only considers values equal that have the same type. The “normal” (or lenient) equality operator == tries to convert values of different types, before comparing like strict equality. For more information, take a quick read here.

On line 5, go with else if, you're capitalizing the 'E'.

On line 7 you are giving it a condition, but it's an else statement, there can be no condition. Use an else if if you require another condition.

Let me know if that works.


#5

I did not realize that an else statement could not have conditions. I was thinking like Ruby instead of JS.

For someone reason if I give Math.random a parameter of c it gives me:

ReferenceError: c is not defined

Shouldnt it work if I define Math.random as c?

I did this and it worked:

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

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

#6

Yes, because c wasn't defined, but apart from that, the usage for Math.random() is like this:

Example: Math.floor((Math.random() * 10) + 1); This would return a value between 1 - 10
Example: Math.floor() This would return a random number from 0 (inclusive) up to but not including 1 (exclusive).

I did not catch you using something within that, :frowning:

Since you were also using c which was an undefined variable, that would have given you an error after fixing the other syntax errors mentioned. The variable you are comparing is the one you need, which is great that you figured that out on your own! Great job!

And yes, if you were to say var c = Math.random(); it would work - not sure if the checker here would pass it.

Make sure you add a semi-colon on line 4 var computerChoice = Math.random();


#7

Thank you lloan!

JS is somewhat confusing compared to Ruby but I think I will get the hang of it.

Thank you for your help!