JavaScript Can I define arbitrary rules(something bigger than another) for variables that i can later compare in if/else statements in JavaScript?

I'm writing a rock-paper-scissors-like game in JavaScript. I have it somehow working but it's done more like writing out all the cases rather than making it do comparisons. Looks something like this:

var weapons = ["bomb", "knife", "pistol"];


var player = prompt("Choose your weapon: bomb, knife or a pistol?");
console.log("You picked a " + player + ".");

var  = weapons[Math.floor(Math.random() * weapons.length)];
console.log(" picked a " +  + ".");


while (player == ) {
    var player = prompt("You chose " + player + " and he chose " +  + 
    " - the same weapon. Draw is not an option, so " +
    "try again! Pick bomb, knife or pistol.");
    console.log('...');
    var  = weapons[Math.floor(Math.random() * weapons.length)];
    console.log(" picked a " +  + ".")
}

if (player == "bomb" &&  == "knife") 
    console.log( `...`);

else if (player == "bomb" &&  == "pistol")
    console.log(`...`);

else if (player == "knife" &&  == "pistol")
    console.log('...');

else if (player == "knife" &&  == "bomb")
    console.log(`...`);

else if (player == "pistol" &&  == "bomb")
    console.log(`...`);

else if (player == "pistol" &&  == "knife")
    console.log(`...`);

else
    console.log(`...`);

Is there a way to store rules like (bomb > knife && bomb < pistol) into something and then pass it over to if/else for comparisons? Or maybe I shouldn't do it even if it's possible?

Answer:1

You could use an object with the decisions as tree

decisions = {
    bomb: {
        bomb: function () { },
        knife: function () { },
        pistol: function () { }
    }, knife: {
        bomb: function () { },
        knife: function () { },
        pistol: function () { }
    }, pistol: {
        bomb: function () { },
        knife: function () { },
        pistol: function () { }
    }
};

Access via

decisions[player][computer]()
Answer:2

Im editing a HTML book not coded by me from the start. Images are set as div backgrounds with base64 format like this one <div class="cntnt" style="background-image: url(data:image/jpeg;base64,/...

Im editing a HTML book not coded by me from the start. Images are set as div backgrounds with base64 format like this one <div class="cntnt" style="background-image: url(data:image/jpeg;base64,/...

I am trying to get the range of numbers using recursion. Can someone explain to me why it isn't working? function range(x,y){ var results = []; if(x === y){ return results; } ...

I am trying to get the range of numbers using recursion. Can someone explain to me why it isn't working? function range(x,y){ var results = []; if(x === y){ return results; } ...

I'm using Facebook login feature & transferring some variables to other page through ajax. This particular ajax code isn't working, data is not going through. However I've other ajax code in other ...

I'm using Facebook login feature & transferring some variables to other page through ajax. This particular ajax code isn't working, data is not going through. However I've other ajax code in other ...

  1. going through data too fast
  2. going through data synonym
  3. going through data

I want to assign some properties to an array, but only if they are array indices. Otherwise some implementations might switch the underlying structure to a hash table and I don't want that. For ...

I want to assign some properties to an array, but only if they are array indices. Otherwise some implementations might switch the underlying structure to a hash table and I don't want that. For ...

  1. check property name javascript
  2. check property name
  3. check property name c#
  4. check property name js
  5. check property owner name
  6. check if property name exists javascript
  7. check object property name
  8. check property tax by name
  9. check property ownership by name
  10. property name check online
  11. c# check if property name exists
  12. javascript check object property name