The Student Room Group

The computer keeps picking llama and scissors

The computer keeps picking llama and scissors but never paper and rock and I've tested it plenty of times, I thought I'd given them all equal chance but can anyone see what's wrong with the code please?:smile:
Thanks


var userChoice = prompt("Do you choose rock, paper, llama or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.25) {
computerChoice = "rock";
} else if(computerChoice <= 0.50) {
computerChoice = "paper";
} if (computerChoice <= 0.75) {
computerChoice = "llama";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare= function (choice1, choice2)
{
if (choice1 === choice2)
{return "It's a tie!"}
else if (choice1 === "rock")
if(choice2 === "scissors")
{ return "Rock smashes up them scissors!Rock wins!";}
else if(choice2 === "llama")
{return "Llama gets the rock stuck in it's paw while raving: Rock wins!"}
else
{return "Paper rolls the rock up like a christmas pudding under Karl Pilkington's christmas tree! Paper wins!";}

else if (choice1 === "paper")
if( choice2 === "rock")
{return "Paper rolls the rock up like a christmas pudding under Karl Pilkington's christmas tree! Paper wins!";}
else if(choice2 === "llama")
{return "That llama is rushing off for a cheeky nandos, it aint got time to be picking up pieces of paper, it demolishes the paper! Llama wins!";}
else
{ return "Scissors slice that paper as if it were slicing bread. Scissors wins!";}

else if (choice1 === "scissors")
if (choice2 === "rock")
{return "Rock smashes up them scissors!Rock wins!";}
else if(choice2 === "llama")
{return " PLOT TWIST! That llama was angry because freddos have gone up to £1, it bends over, grabs the scissors with it's non opposable thumbs, breaks the scissors in half and stabs himself. Non-one wins!";}
else
{return "Scissors slice that paper as if it were slicing bread. Scissors wins!";}

else if (choice1 === "llama")
if(choice2 === "rock")
{return "Llama gets the rock stuck in it's paw while raving:Rock wins!";}
else if (choice2 === "scissors")
{return "PLOT TWIST! That llama was angry because freddos have gone up to £1, it bends over, grabs the scissors with it's non opposable thumbs, breaks the scissors in half and stabs himself. Non-one wins!";}
else
{return "That llama is rushing off for a cheeky nandos, it aint got time to be picking up pieces of paper, it demolishes the paper! Llama wins!";}
};
compare (userChoice, computerChoice)
Reply 1
It's very hard to read your code when its not indented. Use code tags to achieve this.

Also, could you explain why you are using the triple "===" equal sign to compare rather than "==" the double.
The triple equal sign doesn't do what the double equal sign does.

The way you select the computes turn is written in more lines than it needs to be.

1 equal sign assigns a value.
2 equal signs compares values
3 equal signs compares the types I believe.
(edited 8 years ago)
I have no idea what this is about but I love it.
You've said if it's less than or equal to 0.75 it'll choose llama, so that overrides the two before it! You can solve this by changing the 'if' just before it to 'else if'.

Also instead of all those else ifs at the end, you could clean it up a bit by using a switch :smile:
(edited 8 years ago)
Your if statements for computerChoice are wrong.if (computerChoice < 0.25) {
computerChoice = "rock";
}
else if (computerChoice <= 0.50) {
computerChoice = "paper";
}
else if (computerChoice <= 0.75) {
computerChoice = "llama";
}
else {
computerChoice = "scissors";
}
You're getting the random number and if it's below 0.25 or 0.50 assigning the choice to be "rock" or "paper" but then you're checking if it's below 0.75 and assigning the choice as "llama" (which will override the previous choices), otherwise it's choosing scissors.
Reply 5
OP to avoid all the confusion, select the computers turn like this.



It's just hassle free.
Original post by Async
OP to avoid all the confusion, select the computers turn like this.



It's just hassle free.


Okay thank you:smile:
Reply 7
This looks familiar... is this from CodeAcademy by any chance? :tongue:
Original post by Inexorably
This looks familiar... is this from CodeAcademy by any chance? :tongue:


Oh god you caught me:L I was only trying to learn it and then everyone replied with these smart answers and I'm just like whoopsssss:colondollar:

Posted from TSR Mobile
Reply 9
Original post by bellchez
Oh god you caught me:L I was only trying to learn it and then everyone replied with these smart answers and I'm just like whoopsssss:colondollar:

Posted from TSR Mobile


Haha!!

It's only 'cause I tried doing that course a while ago :tongue: I think codeacademy do have forums where you can look for help too, and sometimes if you can't progress the forums will say it's an error on their behalf and you need to X, Y, Z.

But it's good you're trying to learn though :smile:
Original post by Inexorably
Haha!!

It's only 'cause I tried doing that course a while ago :tongue: I think codeacademy do have forums where you can look for help too, and sometimes if you can't progress the forums will say it's an error on their behalf and you need to X, Y, Z.

But it's good you're trying to learn though :smile:


Hahaa thanks, it doesn't matter really I just go onto the next course:smile:

Quick Reply

Latest

Trending

Trending