This functionality is for a Monopoly board game.  In particular, when the player lands on Chance or Community Chest, a random card is drawn with a particular set of instruction, a bonus could be paid out, or perhaps the player is fined. The two cards are different in that one is a set of 16 cards and the other a set of 14. How would you make this code more efficient?Chancefunction chanceCard() {    var x = Math.floor(Math.random() * ((16 - 1) + 1) + 1);    var title = chancecards['chance' + x].title;    var type = chancecards['chance' + x].type;    var bill = chancecards['chance' + x].bill;    var bonus = chancecards['chance' + x].bonus;    if (type == bill) {        updateBalance(-, bill);    } else if (type == bonus) {        updateBalance(+, bonus);    } else if (type == move) {        var newposition = chancecards['chance' + x].newposition;        var currentposition = players[player].currentpos;        if (newposition == 40) { //this if the player has to advance to go            updateBalance(+, 200);        } else if (newposition < currentposition) { //if the new position is less than the current one it means the player has to go past go            updateBalance(+, 200);        }        players[player].prevpos = players[player].currentpos;        players[player].currentpos = newposition;        players[player].startpos = players[player].currentpos;        movePiece();        checkForSale();    } else if (title == Go back 3 spaces) {        players[player].prevpos = players[player].currentpos;        players[player].currentpos -= -3;        players[player].startpos = players[player].currentpos;        movePiece();        checkForSale();    }    flipCard(Chance);}Community Chestfunction chestCard() {    var x = Math.floor(Math.random() * ((14 - 1) + 1) + 1);    var title = chestcards['chest' + x].title;    var chesttype = chestcards['chest' + x].type;    var bill = chestcards['chest' + x].bill;    var bonus = chestcards['chest' + x].bonus;    if (chesttype == bill) {        updateBalance(-, bill);    } else if (chesttype == bonus) {        updateBalance(+, bonus);    } else if (chesttype == move) {        var newposition = chestcards['chest' + x].newposition;        var currentposition = players[player].currentpos;        if (newposition == 40) { //this if the player has to advance to go            updateBalance(+, 200);        } else if (newposition < currentposition) { //if the new position is less than the current one it means the player has to go past go            updateBalance(+, 200);        }        players[player].prevpos = players[player].currentpos;        players[player].currentpos = newposition;        players[player].startpos = players[player].currentpos;        movePiece();        checkForSale();    }    flipCard(Community Chest);}CreateCardsfunction createCards() {    chancecards = {        chance1: {            title: Advance to go,            type: move,            newposition: 40        },        chance2: {            title: Advance to London,            type: move,            newposition: 39        },        chance4: {            title: Your ass is going to jail,            type: move,            newposition: 10        },        chance9: {            title: Advance to Rome,            type: move,            newposition: 24        },        chance10: {            title: Advance to Charles de Gaulle,            type: move,            newposition: 15        },        chance11: {            title: Advance to Amsterdam,            type: move,            newposition: 11        },        chance6: {            title: Go back 3 spaces,            type: movex,            newposition: -3        },        chance14: {            title: No drink and driving mate1,            type: bill,            bill: 20        },        chance15: {            title: Get out of Jail free card,            type: bill,            bill: 150        },        chance7: {            title: Pay school fees,            type: bill,            bill: 150        },        chance12: {            title: Speeding fine,            type: bill,            bill: 150        },        chance5: {            title: Bank pays you dividend,            type: bonus,            bonus: 40        },        chance13: {            title: You have won the competition,            type: bonus,            bonus: 200        },        chance16: {            title: Your building loan matures,            type: bonus,            bonus: 200        },        chance3: {            title: You are assessed for street repairs $40 per house $115 per hotel,            type: billx        },        chance8: {            title: House repairs $25 per house $100 per hotel,            type: billx        }    };    chestcards = {        chest1: {            title: Advance to go,            type: move,            newposition: 40,            bonus: 200        },        chest2: {            title: Advance to Cairo,            type: move,            newposition: 1        },        chest3: {            title: Go to Jail,            type: move,            newposition: 10        },        chest4: {            title: Pay hospital fees,            type: bill,            bill: 100        },        chest5: {            title: Pay doctor fees,            type: bill,            bill: 50        },        chest6: {            title: Pay insurance premium,            type: bill,            bill: 50        },        chest7: {            title: Bank error. Collect $200,            type: bonus,            bonus: 200        },        chest8: {            title: Annuity matures. Collect $100,            type: bonus,            bonus: 100        },        chest9: {            title: You inherit $100,            type: bonus,            bonus: 100        },        chest10: {            title: From sale of stock you get $50,            type: bonus,            bonus: 50        },        chest11: {            title: Preference shares: $25,            type: bonus,            bonus: 25        },        chest12: {            title: You have won second prize in a beauty contest. Collect $10.,            type: bonus,            bonus: 10        },        chest13: {            title: It is your birthday. Collect $10.,            type: bonus,            bonus: 10        },        chest14: {            title: You win the lottery. Collect $10,            type: bonus,            bonus: 10        }    };}
Monopoly game cards
javascript;game
Card representationI offer three important tips to improve the representation of the cards:The draw-a-card-from-the-deck functions should have nothing to do with updating balances, moving pieces, etc.  (In technical terms, chanceCard() and chestCard() violate the Single Responsibility Principle.)JavaScript lets you store code as a kind of data.  (In technical terms, functions are first-class objects in JavaScript.)Giant switch blocks (like the if-elseif-elseif- chain in chanceCard()) are a hint to use objects instead.  (In technical terms, apply the Replace Conditionals With Polymorphism refactoring rule.)These indicators suggest that the action for the cards should be encoded in the cards themselves.  Since you don't want to write a function per card, though, you'll want to classify the cards into several types, and define the functions in their prototypes.Note that bill and bonus cards are really just the same thing, except that bills have a negative impact on the player's balance and bonuses have a positive impact.Codefunction updateBalance(player, amount) {    player.balance += amount;}function movePiece(player, direction, destination) {    if (direction > 0 && destination != jail && destination < player.currentpos) {        // Moving forward to or past Go, but not going to jail        updateBalance(player, +200);    }    player.currentpos = destination;    checkForSale();}function drawCard(player, deck) {    var card = deck.shift();    // console.log(card.title);    card.act(player);    deck.push(card);}function shuffle(deck) {    // Fisher-Yates shuffle    for (var i = deck.length - 1; i > 0; i--) {        var j = Math.floor((i + 1) * Math.random());        var swap = deck[i];        deck[i] = deck[j];        deck[j] = swap;    }}//////////////////////// CARD TYPES ////////////////////////function AbsMoveCard(title, destination) {    this.title = title;    this.destination = destination;}AbsMoveCard.prototype.act = function(player) {    // All logic about current position, bonus for passing Go, and checking    // for property sale should be included in movePiece().    movePiece(player, +1, this.destination);};function RelMoveCard(title, distance) {    this.title = title;    this.distance = distance;}RelMoveCard.prototype.act = function(player) {    // All logic about current position, bonus for passing Go, and checking    // for property sale should be included in movePiece().    movePiece(player, this.distance, (player.currentpos + 40 + this.distance) % 40);}function MoneyCard(title, amount) {    this.title = title;    this.amount = amount;}MoneyCard.prototype.act = function(player) {    updateBalance(player, this.amount);}function AssessmentCard(title, perHouse, perHotel) {    this.title = title;    this.perHouse = perHouse;    this.perHotel = perHotel;}AssessmentCard.prototype.act = function(player) {    updateBalance(player, player.houseCount * this.perHouse +                          player.hotelCount * this.perHotel);}/////////////////////////// CARDS ///////////////////////////var chanceCards = [    // Go should be position 0 rather than 40    new AbsMoveCard(Advance to go, 0),    new AbsMoveCard(Advance to London, 39),    new AbsMoveCard(Your ass is going to jail, jail),    new AbsMoveCard(Advance to Rome, 24),    new AbsMoveCard(Advance to Charles de Gaulle, 15),    new AbsMoveCard(Advance to Amsterdam, 11),    new RelMoveCard(Go back 3 spaces, -3),    new MoneyCard(No drink and driving mate!, -20),    new MoneyCard(Get out of Jail free card, -150),    new MoneyCard(Pay school fees, -150),    new MoneyCard(Speeding fine, -150),    new MoneyCard(Bank pays you dividend, +40),    new MoneyCard(You have won the competition, +200),    new MoneyCard(Your building loan matures, +200),    new AssessmentCard(You are assessed for street repairs $40 per house $115 per hotel, -40, -115),    new AssessmentCard(House repairs $25 per house $100 per hotel, -25, -100),];var chestCards = [    new AbsMoveCard(Advance to go, 0),    new AbsMoveCard(Advance to Cairo, 1),    new AbsMoveCard(Go to Jail, jail),    new MoneyCard(Pay hospital fees, -100),    new MoneyCard(Pay doctor fees, -50),    new MoneyCard(Pay insurance premium, -50),    new MoneyCard(Bank error. Collect $200, +200),    new MoneyCard(Annuity matures. Collect $100, +100),    new MoneyCard(You inherit $100, +100),    new MoneyCard(From sale of stock you get $50, +50),    new MoneyCard(Preference shares: $25, +25),    new MoneyCard(You have won second prize in a beauty contest. Collect $10., +10),    new MoneyCard(It is your birthday. Collect $10., +10),    new MoneyCard(You win the lottery. Collect $10, +10),];shuffle(chanceCards);shuffle(chestCards);Example usagevar player1 = { currentpos: 0, balance: 200, houseCount: 0, hotelCount: 0 };drawCard(player1, chanceCards);Additional remarksIt would be more natural to represent Go as position 0 rather than position 40.  You could then use the modulo 40 operation to maintain the illusion of a cyclical board.You designated board position 10 as the jail.  However, I would prefer to think of board position 10 as the Just Visiting space rather than the In Jail portion.  One or the other will need special treatment, and since the jail requires special handling anyway, it seems appropriate that it should be the exceptional case.  Here, I've used the string jail to represent that position, but deeper consideration may be worthwhile.Picking a random element from the array to doesn't accurately simulate a card draw in a board game.  I think it would be more appropriate to shuffle the cards initially, draw from the top of the deck, and replace the cards at the bottom after use.