Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write a function for a card game which will allow players to exchange a card in their hand for a card in their opponents'. I'm just starting out, so I know I'm running into a problem with accessing the arrays when I do it - my program currently gets hung up on the first step, and won't recognize the input "player1" as a match for the object player1 in the players array. Is there an easy way to do that? Do I need to add a "name" key or something similar to my player object, and have the prompt ask for that?

 function player(coins, active) {
      this.coins = coins;
      this.active = active;
      this.hand = ["Bakery", "Wheat Field"]
    }

var player1 = new player(0, true);
var player2 = new player(0, false);
var activePlayer = player1;
var allPlayers = [player1, player2];

function cardSwap() {
    var targetPlayer = prompt("Which player do you want to exchange with?");
        for (i = 0; i < allPlayers.length; i++) {
            if (allPlayers[i] === targetPlayer) {
                targetPlayer = allPlayers[i];
            } else {
            console.log ("That player doesn't exist!");
            cardSwap();
            }
        } 
    console.log(targetPlayer["hand"]);
    var targetCard = prompt("Which of their cards do you want?");
    console.log(activePlayer["hand"]);
    var giveCard = prompt("Which of your cards will you give in exchange?");
    for (i = 0; i < targetPlayer["hand"].length; i++) {
          if (targetPlayer["hand"][i] === targetCard) {
            delete targetPlayer["hand"][i];
            targetPlayer["hand"].push(giveCard);
            break;
            } else {
                console.log(targetPlayer + " doesn't have that card!");
                break;
            }
    }
    for (i = 0; i < activePlayer["hand"].length; i++) {
        if (activePlayer["hand"][i] === giveCard) {
            delete activePlayer["hand"][i];
            activePlayer["hand"].push(targetCard);
            break;
            } else {
                console.log("You don't have that card!");
                break;
            }
    }
};

cardSwap();
console.log(allPlayers);
share|improve this question

closed as off-topic by Pimgd, mdfst13, forsvarir, Quill, Mast Jul 21 at 6:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Pimgd, mdfst13, forsvarir, Quill, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.