Does my code work? Is it right?


#1


Computer Choice Part 2

I'm getting the pass but my code keeps printing "scissors" underneath the number. Is that okay or am I missing something?


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

We cannot write expressions this way. It needs to be broken into two expressions and combined with an &&.

if (0 <= computerChoice && computerChoice <= 0.33) {}

When we look at the problem, though, we can see that if done in order from lowest to highest, we can use much simpler logic:

[0, 1/3), [1/3, 2/3), [2/3, 1)

Each interval is exactly equal in size. 1/3.

if c < 1/3 then rock
else if c < 2/3 then paper
else scissors

The above is pseudo-code so don't try to use it. Just follow the pattern.


#3

Ahh gotcha. I appreciate it.

One quick question. For the last parts of the RPS game, is my code supposed to carry over from each exercise? It keeps resetting every time I begin a new section.

Also, it doesn't always print everything I put in.. but it always gives me the pass


#4

Your code should be carried forward. Perhaps after passing each lesson, refresh the page. submit again then proceed. See if that makes any difference. Sorry for the seat of the pants answer.


#5

No problem. Thanks for taking the time to help out!


#6

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