JSFiddle - React, Tailwind, and code Playground

by Shaun Ellis

JavaScript

// tic-tac-toe.js

function Player(name,label) {
    this.name = name;
    this.label = label;
    this.pieces = 5;
}

Player.prototype = {
    constructor: Player,
    hello: function() { return "Hello, " + this.name + "!"; },
    placePiece: function() { return this.pieces--; },
    piecesLeft: function() { return this.pieces; },
    toString: function() { 
        return this.name + " has " 
                + this.piecesLeft() + " " 
                + this.label + "'s left.";
    }
};

var p1 = new Player("Shaun", "x");
console.log(p1.hello());
console.log(p1.piecesLeft());
p1.placePiece();
console.log(p1.piecesLeft());
console.log(p1.toString());
console.log(p1.constructor.toString());