JSFiddle - React, Tailwind, and code Playground

by jonjohnjohnson

JavaScript

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

var deal = function() {
    var suit = Math.floor(Math.random() * 4 + 1);
    var number = Math.floor(Math.random() * 13 + 1);
    Card(suit, number);
};

var Hand = function() {
    this.card1 = deal();
    this.card2 = deal();
    var score1 = this.card1.getValue();
    var score2 = this.card2.getValue();
    this.score = function() {
        return score1 + score2;
    };
};


var myHand = new Hand();
var yourHand = new Hand();

console.log("I scored a "+myHand.score()+" and you scored a "+ yourHand.score());

if(yourHand.score() > myHand.score()) console.log("you win!"); else if(yourHand.score() < myHand.score()) console.log("I win!"); else console.log("We tied!");