dimanche 30 novembre 2014

Javascript game code not console logging


Hey im new at javascript and im doing a rock, paper, scissors, lizard, spock game for class. Instead of using switch statements I decided to use continuous if, else if statements to make sure I account for every circumstance that I may encounter. My program is finished, the computer picks a random choice, users pick their choice, however the computer will not console log the statements if a circumstance occurs. I've tried for hours and cannot come up with a solution. My program is 350 lines long but here are the first few. The other lines are exactly the same but just have different choices for choice1 and choice2. Please help. Thank You in advance



var userChoice1 = prompt("Choose rock, paper, scissors, lizard, or spok")
var userChoice2 = prompt("Choose rock, paper, scissors, lizard or spok")
var computerChoice = Math.random();
if (computerChoice < 0.2) {
computerChoice = "rock";
} else if (computerChoice <= 0.4) {
computerChoice = "paper";
} else if (computerChoice <= 0.6) {
computerChoice = "scissors";
} else if (computerChoice <= 0.8) {
computerChoice = "lizard";
} else if (computerChoice <= 1.0) {
computerChoice = "spock";
}

console.log("Computer chose " + computerChoice);
console.log("Player 1 chose " + userChoice1)
console.log("Player 2 chose " + userChoice2)

var compare = function(choice1, choice2, choice3){
if (choice1 === choice2 === choice3) {
console.log("It's a tie!");
}


else if (choice1 === "rock", choice2 === "rock") {
if (choice3 === "scissors" ) {
console.log("Rock crushes both scissors.");
} else if (choice3 === "paper") {
console.log("Paper covers both rocks. ");
} else if (choice3 === "lizard") {
console.log("Rocks crush the lizard. ");
} else if (choice3 === "spock") {
console.log("Spock vaporizes both rocks. ");
}
}




Aucun commentaire:

Enregistrer un commentaire