Did I do everything correctly here?


#1

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return "The result is a tie!";
    }
    else if(choice1 === "rock") {
        
        if(choice2 === "scissors") {
            return "rock wins";
    }
    else {
            return "paper wins";
    }
    }
    else if(choice1 === "paper") {
        
        if(choice2 === "rock") {
            return "paper wins";
        }
        else {
            return "scissors wins";
        }
    }
    else if(choice1 === "scissors") {
        
        if(choice2 === "rock") {
            return "rock wins";
        }
        else {
            return "scissors wins";
        }
    }
    
};

var result = compare(userChoice, computerChoice);
if (result === "The result is a tie!") {
    console.log(prompt("Try again"));
}
else {
    console.log("Play again soon");
}

#2

Hey there! Could you paste your code between three backlits to make it more readable?
Like this:

```javascript        <- javascript is optional
//your code here
```

And I see a small problem, at a tie, you prompt the user for another choice, but you don't do anything with that choice.


#3

Which tie are you talking about?


#4

At the bottom, it just says "Try again" and the program ends.


#5

What should I have put?


#6

I'm pretty confused with this bit of code.

var result = compare(userChoice, computerChoice);
if (result === "The result is a tie!") {
    console.log(prompt("Try again"));
}
else {
    console.log("Play again soon");
}

#7

You could make the program a loop and after it says "Try Again" it could restart the loop, though you might've not learned that yet.


#8

I just started loops :stuck_out_tongue:


#9

Okay, well it shouldn't be long before you learn about continue.


#10

Alright, so I should go back to this project when I learn loops?


#11

Sure, but loops aren't too horribly hard, even just a basic understanding of a for loop should work fine.


#12

thanks, also nice to meet you!


#13

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