Why is computerChoice coming back as undefined in the console.log???

console.log("Let the games begin!");

var wins = 0; var ties = 0; var losses = 0;

function startGame() { var userChoice = prompt("Rock, Paper, or Scissors to play against the computer!");

console.log("You've made your choice!", userChoice);

var options = ["Rock", "Paper", "Scissors"];

var ranDecimal = Math.floor(Math.random) * options.length;

var ranInt = Math.floor(ranDecimal);

var computerChoice = options[ranInt];

console.log("Computer Has Chosen ", computerChoice);

if (userChoice === computerChoice){ alert("tie game") ties++; } else if ( (userChoice==="Rock" && computerChoice==="Scissors") || (userChoice==="Paper" && computerChoice==="Rock") ||
(userChoice==="Scissors" && computerChoice==="Paper") ){ alert("You won the game"); wins++;
} else { alert("You Lost the Game!") losses++; }

alert("total: ",+ "wins: "+ wins,+"ties: ",+ ties,+ "losses: ",+ losses);

var playAgain = confirm("play again??"); if (playAgain) { startGame(); } }

startGame();

I got this,…
https://mobdro.onl/

Your code above has syntax errors you need to fix first. If I comment out the section with the syntax error, then I get the undefined for computerChoice.

The line causing the issue is:

var ranDecimal = Math.floor(Math.random) * options.length;

Add a console.log(ranDecimal) to check ranDecimal’s value.

looks typo Math.random should be Math.random() and you want to get floor value after multiply it to optons.length, otherwise it will be always 0.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.