This forum is now read-only. Please use our new forums at discuss.codecademy.com.

831 points
Ed5c16874f51958477831e7ecf06a47c?s=140&d=retro
Submitted by
drewthemunky
almost 6 years ago

3.2 - Stuck and really need some help please

Hi all, please can you help. I've been stuck on this exercise all day and it's finally defeated me. I think there is something wrong with my 'score' function, as this has never returned an actual value (it's always been undefined) and before I go any further that will need to be fixed. Any help you can give would be appreciated.

Here's the code:
// Card Constructor

function Card(s,n) {

    var suit = s;
    var number = n;

    this.getSuit = function(){
        return suit;
    };
    this.getNumber = function(){
        return number;
    };
    this.getValue = function(){
        if (this.number===1){
            return 11;
        } else if (this.number > 10 ){
            return 10;
        } else {return this.number;}
    };
}

var deal = function(){
        var randSuit = Math.floor(Math.random()*4)+1;
        var randNumber = Math.floor(Math.random()*13)+1;
        var newCard = new Card(randSuit,randNumber);
        return newCard;
        };

//Hand constructor
function Hand(){
    var myHand = [deal(),deal()];

    this.getHand = function(){
        return this.myHand;
        };

    this.score = function(){
        var total=0;
        var aceNum=0;
        for(i=0;i<myHand.length; i++){
            var value = myHand[i].getValue();
            if (value===11){
                aceNum ++;}
            total += value;
            }
            while (total>21 && aceNum>0){
                total -=10;
                aceNum--;
            }
            return total;
    };
    this.printHand = function(){
        var printOut="";
        for (i=0;i<myHand.length;i++){
        var ranks = myHand[i].getNumber();
        var suits = myHand[i].getSuit();
            if (ranks ===1){
                ranks = "Ace";
            } else if (ranks === 11){
                ranks = "Jack";
            } else if (ranks === 12){
                ranks = "Queen";
            } else if (ranks === 13){
                ranks = "King";
            }           
            if (suits===1){
                suits = "Clubs";
            } else if (suits===2){
                suits = "Diamonds";
            } else if (suits===3){
                suits = "Hearts";
            } else {suits = "Spades";}

            printOut = printOut +ranks+" of "+suits;
            if (i<(myHand.length-1)){
                printOut+= ", ";
            }
        }
        //console.log("You scored "+myHand.score);
        return printOut;
        };
//Hitme
    this.hitMe = function(){
        myHand.push(deal());    
        };
    }

var playAsDealer = function(){
    var dealerHand = new Hand();    
    while (dealerHand.score()<17){
        dealerHand.push(deal());
    }
    return dealerHand.getHand();
};

var playAsUser = function(){
    var userHand = new Hand();
    var decision = confirm("Your hand is "+userHand.printHand()+". Do you want to hit?");
    while (decision){
        userHand.hitMe();
        decision  = confirm("Your hand is "+userHand.printHand()+". Do you want to hit?");
}
    console.log("You decided to stick.");
};
//test codes for previous exercises
//var myHand = new Hand();
//myHand.hitMe();
//console.log(myHand.printHand());
//playAsDealer();
//playAsUser();

The console currently gives this an 'oops'.


1 vote

permalink

this.getValue = function(){
    if (this.number===1){
        return 11;
    } else if (this.number > 10 ){
        return 10;
    } else {return this.number;}
};

number is already defined while this.number is undefined so remove the this. and this function will work. And your playAsDealer and playAsUser functions should return their object and not just the result of getHand() or even not return at all.

3324 points
5139256f6a90a0ef75009600_690502710
Submitted by
haxor789
almost 6 years ago

1 Comment

Ed5c16874f51958477831e7ecf06a47c?s=140&d=retro drewthemunky almost 6 years ago

Thanks so much for your help. That works now so just making a few more tweaks before I'm done!


0 votes

permalink

Update: I have managed to get this working, sort of, but the scoring function still does not return an actual score, instead an 'NaN'. Here's the updated code. Running as it is returns NaN as it's only looking at the score function, running playAsUser() seems ok otherwise. Any ideas?

// Card Constructor
function Card(s,n){
    var suit = s;
    var number = n;

    this.getSuit = function(){
        return suit;
    };
    this.getNumber = function(){
        return number;
    };
    this.getValue = function(){
        if (this.number===1){
            return 11;
        } else if (this.number > 10 ){
            return 10;
        } else {return this.number;}
    };
}

var deal = function(){
        var randSuit = Math.floor(Math.random()*4)+1;
        var randNumber = Math.floor(Math.random()*13)+1;
        var newCard = new Card(randSuit,randNumber);
        return newCard;
        };

//Hand contructor
function Hand(){
    var card1 = deal();
    var card2 = deal();
    var myHand = [card1,card2];

    this.getHand = function(){
        return this.myHand;
        };

    this.score = function(){
        var total=0;
        var aceNum=0;
        for(i=0;i<myHand.length; i++){
            total += myHand[i].getValue();
            if (total===11){
                aceNum ++;}
            }
            while (total>21 && aceNum>0){
                total -=10;
                aceNum--;
            }
            return total;
    };
    this.printHand = function(){
        var printOut="";
        for (i=0;i<myHand.length;i++){
        var ranks = myHand[i].getNumber();
        var suits = myHand[i].getSuit();
            if (ranks ===1){
                ranks = "Ace";
            } else if (ranks === 11){
                ranks = "Jack";
            } else if (ranks === 12){
                ranks = "Queen";
            } else if (ranks === 13){
                ranks = "King";
            }           
            if (suits===1){
                suits = "Clubs";
            } else if (suits===2){
                suits = "Diamonds";
            } else if (suits===3){
                suits = "Hearts";
            } else {suits = "Spades";}

            printOut = printOut +ranks+" of "+suits;
            if (i<(myHand.length-1)){
                printOut+= ", ";
            }
        }
        //console.log("You scored "+myHand.score);
        return printOut;
        };
//Hitme
    this.hitMe = function(){
        myHand.push(deal());    
        };
    }

var playAsDealer = function(){
    var dealerHand = new Hand();    
    while (dealerHand.score()<17){
        dealerHand.push(deal());
    }
    return dealerHand.getHand();
};

var playAsUser = function(){
    var userHand = new Hand();
    var decision = confirm("Your hand is "+userHand.printHand()+" for a score of "+userHand.score()+". Do you want to hit?");
    while (decision){
        userHand.hitMe();
        decision  = confirm("Your hand is "+userHand.printHand()+" for a score of "+userHand.score()+". Do you want to hit?");
}
    console.log("You decided to stick.");
    return;
};

var declareWinner = function(userHand, dealerHand){
    if (userHand.score()>21){
        return "You lose!";}
    else if (dealerHand.score()>21){
        return "Delaer is bust! You win!";}
    else if (userHand.score()===dealerHand.score()){
        return "You tied!";}
    else if (userHand.score()>dealerHand.score()){
        return "You win!";}
    else if (userHand.score()<dealerHand.score()){
        return "You lose!";
    }
};

var userHand = new Hand();
console.log(userHand.score());
//playAsDealer();
//playAsUser();

831 points
Ed5c16874f51958477831e7ecf06a47c?s=140&d=retro
Submitted by
drewthemunky
almost 6 years ago


0 votes

permalink

this.getValue = function(){
        if (this.number===1){
            return 11;
        } else if (this.number > 10 ){
            return 10;
        } else {return this.number;}
    };

you don't wanna have this before number when you are using it in this function, because you declare number as private in your Card constructor.
Here is what I wrote:

this.getValue = function(){
        if(number > 10){
            return 10;
        }
        else if (number === 1){
            return 11;
        }
        else return number;
    };

520 points
D198a33085dea447baafdf6e8555c61c?s=140&d=retro
Submitted by
straszny
over 5 years ago

1 Comment

Ed5c16874f51958477831e7ecf06a47c?s=140&d=retro drewthemunky over 5 years ago

Thanks for that! I got it working eventually and posted the finished code in the forum.