get value of user :
UserSelect.options[UserSelect.selectedIndex].textContent
get random value :
var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)];
Full Code
let UserSelect = document.querySelector("#UserSelect") UserSelect.addEventListener("change",()=>{ var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)]; var UserOption = UserSelect.options[UserSelect.selectedIndex].textContent; console.log("User selected: " + UserOption); console.log("Computer selected: " + randomOption); if(UserOption === randomOption){ console.log("It's a draw 🤝🤝"); } else if((UserOption === "Rock" && randomOption === "Scissors") || (UserOption === "Paper" && randomOption === "Rock") || (UserOption === "Scissors" && randomOption === "Paper")){ console.log("User wins 👏🎉"); } else { console.log("Computer wins 😔😔!"); } })
<div class="form-group"> <select class="form-control" id="UserSelect"> <option value="1">Rock</option> <option value="2">Paper</option> <option value="3">Scissors</option> </select> </div>
This code sets up an event listener for the change event on the UserSelect element. When the user selects an option from the dropdown, the event is triggered, and the code inside the event listener is executed.
Play()function ? I can not see this function being called anywhere in your codevalueattributes to be the actual string you want instead of a number, eg<option value="Rock">Rock</option>then you can simply usedocument.getElementById("UserSelect").value