0

I am working on an javascript app and having some trouble with the object constructor. I am going to make each card in the deck an object and so just working on building an array of those objects. Here is what i have :

    function card(suit1,number1) // Constructor
    {
    this.suit1 = suit1;
    this.number1 = number1;
    }


    function makeDeck1() {
    var deck1 = new Array()
  
    deck1[0]= new card('S','A');
    deck1[1]= new card('S','1');

    }


    function myFunction() {
 makeDeck1();
    alert('gman' + deck1); 
    }
    <h2>Blackjack</h2>

    <button onclick="myFunction()">Click me</button>

basically the console is telling me that "deck1" is not defined...so something is wrong with the process...

was wondering if anyone can shed any light on this.

Thanks! G

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
gman_donster
  • 331
  • 3
  • 12

3 Answers3

2

Why dont you use a class like you did with card? (P.S. Classes should start with an uppercase letter).

function Card(suit, number) {
  this.suit = suit;
  this.number = number;
}

Card.prototype.toString = function() {
  return this.suit + this.number;
};


function Deck() {
  this.deck = [new Card('S', 'A'), new Card('S', '1')];
}

Deck.prototype.toString = function() {
  return this.deck.join(",");
};


function myFunction() {
  var deck = new Deck;
  console.log('gman:' + deck);
}
<h2>Blackjack</h2>

<button onclick="myFunction()">Click me</button>
Christoph
  • 50,121
  • 21
  • 99
  • 128
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Thanks for everyones help as I am working hard on mastering Javascript. I have made a lot of progress in the last 30 days but obviously still need some learning on scope and objects etc.... – gman_donster Jul 10 '17 at 18:49
1

console is giving correct error as deck1 doesn't exist inside myFunction(), it was declared inside makeDeck1() and its scope is limited to this function, you need to return it and assign to a variable inside myFunction() to be able to use it.

function card(suit1, number1) {
  this.suit1 = suit1;
  this.number1 = number1;
}


function makeDeck1() {
  var deck1 = []; // avoid new Array constructor
  deck1.push(new card('S', 'A'));
  deck1.push(new card('S', '1'));
  return deck1;
}

function myFunction() {
  var deck1 = makeDeck1();
  alert('gman' + JSON.stringify(deck1));
  console.log(deck1);
}
<h2>Blackjack</h2>
<button onclick="myFunction()">Click me</button>
Dij
  • 9,761
  • 4
  • 18
  • 35
  • Thanks for everyones help as I am working hard on mastering Javascript. I have made a lot of progress in the last 30 days but obviously still need some learning on scope and objects etc.... – gman_donster Jul 10 '17 at 18:49
1

Please don't upvote this, since it's a trivial answer, just being helpful

The problem is that your variable only exists in the scope of makeDeck1(). You need to return the value like so:

function card(suit1, number1) // Constructor
{
  this.suit1 = suit1;
  this.number1 = number1;
}


function makeDeck1() {
  var deck1 = new Array()

  deck1[0] = new card('S', 'A');
  deck1[1] = new card('S', '1');

  return deck1; // return it
}


function myFunction() {
  var deck1 = makeDeck1(); // define it here
  alert('gman ' + JSON.stringify(deck1)); // serialize it
}
<h2>Blackjack</h2>

<button onclick="myFunction()">Click me</button>

In addition, it is conventional to define constructors using title-case, so Card instead of card. Good luck.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153