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

              
                <header>
  <h1>Why loops, functions, objects, and classes?</h1>
  <hr>
</header>

<main>
  <h2>The life lines:</h2>
  <div class="lifeline-container">
    <div id="ada">
      <h3>Ada</h3>
    </div>

    <div id="grace">
      <h3>Grace</h3>
    </div>

    <div id="jane">
      <h3>Jane</h3>
    </div>

  </div>
  <button id="advance-one-year">+ 1 year</button>
  <button id="advance-ten-years">+ 10 years</button>
  <button id="start-counting">count yerself!</button>
  <button id="stop-counting">stop it!</button>
</main>

<script src="main.js"></script>

              
            
!

CSS

              
                html {
    background-color: #222;
    color: #ccc;
}

body {
    max-width: 1200px;
    margin: 1em auto;
    padding: 2em;
    border: 1px dotted gray;
    border-radius: 10px;
}

header {
    text-align: center;
}

.lifeline-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: 1fr;
}

.lifeline-container div {
    padding: 0 1em;
}
.lifeline-container div:nth-child(1) {
    border-left: 3px solid rgb(199, 21, 190);
}
.lifeline-container div:nth-child(2) {
    border-left: 3px solid rgb(22, 160, 160);
}
.lifeline-container div:nth-child(3) {
    border-left: 3px solid rgb(132, 24, 219);
}

button {
    font-size: 2em;
    margin-top: 1em;
    margin-right: 0.5em;
}
              
            
!

JS

              
                /*
This code was incrementally developed, refactored and improved, starting
out with a long list of spaghetti code. To see the whole progression, which
was intended to elaborate on why using loops, functions, and objects makes
a lot of sense, check out the accompanying recordings on
https://tantemalkah.at/artful-coding/2022wt/#xtra
*/

const btnAdvanceOneYear = document.getElementById('advance-one-year')
const btnAdvanceTenYears = document.getElementById('advance-ten-years')
const btnStart = document.getElementById('start-counting')
const btnStop = document.getElementById('stop-counting')

const ages = {
    ada: -1,
    grace: -1,
    jane: -1,
    lifelines: {
        ada: document.getElementById('ada'),
        grace: document.getElementById('grace'),
        jane: document.getElementById('jane'),
    }
}

function increasePersonAge (person) {
    let message = ''

    ages[person] = ages[person] + 1

    if (person === 'ada') {
        if (ages[person] === 0) { message = 'Hey there, nice to be here!' }
        else if (ages[person] === 8) { message = 'Lord Byron, Ada\'s father dies' }
        else if (ages[person] === 13) { message = 'Designed a flying horse' }
        else if (ages[person] === 14) { message = 'Darn! Got the measles!' }
        else if (ages[person] === 18) { message = 'Ada meets Charles Babbage' }
        else if (ages[person] === 21) { message = 'Ada\'s first child was born' }
        else if (ages[person] === 23) { message = 'Ada become a Countess, of Lovelace' }
        else if (ages[person] === 28) { message = 'Created translations and notes on the analytical engine (contained the firs algorithms/programmes)' }
        else if (ages[person] === 36) { message = 'Ada dies.' }
    } else if (person === 'grace') {
        if (ages[person] === 0) { message = 'Yeah, what adventure awaits?' }
        else if (ages[person] === 19) { message = 'Begins to study maths and physics.'}
        else if (ages[person] === 22) { message = 'Moves to Yale fo a master in maths.'}
        else if (ages[person] === 24) { message = 'Master in Mathematics'}
        else if (ages[person] === 28) { message = 'PhD in Mathematics'}
        else if (ages[person] === 37) { message = 'Joins the Navy.'}
        else if (ages[person] === 38) { message = 'Becomes lieutenant, assigned to the Bureau of Ordnance Project at Harvard, where she learns to program a Mark I computer.'}
        else if (ages[person] === 46) { message = 'Creates the first compiler for computer languages.'}
        else if (ages[person] === 60) { message = 'Retires from the Naval Reserve, but stays in the Navy to tackle standardising communication between different computer languages.'}
        else if (ages[person] === 79) { message = 'Retires from the Navy as rear admiral and oldest serving.'}
        else if (ages[person] === 84) { message = 'Receives the National Medal of Technology (as the first female individual to do so ... in 1991 btw.).'}
        else if (ages[person] === 85) { message = 'Grace dies.'}
    } else if (person === 'jane') {
        if (ages[person] % 6 === 0 && ages[person] % 8 === 0) { message = 'Something <i><b>awesomely weird</b></i> is happening.' }
        else if (ages[person] % 6 === 0) { message = 'Something <i>weird</i> is happening.' }
        else if (ages[person] % 8 === 0) { message = 'Something <b>awesome</b> is happening.' }
    }

    ages.lifelines[person].innerHTML += `<p>${ages[person]}: ${message}</p>`
}


function advanceTenYears () {
    for (let x=0; x < 10; x++) {
        advanceOneYear()
    }
}

function advanceOneYear() {
    if (ages.ada < 36) increasePersonAge('ada')
    if (ages.grace < 85) increasePersonAge('grace')
    if (ages.jane < 100) increasePersonAge('jane')
}

btnAdvanceOneYear.onclick = advanceOneYear
btnAdvanceTenYears.onclick = advanceTenYears
let intervalId
btnStart.onclick = function () {
    intervalId = setInterval(advanceOneYear, 1000)
}
btnStop.onclick = function () {
    clearInterval(intervalId)
}

              
            
!
999px

Console