-2

So I'm creating a Blackjack game via Javascript and what I'm trying to do is set up all the cards and add it to the deck. When I tried this and checked in the console, it did not work and I can't seem to find what I did wrong. Am I on the right track here? I'll post what I have. I'm just getting started. Any help would be appreciated. Thanks.

var card = function(value, name, suit) {
  this.value = value;
  this.name = name;
  this.suit = suit;
}

var deck = function() {
  this.names = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
  this.suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs'];
  this.values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 11];
  var cards = [];
  for (var s = 0; s < this.suits.length; s++) {
    for (var n = 0; n < this.name.length; n++) {
      cards.push((this.values[n], this.names[n], this.suits[s]));
    }
  }
  return cards;
}
Sarjan Desai
  • 3,683
  • 2
  • 19
  • 32
  • 1
    Why do you declare all functions with `var`? With that syntax, the functions are not available prior to being defined. – Tomáš Zato Oct 16 '15 at 15:10
  • @TomášZato do you mind expanding your comment ? Curious about that. – Onilol Oct 16 '15 at 15:12
  • @Onilol: http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname/22173438#22173438 – T.J. Crowder Oct 16 '15 at 15:12
  • How are you calling `deck`? – T.J. Crowder Oct 16 '15 at 15:13
  • 1
    `for (var n = 0; n < this.name.length; n++) {` You're missing the `s` on `names` there. – T.J. Crowder Oct 16 '15 at 15:13
  • @T.J.Crowder bless you ! – Onilol Oct 16 '15 at 15:13
  • Please don't close this, I already have half the answer written down. Give newbie a chance... – Tomáš Zato Oct 16 '15 at 15:14
  • There are some scopes issues here. Specifically your use of the `this` keyword. There are no constructors, meaning `this` is your window object. So in your card function (which you aren't calling anywhere), you are constantly overwriting the previous values. – lintmouse Oct 16 '15 at 15:20
  • @Onilol I added explanation to the bottom of my answer. – Tomáš Zato Oct 16 '15 at 15:25
  • 2
    @T.J.Crowder You're right, though there was one more mistake, which is that `names` and `values` had different length. But I thought he deserves a chance to get some hints to this code, even though I understand this is not general purpose of SO. But unlike many other beginers, he at least tried to write code... – Tomáš Zato Oct 16 '15 at 15:28
  • @dustmouse: We don't know that `deck` isn't being called as a constructor. It would work, since it returns a non-null object reference, which will supplant the object created by `new`. – T.J. Crowder Oct 16 '15 at 15:38
  • 1
    @T.J.Crowder - I was referring to how the card function is being called. Well, it isn't being called at all, but where OP is trying to call it (in `cards.push()`), they aren't using a `new` keyword. – lintmouse Oct 16 '15 at 15:44
  • 1
    @dustmouse: Quite, that `push` line is going to end up pushing `'Hearts'`, `'Diamonds'`, `'Spades'`, or `'Clubs'` onto the `cards` array each time. Gotta love that comma operator. :-) – T.J. Crowder Oct 16 '15 at 15:53
  • @T.J.Crowder - the blessing and curse of JavaScript. – lintmouse Oct 16 '15 at 15:57

1 Answers1

2

Seeing you're new to javascript, I'd propose a design pattern change:

function Card(value, name, suit){
    this.value = value;
    this.name = name; 
    this.suit = suit;
}
// Allows to print nice card name
Card.prototype.toString = function() {
    if(this.value==11 && this.suit == "Spades")
        return "Ace of spades";
    else
        return this.suit+" "+this.name;
}


function Deck() {
     this.cards = [];
}

Deck.prototype.createAllCards = function() {
     for (var s = 0; s < Deck.suits.length; s++) {
         for (var n = 0; n < Deck.names.length; n++) {
            this.cards.push(new Card(Deck.values[n], Deck.names[n], Deck.suits[s]));
         }
     }
}
// These are so called static properties on OOP
// We can just assign those values to Deck() function
// Because they never ever change
Deck.names = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
Deck.values = [2,   3,   4,   5,   6,   7,   8,   9,   10,   10,  10,  10,  11];
Deck.suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']; 


// Initialise

var deck = new Deck();
deck.createAllCards();
console.log(deck.cards);

Also, don't initialise functions using var if you can avoid it. Try this two code samples to see why:

  1. smile()
    function smile() {console.log(":)")};
    
  2. smile()
    var smile = function() {console.log(":)")};
    

Only #1 works.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778