4. Computer Choice part 2 help


#1


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


Oops, try again.
There was a problem with your syntax.
SyntaxError: missing before statement
I am not sure as to what I am missing/why this code doesn't work, any help appreaciated

var userChoice = prompt("Do you choose rock, paper or scissors?")
var computerChoice = Math.random() {
    if (computerChoice <= 0.23 and >= 0) {
        computerChoice = "rock";
    }
    else if (compuerChoice >= 0.34 and <= 0.66) {
        computerChoice = "paper";
    }
    else (computerChoice >= 0.67 and <= 1) {
        computerChoice = "scissors";
    }
};


#2

Hey there. Three things

  1. You have a curly brace after Math.random(). This should be removed as it serves no purpose.

  2. When we want to compare two conditions we use the logical operator "&&". It's the correct way of saying and in JavaScript. Also after your first && you'll want to compare >= 0 with computerChoice again. Same goes for <= 0.66

  3. else doesn't need parentheses because the else code will always run if the above code blocks do not. Therefore there's no logical reason for testing conditions.

Hope that clears it up. Just ask if you need some more clarification :slight_smile:


#4

All fixed, was a spelling mistake

Working code below for those wondering

var userChoice = prompt("Do you choose rock, paper or scissors?")
var computerChoice = Math.random()
    if (computerChoice <= 0.23 && 0) 
    {
        computerChoice = "rock";
    }
    else if (computerChoice >= 0.34 && 0.66) 
    {
        computerChoice = "paper";
    }
    else computerChoice >= 0.67 && 1 
    {
        computerChoice = "scissors";
};

#5

Did you test this code? Just looking at it gave me the spins.


#6

It may look like it's working but it's not quite working as it should.

Lets run through this code and see what it will do line(ish) by line(ish).

First line in question

if (computerChoice <= 0.23 && 0)

Straight away we can say that this will never evalute to true. Because in this context 0 is the same as saying false. So essentially we're saying: Do this "if" block only when computerChoice <= 0.23 is equal to true and 0 is equal to true. Again 0 is by default is equal to false so this if block will never execute for that reason.

Also we need to change the 0.23 to 0.33 because our next block (else if) starts at 0.34 so we need to cover all the values before then.

This line should instead read:

if (computerChoice <= 0.33 && computerChoice >=0)

Next line in question:

else if (computerChoice >= 0.34 && 0.66)

It's worth noting that when testing conditions, any value that is either a "0" or "-0" (and a few others that we won't cover) is automatically true by default. So 0.66 will automatically be true, which means that essentially the only condition we're testing here is if computerChoice >=34. This is because the && operator needs both conditions on either side to equal true, and since the right side is always true the left side in the only unsure condition left to test.

This line should look like this:

else if (computerChoice >= 0.34 && computerChoice <= 0.66)

Last line in question

else computerChoice >= 0.67 && 1

*Now I'm not sure why this is the case but the else block always runs when there is a condition just after else. If someone can enlighten me as to why I'd happily appreciate it.

Nevertheless that's not the real issue. The issue here is that after else should always follow an opening curly brace "{". There is no need to test a condition because the else block covers any condition that is not true. It's the default bit of code that will run when nothing above is true. Therefore we do not need to test a condition. If we want to test another condition we simply add another else if block instead.

This line should simply read:

else

Hope that clears things up. It know it might be confusion, and in fact this solution can be simplified quite dramatically. But I won't go into how because that would just add even more confusion.

*I figured out why it does this. If you're curious too you can PM me :slight_smile:


#7

When the logic starts getting complex, that's a sign to step back and look at the big picture. This project does not require any complicated logic. It's a simple if..else if..else so long as we keep the order from lowest to highest.

There are three possible outcomes for computerChoice, based on its value as a fraction of 1. That means,

1/3, 2/3 and 3/3 (1)

// let c = computerChoice for brevity

if (c < 1/3) {

} else if (c < 2/3) {

} else {

}

#8

Wery nice tips! Thank you bro)


#9

Yeah, it works according to the exercise allowing me to continue


#10

why would the curly brace be removed shouldn't all the functions syntax contain the curly brace as they said before? I'm very confused


#11

Function code needs to be placed inside curly braces yes.
But in this case we're calling a function Math.random(). The code for this pre-made function is built into JavaScript and has already been defined between curly braces.


#12

thank you i get it now


#13

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