I’m building the first part of the Rock-Paper-Scissors project of The Odin Project foundations course and my function playRound works but never stops running
function getComputerChoice(){
//create array of (rock, paper, scissors)
const listOfWeapons = new Array ('Rock','Paper','Scissors');
//make computer choose randomly between the objects of the array
let computerChoice = listOfWeapons[Math.floor(Math.random()*listOfWeapons.length)];
console.log(computerChoice)
}
getComputerChoice();
function playRound(playerSelection, computerSelection){
let getPlayerSelection = prompt('Choose your weapon!');
if (getComputerChoice == getPlayerSelection){
alert('It's a tie you both have choosen the same weapon')
}
for (getComputerChoice == 'Rock'; getPlayerSelection == 'Paper';){
alert('You won paper beats rock')
}
for (getComputerChoice == 'Rock'; getPlayerSelection == 'Scissors';){
alert('You lost rock beats scissors')
}
for (getComputerChoice == 'Paper'; getPlayerSelection == 'Scissors';){
alert('You won scissors beats paper')
}
for (getComputerChoice == 'Paper'; getPlayerSelection == 'Rock';){
alert('You lost paper beats rock')
}
for (getComputerChoice == 'Scissors'; getPlayerSelection == 'Rock';){
alert('You won rock beats paper')
}
for (getComputerChoice == 'Scissors'; getPlayerSelection == 'Paper';){
alert('You lost scissors beats paper')
}
}
playRound();
How can I modify this code so the function terminates when it’s done? Or should I start over?
2
Answers
You need to use if else logic not if followed by for. Replace each for with else if in your playRound function like this:
There are multiple things wrong:
if
, but you usedfor
&&
) operator to compare many thing in oneif
statementgetComputerChoice
is a function, not a stringgetComputerChoice
have return a valuegetComputerChoice
have to be called insideplayRound