9/9 Completed, but Code Still Broken


#1

I finished 9/9 and it says I passed, but the game doesn’t seem to work the way it is supposed to.
Code testers used:

labs repl.it
http://www.webtoolkitonline.com/javascript-tester.html

NOTE: I have condensed the code for easier debugging. I included only one of the conditions “(choice1 = “rock”)”. And I removed the “Math.random()” and put in a fixed number.

PROBLEM: No matter what fixed decimal fraction you enter (e.g. 0.3, 0.5, 0.89) this code always produces “paper wins.” There seems to be something wrong with the “if / else” statements. What am I missing?

var userChoice = prompt(“Do you choose rock, paper or scissors?”);
var computerChoice = 0.80;
if (computerChoice <= 0.33) {
console.log(“rock”);
}
else if (computerChoice <= 0.66) {
console.log(“paper”);
}
else {
console.log(“scissors”);
}
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === “rock”) {
if (choice2 === “scissors”) {
return “rock wins”;
}
else {
return “paper wins”;
}
}
};
compare(userChoice, computerChoice);


#2

You have
if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
};

BUT
if (choice1 === "rock") {
//why not return rock wins HERE ? otherwise, you are just falling through conditions
if (choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
};


#3

Thanks! I liked where you were going with that. I spent a few more hours on this and did some more research on other sites AND IT WORKS! :smile: Helpful site: http://www.w3schools.com/js/js_comparisons.asp

Below is my modified version. I had to remove the separate "if" condition for when choice 1 = choice 2 and add a line for it to each of the three choice1 condition sets. (Commented lines included for easy viewing.) What I realized is that the code apparently does not recognize the "console.log" output (e.g. "rock") from the "var computerChoice = Math.random();" but instead recognizes the numerical value (e.g. 0.542...). Take a look, try it in the Codecademy Labs editor (link in previous post above), and see that it produces the correct answers according to the rules of the game. There may, however, be better or simpler ways to write this.

CODE:

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

var compare = function(choice1, choice2) {
    if (choice1 === "rock" && choice2 <= 0.66) {
            return "paper wins";
            }
        else if (choice1 === "rock" && choice2 <= 0.33) {
            return "It's a tie! Start again!";
        } else if (choice1 === "rock") {
            return "rock wins";
        }
//____________________________________
    else if (choice1 === "paper" && choice2 > 0.66) {
            return "scissors wins";
            }
        else if (choice1 === "paper" && choice2 <= 0.33) {
            return "paper wins";
        } else if (choice1 === "paper") {
            return "It's a tie! Start again!";
        }
//_____________________________________
    else if (choice1 === "scissors" && choice2 <= 0.33) {
            return "rock wins";
            }
        else if (choice1 === "scissors" && choice2 > 0.66) {
            return "It's a tie! Start again!";
        } else if (choice1 === "scissors") {
            return "scissors wins";
        }
        
};
compare(userChoice, computerChoice);

#4

The idea is that you reassign computerChoice based on the random number. But here you just console.log the value, but instead it should be assigned to computerChoice:

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

currently you print the value but go on with the number, as you already found out.

Interesting way to work around, not bad. But I guess the way present in the exercise is a little easier. There you boil down your 9 cases to effectly 7

tie case
choice1 == rock including 2 options for the computer
choice1 == paper including 2 options for the computer
choice1 == scissors including 2 options for the computer

so here you get rid of at least 2 tie cases as they are always the same.


#5

Thanks for the reply! You mentioned...

When I completed the current exercise, the way the code was written it was broken (even tho I passed). Therefore I created my work-around. How was the code supposed to have been written? I don't have the "correct" answer to see the 7 cases you refer to. Thanks!


#6

It's more or less what I mentioned here:

tie case
choice1 == rock including 2 options for the computer
choice1 == paper including 2 options for the computer
choice1 == scissors including 2 options for the computer

tie case is obvious (choice1 === choice2) and an example for one of the other cases is mentioned in the hint of 6/9 the other 2 cases are similar to this one. So in the end you got 4 checks for choice1 (tie +RPS) and each of the RPS cases has to checks for choice2 leaving you with 7 cases.