Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p>
   The Tic Tac Toe game below is based on the very first "real" thing React teaches in their tutorials. I have rewritten it without React to show how inefficient their approach to doing things -- with all that nonsensical "virtual DOM" trash -- really is.
</p>
<noscript>
  <p>
    This page requires Javascript to function. Please enable it or revisit the page in a compatible browser.
  </p>
</noscript>
              
            
!

CSS

              
                /* null margins and padding to give good cross-browser baseline */
html,body,div,
form,fieldset,caption {
	margin:0;
	padding:0;
}

body, button, input {
	font:normal 1em/1.5 arial,helvetica,sans-serif;
}

#board {
	float:left;
	font-size:2em;
	width:6em;
	margin:1em;
	border:solid #000;
	border-width:0 0 1px 1px;
}

#board input {
	box-sizing:border-box;
	float:left;
	width:2em;
	line-height:2em;
	text-align:center;
	background:#FFF;
	border:solid #000;
	border-width:1px 1px 0 0;
}

#board + div {
	overflow:hidden;
	padding:1em;
}

ol {
	padding:0.5em 0 0.5em 2em;
}

p {
  margin:1em;
  max-width:48em;
}
              
            
!

JS

              
                {

	let
		player,
		turn,
		turnHistory = [],
		winner;

	const
		lines = [
			[ 0, 1, 2 ],
			[ 3, 4, 5 ],
			[ 6, 7, 8 ],
			[ 0, 3, 6 ],
			[ 1, 4, 7 ],
			[ 2, 5, 8 ],
			[ 0, 4, 8 ],
			[ 2, 4, 6 ]
		],

		make = (tagName, data) => {
			let e = document.createElement(tagName);
			if (data) {
				if (data.attr) Object.assign(e, data.attr);
				if (data.content) {
					if (data.content instanceof Array) {
						for (var row of data.content) e.append(
							row instanceof Array ? make(...row) : row
						);
					} else e.append(data.content);
				}
				if (data.repeat) while (data.repeat[0]--) e.append(
					make(data.repeat[1], data.repeat[2])
				);
				if (data.parent) data.parent.appendChild(e);
			}
			return e;
		}, // make

		purge = (e, amt) => {
			var dir = amt < 0 ? "firstChild" : "lastChild";
			amt = Math.abs(amt);
			while (amt--) e.removeChild(e[dir]);
		}, // purge

		nextPlayer = () => {
			txtPlayer.textContent = player = player === "X" ? "O" : "X";
		}, // nextPlayer

		goToTurn = (e) => {
			restart();
			for (var input of turnHistory) {
				input.value = player;
				if (++turn == e.currentTarget.value) break;
				nextPlayer();
			}
			checkWinner();
		}, // goToTurn

		checkWinner = () => {
			for (let [a, b, c] of lines) {
				if (
					(player == squares[a].value) &&
					(player == squares[b].value) &&
					(player == squares[c].value)
				) {
					txtTurn.textContent = "Winner";
					return txtPlayer.textContent = winner = player;
				}
			}
			if (turn == 9) {
				txtTurn.textContent = "Tie";
				txtPlayer.textContent = "Game Over";
			} else nextPlayer();
		}, // checkWinner

		squareClick = (e) => {
			e = e.currentTarget;
			if (winner || e.value) return;
			e.value = player;
			if (turnHistory.length > turn) {
				purge(turnOL, turnHistory.length - turn);
				turnHistory = turnHistory.slice(0, turn);
			}
			turnHistory.push(e);
			turn++;
			turnButton("Go to move " + turn, goToTurn, turn);
			checkWinner();
		}, // squareClick

		restart = () => {
			for (var square of squares) square.value = "";
			txtPlayer.textContent = player = "X";
			txtTurn.textContent = "Next Player";
			winner = false;
			turn = 0;
		}, // restart

		turnButton = (content, onclick, value) => {
			make("button", {
				content,
				attr : { onclick, value },
				parent : make("li", { parent : turnOL })
			});
		}, // turnButton

		squares = make("fieldset", {
			attr : { id : "board" },
			parent : document.body,
			repeat : [ 9, "input", {
				attr : { onclick : squareClick, type : "button" }
			} ]
		}).elements,
				
		turnOL = make("OL"),
		txtPlayer = new Text(),
		txtTurn = new Text();


	make("div", {
		content : [ txtTurn, " : ", txtPlayer, turnOL ],
		parent : document.body
	});

	turnButton("Go To Game Start", restart);

	restart();

}
              
            
!
999px

Console