I'm not very proficient with javascript so I decided to do an online course from scratch and they put me as an exercise to make a paper or scissors stone game, so I did it but I wanted to add html so the user could insert the option of your preference and the variable travels well, is received by javascript and interpreted, if I ask you to throw me the results through a console.log it does it without problems but if I decide to change the console.log of the function play () by an alert, the alert on the contrary only throws me undefined. I would like to be able to show the result of the game or the play () function in html either through an alert or a text. Next the code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS NAME</title>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script>
var userInput = null;
function sub(){
userInput = document.getElementsByName("prod")[0].value;
userInput = userInput.toLowerCase();
function getComputerChoice(){
var randomNumber = Math.floor(Math.random() * 3);
//console.log(randomNumber);
switch(randomNumber){
case 0:
return 'piedra';
case 1:
return 'papel';
case 2:
return 'tijeras';
default:
return 'error';
}
}
var computerChoice = getComputerChoice();
/*console.log('Computadoraasd: ' + computerChoice);
console.log('Usuarioasd: ' + userInput);*/
function determineWinner(){
if(userInput === computerChoice){
console.log('Empate');
}else if(userInput==='piedra' && computerChoice==='papel'){
console.log('Gana la computadora');
}else if(userInput==='papel' && computerChoice==='piedra'){
console.log('Gana el usuario');
}else if(userInput==='papel' && computerChoice==='tijeras'){
console.log('Gana la computadora');
}else if(userInput==='tijeras' && computerChoice==='papel'){
console.log('Gana el usuario');
}else if(userInput==='tijeras' && computerChoice==='piedra'){
console.log('Gana la computadora');
}else if(userInput==='piedra' && computerChoice==='tijeras'){
console.log('Gana el usuario');
}else if(userInput==='bomba'){
console.log('Gana el usuario con trampa');
}else{
console.log('No computado');
}
}
function jugar(){
console.log('Computador: ' + computerChoice);
console.log('Usuario: ' + userInput);
console.log(determineWinner(userInput, computerChoice));
}
console.log(jugar());
}
</script>
</head>
<body>
<h2>Ejila su opción</h2>
<form name="form" action="" method="get">
<input type="text" name="prod">
</form>
<button onclick="sub()">enviar</button>
</body>
</html>