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

  • SEARCH
  • Home
  • 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 8543393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:17:28+00:00 2026-06-11T12:17:28+00:00

I asked a question not long ago about the same batch of code, but

  • 0

I asked a question not long ago about the same batch of code, but this time it’s a different problem. Using the tips I got about that different problem, I tried to solve this problem, which is my score function in my JavaScript blackjack game. My score function keeps printing out an NaN when I test it out on the Mozilla Scratchpad. I’ve tried tweaking the returns, merging the two for loops, and even renaming the variables in my deal function to make sure it doesn’t mess with other variables, and still nothing. There was a similar question that somebody asked, but it didn’t solve my problem.

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

var cardNames = {1:"Ace", 2:"2", 3:"3", 4:"4", 5:"5", 6:"6", 7:"7", 8:"8", 9:"9", 10:"10", 11:"Joker", 12:"Queen", 13:"King"};
var suitNames = {1:"Clubs", 2:"Diamonds", 3:"Hearts", 4:"Spades"};

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

function Hand(){
    var cards = [];
    var card1 = deal();
    var card2 = deal();
    cards = [card1, card2];
    this.getHand = function () {
        return cards;
    };
    this.score = function () {
        var points;
        for (i = 0; i < cards.length; i++) {
            points = points + cards[i].getValue();
        }
        for (i = 0; i < cards.length; i++) {
            if (points > 21 && cards[i].getValue() === 11) {
                points = points - 10;
            }
        } return points;
    };
    this.printHand = function () {
        for (i = 0; i < cards.length; i++) {
            var hand;
            if (i === 0) {
            hand = cardNames[cards[i].getNumber()] + " of " + suitNames[cards[i].getSuit()];
            } else {
            hand = hand + " and a " + cardNames[cards[i].getNumber()] + " of " + suitNames[cards[i].getSuit()];
            }
        } return hand;
    };
    this.hitMe = function () {
        cards.push(deal());
    };
}

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

var playAsUser = function () {
    var playUser = new Hand();
    var decision = confirm("Your hand is " + playUser.printHand() + ". Click OK to hit or Cancel to stand");
    for (i = 0; decision !== false; i++) {
        playUser.hitMe();
        decision = confirm("Your hand is " + playUser.printHand() + ". Click OK to hit or Cancel to stand");
    }
    this.printHand = function () {
    return playUser.printHand();
    };
    this.score = function () {
    return playUser.score();
    };
    return playUser;
};

var declareWinner = function (userHand, dealerHand) {
    if ((userHand.score < dealerHand.score) || userHand.score > 21) {
        alert("You lose.");
    } else if (userHand.score > dealerHand.score) {
        alert("You win.");
    } else {
        alert("You tied.");
    }
};

var playGame = function () {
    user = playAsUser();
    dealer = playAsDealer();
    declareWinner(user, dealer);
    alert("User got " + user.printHand() + " for a total score of " + user.score());
    alert("Dealer got " + dealer.printHand() + " for a total score of " + dealer.score());
};

playGame();
  • 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-06-11T12:17:29+00:00Added an answer on June 11, 2026 at 12:17 pm

    You just need to initialise the value of points in your score function. Change var points; to var points = 0; and you should be fine.

    EDIT: Since I’m sure this will be your next question, you also need to actually call the score function in declareWinner. You are using player.score instead of player.score() in multiple places.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I asked a similar question to this not long ago, but I'm
Somebody asked similar question not long ago. But nobody answered comprehensively. Assume I have:
This is a follow-up question to a different question I asked not too long
(note this is similar to but not the same as the question I asked
I asked a question not too long ago about multi-dimensional and jagged arrays in
I was asked this question not too long ago, and didn't have a good
This is somewhat related to a question I asked not too long ago today.
I have asked this question before but did not get the satisfied answer as
Hello I asked this question to superuser but I did not get a good
I am afraid this question is not supposed to be asked here but I

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.