HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
h1 Linear vs Binary
input(type="text" maxLength="1")#random-letter
.controls
button#random-btn Random
button#search-btn Search
.searches
- var alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
.linear-search
h2 Linear
p.info#linear-info
ul.data
for char in alpha
- var id = char + '-linear'
li(id=id)= char
.binary-search
h2 Binary
p.info#binary-info
ul.data
for char in alpha
- var id = char + '-binary'
li(id=id)= char
// ## FONTS ##
@font-face("OperatorMono-Bold" "https://s3-us-west-2.amazonaws.com/s.cdpn.io/161040/OperatorMono-Bold")
@font-face("OperatorMono-Medium" "https://s3-us-west-2.amazonaws.com/s.cdpn.io/161040/OperatorMono-Medium")
// ## VARS ##
$white: white
$yellow: #FFF32B
$blue: #3BB0FF
$red: #FF014E
$green: #66FF00
$reg-stack: "OperatorMono-Medium", "Lucida Console", Monaco, monospace
$bold-stack: "OperatorMono-Bold", "Lucida Console", Monaco, monospace
// ## ANIMATIONS ##
@keyframes shake
10%, 90%
transform: translate3d(-1px, 0, 0)
20%, 80%
transform: translate3d(2px, 0, 0)
30%, 50%, 70%
transform: translate3d(-4px, 0, 0)
40%, 60%
transform: translate3d(4px, 0, 0)
body
background: $blue
text-align: center
font-family: $reg-stack
color: $white
h1, h2, h3, h4, h5, h6
color: $yellow
font-family: $bold-stack
font-size: 2em
h1
font-size: 3em
input#random-letter
text-align: center
width: 1em
border: none
border-bottom: 5px solid $white
padding-bottom: .2em
text-transform: uppercase
background: $blue
font-size: 3em
font-family: $reg-stack
color: white
&.shake
animation: shake 0.82s cubic-bezier(.36,.07,.19,.97) both
&:focus
outline: none
button
text-align: center
border: none
background: $blue
border: 5px solid $yellow
color: $yellow
font-size: 30px
font-family: $bold-stack
padding: .15em 1.3em
margin: 1em
transition: border-color .2s, color .2s
-webkit-transition: border-color .2s, color .2s
&:hover
color: $white
border-color: $white
cursor: pointer
.searches
.linear-search, .binary-search
display: inline-block
ul.data
padding: 0
margin: 1em
margin-top: 0
li
display: inline-block
border-bottom: 2px solid rgba(0,0,0,0)
&.exempt
color: $red
border-bottom: 2px solid $red
&.found
color: $green
border-bottom: 2px solid $green
// ## Global Variables
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
const ARR_LENGTH = ALPHABET.LENGTH
const SEARCH_INTERVAL_TIME = 800
let linear_interval
let binary_interval
// ## Helper Functions
function getArgumentsAsArray() {
let args = Array.from(arguments)
if (typeof args[0] === "object") args = args[0]
if (args[0] && args[0].length) args = args[0]
return args
}
function sum() {
let args = getArgumentsAsArray(arguments)
return Array.from(args).reduce( (p, c) => p + c )
}
function avg() {
let args = getArgumentsAsArray(arguments)
return sum(args) / args.length
}
function randomIndex(array) {
return Math.ceil(Math.random() * array.length) - 1
}
function randomElement(array) {
return array[randomIndex(array)]
}
// ## Search Functions
function startLinearSearch(value, set) {
let current_value
let current_elm
let current_index = 0
let num_iterations = 0
let info = document.getElementById('linear-info')
linear_interval = setInterval( () => {
current_value = set[current_index]
current_elm = document.getElementById(`${current_value}-linear`)
num_iterations += 1
if (current_index >= set.length) {
clearInterval(linear_interval)
info.innerHTML = `Index: -1, Value: none, Num Iterations: ${num_iterations}`
return
}
if (current_value != value) {
current_elm.className = "exempt"
info.innerHTML = `Index: ${current_index}, Value: ${current_value}, Num Iterations: ${num_iterations}`
}
if (current_value == value) {
clearInterval(linear_interval)
current_elm.className = "found"
info.innerHTML = `Index: ${current_index}, Value: ${current_value}, Num Iterations: ${num_iterations}`
return
}
current_index += 1
}, SEARCH_INTERVAL_TIME)
}
function startBinarySearch(value, set) {
let current_value
let current_elm
let current_index
let num_iterations = 0
let min = 0
let max = set.length - 1
let info = document.getElementById('binary-info')
binary_interval = setInterval( () => {
num_iterations += 1
if (min > max) {
clearInterval(binary_interval)
info.innerHTML = `Index: -1, Value: none, Num Iterations: ${num_iterations}`
return
}
current_index = Math.floor(avg(min, max))
current_value = set[current_index]
current_elm = document.getElementById(`${current_value}-binary`)
if (current_value < value) {
min = current_index + 1
set.slice(0, min).forEach( (n) => {
document.getElementById(`${n}-binary`).className = "exempt"
})
// current_elm.className = "exempt"
info.innerHTML = `Index: ${current_index}, Value: ${current_value}, Num Iterations: ${num_iterations}`
}
if (current_value > value ) {
max = current_index - 1
set.slice(current_index, set.length).forEach( (n) => {
document.getElementById(`${n}-binary`).className = "exempt"
})
info.innerHTML = `Index: ${current_index}, Value: ${current_value}, Num Iterations: ${num_iterations}`
}
if (current_value == value) {
clearInterval(binary_interval)
current_elm.className = "found"
info.innerHTML = `Index: ${current_index}, Value: ${current_value}, Num Iterations: ${num_iterations}`
return
}
}, SEARCH_INTERVAL_TIME)
}
// ## DOM triggered functions
function reset() {
clearInterval(linear_interval)
clearInterval(binary_interval)
let exempt = document.querySelectorAll('.exempt')
Array.prototype.forEach.call(exempt, (el) => { el.classList.remove('exempt') })
let found = document.querySelectorAll('.found')
Array.prototype.forEach.call(found, (el) => { el.classList.remove('found') })
let info = document.querySelectorAll('.info')
Array.prototype.forEach.call(info, (el) => { el.innerHTML = ' ' })
enableButton('random-btn')
enableButton('search-btn')
}
function getRandomLetter() {
reset()
const value = randomElement(ALPHABET)
let input = document.getElementById('random-letter')
input.value = value
input.style.webkitAnimationName = 'shake'
input.className = 'shake'
}
function disableButton(id) {
document.getElementById(id).disabled = true
}
function enableButton(id) {
document.getElementById(id).disabled = false
}
// ## Setup functions
function setupInputHandlers () {
let input = document.getElementById('random-letter')
input.onkeyup = function() {
this.value = this.value.toUpperCase()
reset()
}
input.addEventListener('webkitAnimationEnd', function() {
this.style.webkitAnimationName = ''
this.className = ''
}, false)
let random_btn = document.getElementById('random-btn')
random_btn.onclick = function (e) {
e.preventDefault()
getRandomLetter()
}
let search_btn = document.getElementById('search-btn')
search_btn.onclick = function (e) {
e.preventDefault()
if (input.value.length) {
startLinearSearch(input.value, ALPHABET)
startBinarySearch(input.value, ALPHABET)
}
}
}
function inputFocus() {
let input = document.getElementById('random-letter')
input.focus()
}
// Let's roll
window.onload = () => {
setupInputHandlers()
inputFocus()
}
Also see: Tab Triggers