Getting TypeError: Can not call the getNumber method of undefined

advertisements

I am working on a simple blackjack program. I am completely new to javascript and am having trouble debugging my program. I keep getting the TypeError: cannot call getNumber method of undefined..... and i'm totally lost. I am trying to get the numerical value stored for each card but it appears thats the error is happening in my printHand() method inside the Hand class. When printing out the hand of two or more cards, I loop through each card in the hand calling cards[i].getNumber() where cards[] is the array of cards in each hand. Am i not referencing cards[] properly? I double checked to make sure my methods and variables were set to public but I still cannot figure out why getNumber() is being called on an undefined object. Is there something wrong with the way I am referencing this object?

Here is my code:

 // Card Constructor
function Card (suit, number){
var the_suit = suit;
var the_number = number;

this.getNumber = function(){
    return the_number;
};
this.getSuit = function(){
    return the_suit;
};
this.getValue = function (){
    // face cards
    if(the_number > 10){
        return 10;

    // aces
    } else if (the_number < 2){
        return 11;

    // other cards
    } else {
        return the_number;
    }
};
}

function deal (){
// get card suit
var rand1 = Math.floor(Math.random ( ) * 4 + 1);
// get car number
var rand2 = Math.floor(Math.random ( ) * 13 + 1);

var newCard = new Card(rand1, rand2);
}

function Hand (){
// create two cards for initial hand
var card1 = deal();
var card2 = deal();
// store cards in array
var cards = [card1,card2];

// getter
this.getHand = function (){
    return cards;
};

// get the score
this.score = function(){
    var length = cards.length;
    var score = 0;
    var numAces = 0;

    for(i = 0; i < length; i++){
    if (cards[i].getValue() === 11 ){
    numAces++;
    }
        score += cards[i].getValue();
    }
    while(score > 21 && numAces !== 0){
        sum -= 10;
        numAces--;
    }
};
this.printHand = function(){
    var length = cards.length;

    for(i=0; i< length; i++){
    var string = string + cards[i].getNumber() + " of suit " + cards[i].getSuit() + ", ";
    }
    return string;
};

this.hitMe = function(){
    var newCard = deal();
    cards.push(newCard);

}
}

function playAsDealer(){
var newHand = new Hand();
while(newHand.score < 17){
    newHand.hitMe();
}
return newHand;
}

function playAsUser(){
var newHand = new Hand();
var choice = confirm("Current hand: "+ newHand.printHand() + ": Hold (Ok) or Stand(Cancel)");
while(choice){
    newHand.hitMe();
    choice = confirm("Current hand: "+ newHand.printHand() + ": Hold (Ok) or Stand(Cancel)");

}
}

function declareWinner(user, dealer){

//user wins case
if (user.score > dealer.score){
console.log("You are the Champion!");
}

// tie game
else if(user.score===dealer.score){
    console.log("Tied!");
}

else{
    console.log("Loser!!");
}

}

function playGame (){
var user = playAsUser();
var dealer = playAsDealer();
console.log("User's Hand: " + user.printHand());
console.log("Dealer's Hand: " + dealer.printHand());
declareWinner();
}

playGame();


You don't return the card object in the function "deal": should be:

function deal (){
   // get card suit
 var rand1 = Math.floor(Math.random ( ) * 4 + 1);
        // get car number
 var rand2 = Math.floor(Math.random ( ) * 13 + 1);

 var newCard = new Card(rand1, rand2);

 return newCard;
}