I started learning to code recently, and following a program called Odin Project. I'm currently tring to make a Rock, Paper Scissor games, but I got stuck. I created a function to to make the computer choose a random value among the three options and return one of them. Then I created another function to alert a different message according to the computerchoice. But I always get the message "Something went wrong", which means this new function is not getting any of the correct options from the previous one. Can anybody help?
// THIS FUNCTION DECIDES IF THE COMPUTER CHOOSES ROCK, PAPER OR SCISSOS
function getComputerChoice () {
var rand = Math.floor(Math.random() * 10);
if (rand <= 3) {
return "Rock"
} else if (rand <= 6) {
return "Paper"
} else {
return "Scissors"
}}
// I PUT THE COMPUTER CHOICE FUNCTION IN A CONST BOX
const computerSelection = getComputerChoice();
// TESTING PLAYROUND FUNCTION
function playRound(computerSelection) {
if (computerSelection === "Paper") {
alert("You lose! Paper beats Rock")
} else if (computerSelection === "Rock") {
alert("Draw")
} else if (computerSelection === "Scissors") {
alert("You win!")
} else {
alert("Something went wrong")
}
}
function game() {
for (let i = 0; i < 2 ; i++) {
// your code here!
playRound();
}
}
game();
Move the line const computerSelection = getComputerChoice();
into the function playRound()
and get rid of the parameter of that function:
// THIS FUNCTION DECIDES IF THE COMPUTER CHOOSES ROCK, PAPER OR SCISSOS function getComputerChoice() { var rand = Math.floor(Math.random() * 10); if (rand <= 3) { return "Rock" } else if (rand <= 6) { return "Paper" } else { return "Scissors" } } // TESTING PLAYROUND FUNCTION function playRound() { // I PUT THE COMPUTER CHOICE FUNCTION IN A CONST BOX const computerSelection = getComputerChoice(); if (computerSelection === "Paper") { alert("You lose; Paper beats Rock") } else if (computerSelection === "Rock") { alert("Draw") } else if (computerSelection === "Scissors") { alert("You win;") } else { alert("Something went wrong") } } function game() { for (let i = 0; i < 2; i++) { // your code here! playRound(); } } game();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.