JSFiddle - React, Tailwind, and code Playground

by megarameno

HTML

<div id="demo"></div>

JavaScript

var gameoptions = ["rock", "paper", "scissors", "gun"];
var gamerelations = [[0,2],[2,1],[1,0]];
var dialog = ["Do you choose rock, paper or scissors?", 
			  "wins!", "It's a tie!", 
			  "<small>you lose.</small> bang!", 
			  "how unfair!", 
			  "<small>smarty pants, eh?</small> pick again."];

var compare = function() {

	// user choice
	user = prompt(dialog[0]);
	var choice1 = false;
	var i=0;
	while (i<gameoptions.length) {
        console.log("user = "+user);
        console.log("gameoptions["+i+"] = "+gameoptions[i]);
		
        if(gameoptions[i]==user){
			console.log("match!");
            choice1 = i;
        }
		i++;
	}
	
	// AI choice
	var choice2 = Math.floor(Math.random()*(gameoptions.length));
	
	
	//who wins? 
	var m=0;
    var dialogtoreturn = "";
	while (m<gamerelations.length) {
        console.log("choice1 = "+ choice1);
        console.log("choice2 = "+ choice2);
        console.log("gamerelations["+m+"] = "+ gamerelations[m]);
		if( (choice2 === gamerelations[m][0] && choice1 === gamerelations[m][1]) || 
			(choice2 === gamerelations[m][1] && choice1 === gamerelations[m][0]) ){
			dialogtoreturn = gameoptions[gamerelations[m][0]]+" "+dialog[1]; 
			// the mapping has first possition as winning one so gamerelations[m][0] == always the winning one
		}else if(choice2 === 3 || choice1 === 3){
			if(choice2 === 3){
				dialogtoreturn = dialog[3]; //gun
			}else{
				dialogtoreturn = dialog[4]; //gun
			}
			
		}else{
            if(choice1 == choice2){
                dialogtoreturn = dialog[2];
            }else{
			    dialogtoreturn = dialog[5];
            }
		}
		m++;
	}
    return dialogtoreturn;

};

document.getElementById("demo").innerHTML=compare();