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.
const musicData = [
{
name: 'Radiohead',
albums: [
{
title: 'The King of Limbs',
songs: [
{ title: 'Bloom', lengthInMins: 5 },
{ title: 'Morning Mr Magpie', lengthInMins: 4.5 },
{ title: 'Little by Little', lengthInMins: 4.5 },
{ title: 'Feral', lengthInMins: 3 },
{ title: 'Lotus Flower', lengthInMins: 5 },
{ title: 'Codex', lengthInMins: 4.8 },
{ title: 'Give Up the Ghost', lengthInMins: 4.8 },
{ title: 'Separator', lengthInMins: 5.4 },
],
description:
"The King of Limbs is the eighth studio album by English rock band Radiohead, produced by Nigel Godrich. It was self-released on 18 February 2011 as a download in MP3 and WAV formats, followed by physical CD and 12 inch vinyl releases on 28 March, a wider digital release via AWAL, and a special newspaper edition on 9 May 2011. The physical editions were released through the band's Ticker Tape imprint on XL in the United Kingdom, TBD in the United States, and Hostess Entertainment in Japan.",
},
{
title: 'OK Computer',
songs: [
{ title: 'Airbag', lengthInMins: 4.7 },
{ title: 'Paranoid Android', lengthInMins: 6.4 },
{ title: 'Subterranean Homesick Alien', lengthInMins: 6.5 },
{ title: 'Exit Music (For a Film)', lengthInMins: 6.5 },
{ title: 'Let Down', lengthInMins: 5 },
{ title: 'Karma Police', lengthInMins: 4.4 },
{ title: 'Fitter Happier', lengthInMins: 2 },
{ title: 'Electioneering', lengthInMins: 3.8 },
{ title: 'Climbing Up the Walls', lengthInMins: 4.7 },
{ title: 'No Surprises', lengthInMins: 3.8 },
{ title: 'Lucky', lengthInMins: 4.4 },
{ title: 'The Tourist', lengthInMins: 5.4 },
],
description:
"OK Computer is the third studio album by the English alternative rock band Radiohead, released on 16 June 1997 on Parlophone in the United Kingdom and 1 July 1997 by Capitol Records in the United States. It marks a deliberate attempt by the band to move away from the introspective guitar-oriented sound of their previous album The Bends. Its layered sound and wide range of influences set it apart from many of the Britpop and alternative rock bands popular at the time and laid the groundwork for Radiohead's later, more experimental work.",
},
],
},
{
name: 'Portishead',
albums: [
{
title: 'Dummy',
songs: [
{ title: 'Mysterons', lengthInMins: 5 },
{ title: 'Sour Times', lengthInMins: 4 },
{ title: 'Strangers', lengthInMins: 4 },
{ title: 'It Could Be Sweet', lengthInMins: 4 },
{ title: 'Wandering Star', lengthInMins: 4.8 },
{ title: "It's a Fire", lengthInMins: 3.8 },
{ title: 'Numb', lengthInMins: 3.8 },
{ title: 'Roads', lengthInMins: 5 },
{ title: 'Pedestal', lengthInMins: 3.6 },
{ title: 'Biscuit', lengthInMins: 5 },
{ title: 'Glory Box', lengthInMins: 5 },
],
description:
'Dummy is the debut album of the Bristol-based group Portishead. Released in August 22, 1994 on Go! Discs, the album earned critical acclaim, winning the 1995 Mercury Music Prize. It is often credited with popularizing the trip-hop genre and is frequently cited in lists of the best albums of the 1990s. Although it achieved modest chart success overseas, it peaked at #2 on the UK Album Chart and saw two of its three singles reach #13. The album was certified gold in 1997 and has sold two million copies in Europe. As of September 2011, the album was certified double-platinum in the United Kingdom and has sold as of September 2011 825,000 copies.',
},
{
title: 'Third',
songs: [
{ title: 'Silence', lengthInMins: 5 },
{ title: 'Hunter', lengthInMins: 4 },
{ title: 'Nylon Smile', lengthInMins: 3 },
{ title: 'The Rip', lengthInMins: 4.5 },
{ title: 'Plastic', lengthInMins: 3.5 },
{ title: 'We Carry On', lengthInMins: 6.5 },
{ title: 'Deep Water', lengthInMins: 1.5 },
{ title: 'Machine Gun', lengthInMins: 4.7 },
{ title: 'Small', lengthInMins: 6.7 },
{ title: 'Magic Doors', lengthInMins: 3.5 },
{ title: 'Threads', lengthInMins: 5.7 },
],
description:
"Third is the third studio album by English musical group Portishead, released on 27 April 2008, on Island Records in the United Kingdom, two days after on Mercury Records in the United States, and on 30 April 2008 on Universal Music Japan in Japan. It is their first release in 10 years, and their first studio album in eleven years. Third entered the UK Album Chart at #2, and became the band's first-ever American Top 10 album on the Billboard 200, reaching #7 in its entry week.",
},
],
},
]
/*
Structure of data:
[
{
name,
albums: [
{
title,
songs: [
{
title,
lengthInMins
}
]
}
]
}
]
*/
// Practicing with find
// Let's start by focusing on just one artist: Radiohead.
// Find the album called "King of Limbs."
// 1. Get the list of Radiohead Albums. You can access musicData[0] for this
const radioheadAlbums = musicData[0].albums
// 2. Find by the album's name.
const matchingAlbum = radioheadAlbums.find(album => album.title === 'King of Limbs')
console.log(matchingAlbum)
// Practicing with filter
// Get every song on the King of Limbs with a title longer than one word.
// Note: You can turn the title into an array of its words using the .split() function! Look it up to see how it works.
// 1. Get the list of King of Limbs songs. You can use your result from the previous question for this.
const kingOfLimbsSongs = []
// 2. Find the list of matching songs
const longerSongNames = kingOfLimbsSongs.filter /* your code here */
// Practicing with map
// Let's figure out how may seconds long each song is.
// To make this easier, let's just focus on songs for Portishead's Dummy.
// 1. Get the list of songs from the album. You use array indices to access this.
let dummySongs = []
// 2. Map to the song's length in seconds, using basic multiplication. (remember! These functions don't mutate the actual array. You have to reassign, to a "songsInSeconds" variable in this case.)
const songsInSeconds = dummySongs.map /* your code here */
// Practicing with reduce
// Now, let's see how long OK Computer is.
// 1. Get the list of songs for OK Computer.
const okComputerSongs = []
// 2. Calculate the length of the album using reduce!
const albumLength = okComputerSongs.reduce /* your code here */
// Getting fancier
// Get every third song on the "Third" album.
// Note: There are more parameters to the filter callback function than just the current item in the list!
const everyThirdSong = []
// Find the albums with descriptions longer than 90 words.
// Note: You'll have to loop over the list of albums for both artists! You can use a loop for this, or the fancy forEach() function.
const longerThan90 = []
// Get the list of songs across all albums
// Note: This requires some nested looping. I would also recommend something that rhymes with "shmaccumulator" for the outermost callback!
const allSongs = []
Also see: Tab Triggers