I am having trouble with this assignment to create a game of rock, paper, scissors in javascript (Assignment from the odinproject.com). Whenever I use rock as my input it seems to work just fine. However, it does not work properly for when paper or scissors are entered. Please any help, suggestions, and advice would be greatly appreciate it. I am still very new at this and eager to learn more!
Thanks!
Here's my code:
function userChoice() {
const userChoice = prompt("Do you choose Rock, Paper, or Scissors?").toLowerCase();
return userChoice
}
function compChoice() {
let compChoice = Math.random();
if (compChoice < 0.34) {
compChoice = "rock";
} else if (compChoice <= 0.67) {
compChoice = "paper";
} else {
compChoice = "scissors";
}
return compChoice
}
function playRound(userChoice, compChoice) {
//Rock Choice
if (userChoice == "rock" && compChoice == "scissors") {
return "You Win! rock beats scissors!";
} else if (userChoice == "rock" && compChoice == "paper") {
return "Loser! paper Beats rock";
} else if (userChoice == "rock" && compChoice == "rock") {
return "It's a Draw";
} else {
return "Something Went Wrong";
}
//paper Choice
if (userChoice == "paper" && compChoice == "rock") {
return "You Win! paper beats rock";
} else if (userChoice == "paper" && compChoice == "scissors") {
return "You Lose! scissors beats paper";
} else if (userChoice == "paper" && compChoice == "paper") {
return "It's a Draw";
} else {
return "Something Went wrong";
}
//scissors Choice
if (userChoice == "scissors" && compChoice == "paper") {
return "You win! scissors beat paper";
} else if (userChoice == "scissors" && compChoice == "rock") {
return "You Lose! rock beats scissors";
} else if (userChoice == "scissors" && compChoice == "scissors") {
return "It's a Draw";
} else {
return "Something Went Wrong";
}
}
console.log(playRound(userChoice(), compChoice()))
Aucun commentaire:
Enregistrer un commentaire