re-Naming variables by properties (JavaScript)
<p>In a JavaScript code, I am trying to re-name an object by its stored data.  I tried useing pathTo as suggested by this site (<a href="http://thedesignspace.net/MT2archives/000381.html" rel="nofollow">http://thedesignspace.net/MT2archives/000381.html</a>), but my console returns "ReferenceError: 'pathTo' is undefined".  My code looks something like this:</p>

<pre><code>// 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&lt;52 ; i++ ) {
    deck.push( i );
};
for ( i=51 ; i&gt;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&gt;38) {
        suit = "Diamonds";
    }else if(card&gt;25) {
        suit = "Clubs";
    }else if(card&gt;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);
</code></pre>

<p>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.</p>

<p>Here are some examples of what my console is printing:</p>

<pre><code>[ 19, 46 ]
ReferenceError: 'pathTo' is undefined
</code></pre>

<p>and</p>

<pre><code>[ 31, 18 ]
ReferenceError: 'pathTo' is undefined
</code></pre>

<p>There MUST be a way to do this, I just cannot find how.</p>

<p>In fuction 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.</p>

<p>EDITEDITEDIT_SOLVED:
I have done it!  It turns out that I don't need to change the name, the code will keep it seperate for me somehow.  All I needed to do so that it runs correctly is this:</p>

<p>Replace</p>

<pre><code>var Card = function() { 
    this.face = theFace(this); 
    this.suit = theSuit(this); 
    this.value = theValue(this); 
};
</code></pre>

<p>with</p>

<pre><code>var Card = function(card) {
    this.face = theFace(card);
    this.suit = theSuit(card);
    this.value = theValue(card);
};
</code></pre>

<p>and</p>

<pre><code>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;   
};
</code></pre>

<p>with</p>

<pre><code>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;
};
</code></pre>

<p>Thanks for the help!</p>
