Javascript Arrays and Strings


#1

I have a question about how strings an arrays work in a particular project I'm working on outside of Codeacademy.

The project's code is really long so I'll just retype the gist of the problem here, in a different code. I want a user to be able to type in the string (e.g. acespade) of the card they wish to select, but how do I get the function to respond that way, regardless of what they enter?

For instance, the deck of cards is dealt (spliced) into the hand, then it becomes player one's turn. If they choose to draw a card, one card from the deck of cards moves into the hand, and the turn ends. Else, if the user chooses to play a card, they will select their card of choice, and it moves into the pile. Otherwise, it's an invalid move.

Thanks for your help in advance. :slight_smile:

/*start writing code below*/
var DeckofCards = ["acespade","twodiamond","threeclubs","fourhearts","queenhearts","jackspades","kingdiamond"];
var Hand = DeckofCards.splice(0,5);
var Pile= [];
function playerOneTurn(){
var move = prompt("Player One, make your move: DRAW or PLAY").toUpperCase();
if(move == "DRAW"){
Hand.push(DeckofCards[0]);
  DeckofCards.splice(0,1);
}
else if(move == "PLAY"){
var PickaCard= prompt("Choose a card: "+Hand);
  //the next sequence in this function reads the input and scans for the matching string in Hand. If it fails to find a match, it will return an error and ask again. If it matches, place that card into the pile and the turn ends.
        console.log("End of turn.");
  }
else {
alert("Invalid move.");
}
};
playerOneTurn();

#2

have you considered using indexOf()?


#3

Doesn't that just return the index of the array's element?


#4

What if the user has two of the same card, are you going to initiate a pointer that will switch between two cards in the hand with the same name?


#5

Looks like you are only using one of each card name. Why not just use a for loop through your hand array matching each value with the hand variable.

You may want to store all matches into a temp array (sorry I keep assuming you'd have multiple of the same name).


#6

yes, but it gives you the possibility to check if the item is in the list, once you have the index you can store the card in a variable (retrieve from array using index), slice it from the array, and add it to deck (or whatever what you want do)


#7

No, actually. The nature of the game is such that if you have two identical cards, they can and must be played at the same time, if that ever came up.


#8

So I actually used the array.includes(parameter) to check if an item exists in the array. That seemed easier, but now I have to get it to take that specified input and get the function to act specifically on that. I.E. if the user inputs acespades, I want it to only affect acespades cards.


#9

indexOf also validates if the element is in the array, and you get the index which you can use for the rest of your program


#10

I would do this then

// User Choice (String)
var PickaCard= prompt("Choose a card: "+Hand);

// Initialize Turn-Temporary Array of Cards to play
var PlayaCard = [];

// Check each card in Hand for match with PickaCard Prompt
for (var s=0; s<=Hand.length; s++) {
  // If match is found add to PlayaCard
  if (Hand[s] === PickaCard) {
    PlayaCard.push(Hand[s]);
  } else {}
}

// Update Hand
for (var s=Hand.length; s>0; s--) {
  if(Hand[s] === PickaCard) {
    Hand.splice(s, 1);
  } else {}
}

Run this test block in browser console with the pre-set variables below. After it runs Print Hand and PlayaCard to see it work like you wanted.

var PickaCard = "test2";
var Hand = ["test1", "test2", "test2", "test2"];
var PlayaCard = [];

for (var s=0; s<=Hand.length; s++) {
  // If match is found, move to PlayaCard
  if (Hand[s] === PickaCard) {
    PlayaCard.push(Hand[s]);
  } else {}
}

for (var s=Hand.length; s>0; s--) {
  if(Hand[s] === PickaCard) {
    Hand.splice(s, 1);
  } else {}
}

Problems I encountered were, trying to update the Hand array during for loop. After adding match to PlayaCard, I used your splice method to remove from Hand. This however was confusing my for loop which assumed Hand.length, and by removing indeces during the loop, it was returning undefind near the end of the cycle. This can be avoided by using a negative iterating for loop, which is what I did in the second one.

I split up the two for loops for simplicity, one to check for matches, and the other to update Hand.


#11

I tested the code, and it seems to work for everything except the first element.

console.log(PickaCard); //returns test1
console.log(Hand); //returns test1, test2, test3, test4

edit: I fixed this by making the negative iteration inclusive. I'll try to put this into my project and see if it works.


#12

My fault, I made the second loop only test if s is greater than 0, which 0 would be the first index of Hand. Set to s>=0.