JohnnyBizzle
JohnnyBizzle

Reputation: 979

Accessing values passed into a prototype function

I am trying to access the string of the object and that passed into the function but don't know how to. split is not allowed here:

var Result = { "win": 1, "loss": 2, "tie": 3 }

function PokerHand(hand) {
}
PokerHand.prototype.compareWith = function(hand){
    // Start your coding here...
    var myHand = this.hand.split(' ');
    var opHand = hand.split(' ');
    if (myHand[0] > opHand[0])
      return Result.win;
    if (this.hand != hand)
      return Result.win;
    return Result.tie;
}
var player = "4S 5H 6H TS AC";
var opponent =  "3S 5H 6H TS AC";
var p = new PokerHand(player);
var o = new PokerHand(opponent);
p.compareWith(o)

Upvotes: 1

Views: 120

Answers (1)

Piyush
Piyush

Reputation: 1162

Try this. It works. The argument in the compareWith() function should be a PokerHand, not a hand. So, I just did player.hand in places where hand was used.

One more thing, the PokerHand constructor needs to have a hand property.

var Result = { "win": 1, "loss": 2, "tie": 3 }

function PokerHand(hand) {
  this.hand = hand;
}
PokerHand.prototype.compareWith = function(pokerHand){
    // Start your coding here...
    var myHand = this.hand.split(' ');
    var opHand = pokerHand.hand.split(' ');
    if (myHand[0] > opHand[0])
      return Result.win;
    if (this.hand != pokerHand.hand)
      return Result.win;
    return Result.tie;
}
var player = "4S 5H 6H TS AC";
var opponent =  "3S 5H 6H TS AC";
var p = new PokerHand(player);
var o = new PokerHand(opponent);
console.log(p.compareWith(o))

Upvotes: 1

Related Questions