0

I'm coding a Discord bot with the javascript API, and I have an idea that I'm not sure would work, but would be a massive optimization.

Right now i'm adding a Rock Paper Scissors minigame into my bot, and I need it comparing the users guess (A sent emoji in the chat) to its own guess (A random number between 0-2 to represent the emoji of choice), and for now i have a line of if else doing that.

e.g, instead of

if(botGuess == 0 && msg.content.includes("/*unicode fist*/"){
  //tie
}

I put it in a Switch statement... kinda like this??

switch(botGuess, msg){
   case botGuess = 0, msg.content.includes("/*unicode fist*/"):
     //tie
   break;
}

I've just been wondering for a while, is this possible, and if so am I doing it right? As of now I have alot of if else statements, and switching to an advanced switch statement like the one I thought of above would give me ease of mind.

EDIT: I've found a workaround thanks to Simperfy. I'm concatenating both variables into a string and treating them both as one variable.

e.g.

var guesses = botGuess + "|" + msg.content;

switch(guesses){
  case guesses.contains("0" && "/*unicode_fist*/"):
    //tie
  break;
}

Boye
  • 3
  • 3
  • I don't understand how the two codes relate to each other, the first does something if both values are truthy, the second one goes over their specific values... or at least as an idea. Could you show an example with dummy data? PS... isn't the first one less typing and more readable? – Roko C. Buljan Aug 16 '20 at 08:49
  • @Simperfy yes, actually it does, thank you. However i'll still be updating the question to be more specific. – Boye Aug 16 '20 at 18:16

1 Answers1

0

A switch statement can only support a single unit of data. What you could do however, is concatenate two units of data into one. In javascript you can do this by basic multiplication:

switch (i + 1000 * j)
{
  case 1002: ... // i = 2, j = 1
  case 3004: ... // i = 4, j = 3
}

Note that this requires that i < 1000 and no negative numbers etc. Generally speaking a pattern like this isn't recommended. It is also simply not a clean style in any way, and I find it hard to imagine a situation where you would ever need something like this. I suggest editing your question to specify your scenario.

Hi - I love SO
  • 615
  • 3
  • 14