4. Computer Choice: Part 2 -- Intervals


#1



With the following code, I get the error message "Did you make one of your ranges end at .33?", which I understand, but I think the instructions have a slightly mistaken logic.

Using the rounded values 0.33 and 0.34 leave a gap between them: if computerChoice turns out to have the value 0.335, neither "rock" nor "paper" is chosen. Instead, computerChoice takes the value of "scissors", assuming it is under else. Consequently, the computer is slightly more likely to choose "scissors", which makes the game slightly flawed. Admittedly, this is somewhat nitpicking, but the exercise is encouraging sloppy coding.

var userChoice = prompt("Rock, Paper, Scissors?")
var computerChoice = Math.random()
console.log(computerChoice)
if (computerChoice >= 0 && computerChoice < (1/3)) {
    computerChoice = "rock"
} else if (computerChoice >= (1/3) && computerChoice < (2/3)) {
    computerChoice = "paper"
} else if (computerChoice >= (2/3) && computerChoice < 1) {
    computerChoice = "scissors"
} else {
    console.log("Error: Invalid random number.") // Should never happen, but just to be on the safe side.
}


#2

My code...
Mine usually get's rock... It is sloppy yes, I can see what you mean.

//Meh. Sloppy yes.
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"
}
}
}
compare(userChoice,computerChoice);