JSFiddle - React, Tailwind, and code Playground
HTML
<button id="start">Start Game</button>
JavaScript
function numberToName(computer_throw){
if(computer_throw === 0){
computer_throw = "Rock";
}
else if(computer_throw === 1){
computer_throw = "Paper";
}
else if(computer_throw === 2){
computer_throw = "Scissors";
}
else if(computer_throw === 3){
computer_throw = "Lizard";
}
else{
computer_throw = "Spock";
}
return computer_throw;
}
function nameToNumber(user_throw){
if(user_throw == "Rock"){
user_throw = 0;
}
else if(user_throw == "Paper"){
user_throw = 1;
}
else if(user_throw == "Scissors"){
user_throw = 2;
}
else if(user_throw == "Lizard"){
user_throw = 3;
}
else if(user_throw == "Spock"){
user_throw = 4;
}
else{
console.log("Please pick a throw.");
}
return user_throw;
}
function game(thrw){
var computer_throw = Math.floor((Math.random() * 5));
thrw = nameToNumber(thrw);
var difference = Math.abs((computer_throw - thrw) % 5);
$("#result").append("<p>Player <strong>" +numberToName(thrw) +"</strong></p>");
$("#result").append("<p>Comp <strong>" +numberToName(computer_throw) +"</strong></p>");
console.log(difference);
}
$("#start").on("click", function(){
$("#result").children().remove();
var user_throw = $("#throw").val();
//game("Rock");
//game("Paper");
//game("Scissors");
//game("Lizard");
//game("Spock");
})