I’m a JavaScript noob and would like to know if there’s a better way to ask my script whether or not the card generated is already in use.
Right now, I’m using a bunch of Or operators but, there’s gotta be a less redundant way to do it. If any of you could answer my question it would truly help me in the long run. Thanks
var cards = ['Ace of heart','Ace of club','Ace of diamond','Ace of spade',
'Two of heart','Two of club','Two of diamond','Two of spade',
'Three of heart','Three of club','Three of diamond','Three of spade',
'Four of heart','Four of club','Four of diamond','Four of spade',
'Five of heart','Five of club','Five of diamond','Five of spade'];
// Distributed Cards //
var pre_flop = [1];
var flop = [2];
var turn = [0];
var river = [0];
// Cards value //
var your_card;
var indexed_cards = [6];
var total_card = 0;
var deck_size = cards.length;
function generate_card(){
// Assign value to first card //
if(total_card === 0){
your_card = Math.floor(Math.random() * deck_size);
indexed_cards [total_card] = your_card;
total_card++;
}else{
your_card = Math.floor(Math.random() * deck_size);
// Makes sure the cards are different //
while(1){
if(your_card === indexed_cards [0] || your_card === indexed_cards [1] || your_card === indexed_cards [2] || your_card === indexed_cards [3] || your_card === indexed_cards [4] || your_card === indexed_cards [5] || your_card === indexed_cards [6]){
your_card = Math.floor(Math.random() * deck_size);
}else {
indexed_cards [total_card] = your_card;
break;
}
}
total_card++;
}
}
To answer the specific question about replacing ORs:
You can use array.indexOf(..) to find out whether something is in an array.
indexOf returns -1 if an item isn’t found, else it returns the item’s position in the array. You may have also seen this method used on strings, to find the position or presence of a character in a string; same principle applies here.
Other problems with the code may be present, but you should be able to replace
with