Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ add another resource


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


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.


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.


                <body class="wrapper l-stack-xl">
	<h1>Guess the number!</h1>
	<p>We have selected a random number between 1 and 100. See if you can guess it in 10 turns or fewer. We'll tell you if your guess was too high or too low.</p>

	<div id="guessesContainer" class="l-stack-sm">
		<ol role="list" id="guessesList" class="layout-numbers"></ol>
		<p id="msgResult" class="msg"></p>
		<p id="msgCorrectNumber" class="msg"></p>

	<form id="guessForm" class="l-stack-xs">
		<label for="guessInput">Enter a number (1 – 100):</label>
		<input id="guessInput" type="number" inputmode="numeric" pattern="[0-9]*" min="1" max="100" required>
		<button id="guessSubmit" type="submit">Submit guess</button>


                /* This pen is using external base CSS stylesheet(s) in the CSS Settings */

* {
	margin: 0;

body {
	padding-block: var(--s-800);
	text-align: center;

form {
	margin-inline: auto;

.layout-numbers {
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	align-items: center;
	gap: var(--s-200);

.guess-number {
	--size: 3rem;
	min-inline-size: var(--size);
	min-block-size: var(--size);
	display: inline-grid;
	place-items: center;
	font-size: 1.25rem;
	line-height: var(--size);
	font-weight: var(--fw-700);
	background-color: var(--c-bg-2);
	border-radius: var(--border-radius);

.guess-number--correct {
	color: white;
	background-color: green;

.guess-number--wrong {
	color: white;
	background-color: crimson;

.msg {
	font-size: var(--s-500);
	font-weight: var(--fw-700);

.msg--correct {
	color: green;

.msg--wrong {
	color: crimson;

.msg:empty {
	display: none;



                const guessesContainer = document.querySelector("#guessesContainer");
const form = document.querySelector("#guessForm");
const input = document.querySelector("#guessInput");
const submit = document.querySelector("#guessSubmit");
const guessesList = document.querySelector("#guessesList");
const guesses = guessesList.children;
const msgResult = document.querySelector("#msgResult");
const msgCorrectNumber = document.querySelector("#msgCorrectNumber");

const minGuess = 1;
const maxGuess = 100;

// Generate a random number between min and max
let randomNumber = getRandomIntInclusive(minGuess, maxGuess);

const maxTurnNumber = 10;
let turnNumber = 0;

// Create a reset button
const resetButton = document.createElement("button");
resetButton.textContent = "Start again";
resetButton.addEventListener("click", resetGame);
const resetButtonContainer = document.createElement("div");

// Generate a box for every guess/turn and add it to the DOM
for (let i = 1; i <= maxTurnNumber; i++) {
	const li = document.createElement("li");

form.addEventListener("submit", function (e) {
	const guessNumber = parseInt(input.value);
	input.value = "";

	const currentGuessItem = guesses[turnNumber - 1];
	currentGuessItem.textContent = guessNumber;

	checkGuessNumber(guessNumber, currentGuessItem);

// Get a random integer between two values (both inclusive)
function getRandomIntInclusive(min, max) {
	min = Math.ceil(min);
	max = Math.floor(max);
	const randomInt = Math.floor(Math.random() * (max - min + 1) + min);
	return randomInt;

function checkGuessNumber(guessNumber, currentGuessItem) {
	if (guessNumber === randomNumber) {
		// Correct
		msgResult.textContent = "Congratulations! You got it right!";
	} else {
		// Wrong

		if (guessNumber < randomNumber) {
			// Low
			msgResult.textContent = "Too low!";
		} else {
			// High
			msgResult.textContent = "Too high!";

		if (turnNumber === maxTurnNumber) {
			msgCorrectNumber.textContent += `GAME OVER! The number was ${randomNumber}.`;

function setGameOver() {
	// Disable the form elements
	setFormDisabledTo(form, true);

	// Add the reset button to the DOM

function resetGame() {
	// Reset the UI
	for (let guess of guesses) {
		guess.textContent = "";
		guess.classList.remove("guess-number--correct", "guess-number--wrong");
	msgResult.textContent = "";
	msgResult.classList.remove("msg--correct", "msg--wrong");
	msgCorrectNumber.textContent = "";
	setFormDisabledTo(form, false);

	// Reset the game logic
	turnNumber = 0;
	randomNumber = getRandomIntInclusive(minGuess, maxGuess);

function setFormDisabledTo(form, disabled) {
	if (disabled) {
		// disabled = true
		form.setAttribute("disabled", "");
		for (const element of form.elements) element.setAttribute("disabled", "");
	} else {
		// disabled = false
		for (const element of form.elements) element.removeAttribute("disabled");