In a JavaScript code, I am trying to re-name an object by its stored data. I tried using pathTo as suggested by this site (http://thedesignspace.net/MT2archives/000381.html), but my console returns “ReferenceError: ‘pathTo’ is undefined”. My code looks something like this:
// This code defines the Object constructor Card, used to make the card objects
var Card = function() {
this.face = theFace(this);
this.suit = theSuit(this);
this.value = theValue(this);
};
// This code creates the Deck to be used.
var deck = [];
for ( i=0 ; i<52 ; i++ ) {
deck.push( i );
};
for ( i=51 ; i>0 ; i-- ) {
var random = Math.floor(Math.random()*i);
var temp = deck[random];
deck[random] = deck[i];
deck[i] = temp;
};
// 0-12 is Spades.
// 13-25 is Hearts.
// 26-38 is Clubs.
// 39-51 is Diamonds.
// Now we create the hand of the player and dealer
var player = [];
var dealer = [];
// Now to deal a card to player
player.push(deck.pop());
dealer.push(deck.pop());
// and another
player.push(deck.pop());
dealer.push(deck.pop());
// function theFace gives the face of a card
function theFace( card ) {
var faces = ["King","Ace","2","3","4","5","6","7","8","9","10","Queen","Jack"];
return(faces[card%13]);
};
// function theValue uses 'switch' to determine points from a card
function theValue(card) {
var value = card % 13;
switch( value ) {
case(0):
case(11):
case(12):
value = 10;
break;
case(1):
value = 11;
break;
default:
value = value;
break;
};
return value;
};
// function theSuit returns the suit of a card
function theSuit(card) {
var suit;
if(card>38) {
suit = "Diamonds";
}else if(card>25) {
suit = "Clubs";
}else if(card>12) {
suit = "Hearts";
}else {
suit = "Spades";
};
return suit;
};
// function toObject the first (numbered) card of of a hand
// and turns it into an Object with the desired properties
function toObject( hand ) {
var card = hand.shift();
if (typeof(card) !== "number") {
hand.unshift(card);
} else {
var card = new Card ();
card = pathTo[card.suit + card.face];
};
return hand;
};
console.log(player);
toObject(player);
toObject(player);
console.log(player);
I am trying to rename the card I am turning from a typeof===”number” to typeof===”object” so that when I run the code multiple times (hence the function) I do not have duplicate names of the objects in the array of hands.
Here are some examples of what my console is printing:
[ 19, 46 ]
ReferenceError: 'pathTo' is undefined
and
[ 31, 18 ]
ReferenceError: 'pathTo' is undefined
There MUST be a way to do this, I just cannot find how.
In function toObject I am trying to take the first number(card) in the hand array and turn it into an object describing its qualifiers as a card of the standard 52 card deck. EDIT: I just realized I’m not even pushing it back. I am going to try something to see if it will work.
EDITEDITEDIT_SOLVED:
I have done it! It turns out that I don’t need to change the name, the code will keep it separate for me somehow. All I needed to do so that it runs correctly is this:
Replace
var Card = function() {
this.face = theFace(this);
this.suit = theSuit(this);
this.value = theValue(this);
};
with
var Card = function(card) {
this.face = theFace(card);
this.suit = theSuit(card);
this.value = theValue(card);
};
and
function toObject( hand ) {
var card = hand.shift();
if (typeof(card) !== "number") {
hand.unshift(card);
} else {
var card = new Card ();
card = pathTo[card.suit + card.face];
};
return hand;
};
with
function toObject( hand ) {
var card = hand.shift();
if (typeof(card) !== "number") {
hand.unshift(card);
} else {
var card = new Card (card);
hand.push(card);
};
return hand;
};
Thanks for the help!
As @mattbornski points out, there is no
pathToobject in JavaScript and, as far as I know, it’s not supported by any browser anywhere. That article seems to have omitted this part of their code.Based on how you’re trying to use it, though, you’ll want to make a few changes to your code:
Create a
pathToobject like this:or
Then, give each new
Cardobject a suit and face before you try to add it topathTo(or before you try to use it to look up a card). For example, you could write:or
Finally, when your card has both a face and a suit, add it to the
pathToobject like this:or
Then you can look up the cards with: