Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7488439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:54:49+00:00 2026-05-29T14:54:49+00:00

In a JavaScript code, I am trying to re-name an object by its stored

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T14:54:52+00:00Added an answer on May 29, 2026 at 2:54 pm

    As @mattbornski points out, there is no pathTo object 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 pathTo object like this:

    pathTo = {}; 
    

    or

    pathTo = new Object();
    

    Then, give each new Card object a suit and face before you try to add it to pathTo (or before you try to use it to look up a card). For example, you could write:

    myCard = new Card(23);
    

    or

    myCard = new Card('spade','2');
    

    Finally, when your card has both a face and a suit, add it to the pathTo object like this:

    pathTo[this.suit + this.face] = this; // use this inside the Card constructor
    

    or

    pathTo[myCard.suit + myCard.face] = myCard; // use this with an instance of a Card object
    

    Then you can look up the cards with:

    pathTo['spade2']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find URLs in some text, using javascript code. The problem is,
I'm trying to learn about this feature of javascript I keep seeing in code,
I am trying to create a countdown using javascript. I got some code from
i have this code in javascript: var object = { get: function(id){ sel =
I'm writing some Javascript code and I'm trying to change the current page as
I am trying to create a new Element in my javascript code and append
I'm trying to debug the following block of Javascript code to see what the
I am trying to call my function named isUrgencyTypeValid from my javascript code but
I have a javascript code, whereby I'm trying to load a list from a
I am trying to emulate a web browser in order to execute JavaScript code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.