Author | Message | ||
---|---|---|---|
| ![]() been learning some javascript, but im having a hard time making the computers choice random between the outcomes of rock paper and scizzors.. :P I cant manage the the outcome to be random between the 3 chances :/ Maybe someone can help me? Thanks in advanced ^^ var rock = rock < paper; var paper = paper < scizzors; var scizzors = scizzors < rock; var userChoice = prompt("Do you choose rock, paper, or scizzors?"); var rockPaperScizzors = function (outcome) { if (userChoice > rock) { return ("You lose"); } else { return ("You win!"); } }; var rockPaperScizzors = function (outcome) { if (userChoice > paper) { return ("You lose"); } else { return ("You win!"); } }; var rockPaperScizzors = function (outcome) { if (userChoice > scizzors) { return ("You lose"); } else { return ("You win!"); } }; var computerChoice1 = Math.random(); var computerChoice2 = Math.random(); var computerChoice3 = Math.random(); var computerChoice = function (outcome) { if (computerChoice1 === rock) { console.log(computerChoice1 = 0,0.33); } else if (computerChoice2 === paper) { console.log(computerChoice2 = 0,0.66); } else if (computerChoice3 === Scizzors) console.log(computerChoice3 = 0.67,1); };
|
| ![]() I know it's probably a bit late, but here goes: /*ROCK PAPER SCISSORS*/ /* 1 = Rock 2 = Paper 3 = Scissors */ var randomInt = Math.floor(Math.random() * 3) + 1; //randomint between 1 and 3 var computerChoice = ""; if (randomInt == 1) computerChoice = "rock"; else if (randomInt == 2) computerChoice = "paper" else if (randomInt == 3) computerChoice = "scissors"; var userChoice = ""; while(userChoice.toLowerCase() != "rock" && userChoice.toLowerCase() != "paper" && userChoice.toLowerCase() != "scissors"){ userChoice = prompt("Please enter Rock Paper or Scissors"); } //while the input isn't either rock paper or scissors, keep asking the visitor to enter something valid var status =""; switch(computerChoice){ case "rock": if (userChoice=="rock") status="drew"; else if (userChoice=="paper") status="win"; else if (userChoice=="scissors") status="lose"; break; case "paper": if (userChoice=="rock") status="lose"; else if (userChoice=="paper") status="drew"; else if (userChoice=="scissors") status="win"; break; case "scissors": if (userChoice=="rock") status = "win"; else if (userChoice=="paper") status="lose"; else if (userChoice=="scissors") status="drew"; break; } //comparison between the two. Could probably be done in less code using integers but oh well. alert("You: " + userChoice.toLowerCase() + ". \nComputer: " + computerChoice.toLowerCase() + ".\nResult: You " + status); this is a way I got it working :) hope this helps! https://tehsausage.com/paste/rockpaperscissors --- If money doesn't grow on trees, then why do banks have branches? |