Pen Settings

HTML

CSS

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

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

Auto Save

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

              
                
<form id="myForm">
	<h1>Paint calculator</h1>
	
	<input-field>
		<label for="w">Width (in feet)</label>
		<input id="w" type="number" min="1" value="20" required>
	</input-field>
	
	<input-field>
		<label for="h">Height (in feet)</label>
		<input id="h" type="number" min="1" value="30" required>
	</input-field>
	
	<output id="message">
		<!-- message here -->
	</output>
	
	<output id="graphic">
		<!-- paint buckets -->
	</output>
	
<!-- 	how many gallons needed at 400 square feet per gallon... -->
	
	<button type='submit'>Send order</button>
</form>



              
            
!

CSS

              
                
form {
	display: grid;
	gap: 20px;
	padding: 30px;
	border: 1px solid black;
}

form button {
	justify-self: start;
}

input-field {
	display: grid;
	gap: 4px;
	max-width: 300px;
}


body {
	padding: 30px;
}


h1 {
	font-weight: 700;
}


.bucket-list {
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
	gap: 20px;
}

.bucket-list li {
	min-width: 40px;
}
              
            
!

JS

              
                console.clear();


function calculatePaintBuckets(totalSquareFeet) {
	const squareFeetPerGallon = 400;
					// round up
	return Math.ceil(totalSquareFeet / squareFeetPerGallon);
}

// console.log( calculatePaintBuckets(600) );

function renderPaintBuckets(numberOfBuckets) {
	// all we know... is that we magically have the number of buckets...
	// could be 1 or 1645 buckets. (it's not an array) (just a number)
	// how do we present the graphics?
	
	var bucketGraphic = `
		<svg viewBox='0 0 4 6'>
			<rect width='4' height='6' fill='blue' />
		</svg>
	`;

	var bucketsGraphicTemplate = "<ul class='bucket-list'>";
	//           setup               condition (if)          changes per iteration
	for (var timesLooped = 0; timesLooped < numberOfBuckets; timesLooped++) {
		// console.log('[BUCKET]', timesLooped);
		// bucketsGraphicTemplate = bucketsGraphicTemplate + "[BUCKET]";
		// 					// short hand for ^		// add another bucket
		bucketsGraphicTemplate += `<li>${bucketGraphic}</li>`;
	}                     // tack this on the end
	bucketsGraphicTemplate += '</ul>';
	return bucketsGraphicTemplate;
}



function area(width, height) {
	return width * height;
}

function message(w, h, a) {
	// return `With a width of ${w} feet and height of ${h} feet, the total area would be ${a} square feet.`; // This one is hard to read... maybe not great UX
	return `
		${a} square feet.
	`;
}

// this way the function stands on its own. It can be tested alone and composed in more places
function handleAreaCalculation() {
	var theWidth = document.querySelector('#w').value;
	var theHeight = document.querySelector('#h').value;
	var areaTotal = area(theWidth, theHeight);
	var messageOutput = document.querySelector('output#message');
	messageOutput.textContent = message(theWidth, theHeight, areaTotal);

	var numberOfBuckets = calculatePaintBuckets(areaTotal);
	
	var graphicOutput = document.querySelector('output#graphic');
	graphicOutput.innerHTML = renderPaintBuckets(numberOfBuckets);
}

var form = document.querySelector('#myForm');

form.addEventListener('input', function() {
	handleAreaCalculation();
	
});

// run as a default / so user see how the form works (in this case)
handleAreaCalculation();


// function clearMessage() {
// 	var outputArea = document.querySelector('output');
// 	outputArea.textContent = "";
// }

// form.addEventListener('input', function() {
// 	clearMessage(); // so the message isn't out of date when the user is choosing numbers
// });

function handleOrder() {
	console.log('Send order message to buy appropriate paint gallons');
}

form.addEventListener('submit', function(event) {
	event.preventDefault();
	handleOrder();
	// clear info?
});

              
            
!
999px

Console