Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

HTML

              
                <main>
  <div class="pinkOval" id="1"></div>
  <div class="greenSquare" id="2"></div>
  <div class="pinkCircle" id="3"></div>
  <div class="yellowSquare" id="4"></div>
  <div class="blueCircle" id="5"></div>
  <div class="blueOval" id="6"></div>
  <div class="pinkOval" id="7"></div>
  <div class="greenSquare" id="8"></div>
  <div class="yellowSquare" id="9"></div>
  <div class="blueRectangle" id="10"></div>
  <div class="pinkOval" id="11"></div>
  <div class="blueRectangle" id="12"></div>
  <div class="yellowCircle" id="13"></div>
  <div class="yellowOval" id="14"></div>
  <div class="pinkSquare" id="15"></div>
  <div class="blueSquare" id="16"></div>
  <div class="greenOval" id="17"></div>
  <div class="pinkSquare" id="18"></div>
  <div class="yellowRectangle" id="19"></div>
  <div class="blueRectangle" id="20"></div>
  <div class="greenCircle" id="21"></div>
  <div class="blueRectangle" id="22"></div>
  <div class="blueSquare" id="23"></div>
  <div class="blueCircle" id="24"></div>
  <div class="pinkOval" id="25"></div>
  <div class="blueOval" id="26"></div>
  <div class="blueSquare" id="27"></div>
  <div class="yellowRectangle" id="28"></div>
  <div class="greenCircle" id="29"></div>
  <div class="yellowRectangle" id="30"></div>
  <div class="yellowCircle" id="31"></div>
  <div class="greenOval" id="32"></div>
  <div class="pinkRectangle" id="33"></div>
  <div class="greenSquare" id="34"></div>
  <div class="blueSquare" id="35"></div>
  <div class="blueOval" id="36"></div>
  <div class="greenOval" id="37"></div>
  <div class="pinkRectangle" id="38"></div>
  <div class="greenCircle" id="39"></div>
  <div class="yellowSquare" id="40"></div>

  <button type="button" class="triggerSort" id="sortByColor">Sort by colour</button>
  <button type="button" class="triggerSort" id="sortByShape">Sort by shape</button>
  </main>
              
            
!

CSS

              
                :root {
  --black: #101010;
  --purple: #34176e;
  --pink: #fc7bfa;
  --green: #7bfcbe;
  --yellow: #fafc7b;
  --blue: #7bfafc;
}

body {
  background-color: var(--black);
}

main {
  display: grid;
  height: 600px;
  width: 100%;
  min-width: 1224px;
  grid-template-rows: 1fr 1fr 1fr 1fr 1fr;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
}

.triggerSort {
  height: 60px;
  width: 102px;
  background-color: #dddddd;
  text-transform: uppercase;
  font-family: sans-serif;
  font-weight: bold;
  grid-row: 5;
  border-style: solid;
  border-width: 5px;
  border-radius: 5px;
}

#sortByColor {
  background-color: var(--purple);
  color: var(--green);
  grid-column: 5;
  border-color: var(--green);
}

#sortByShape {
  background-color: var(--purple);
  color: var(--yellow);
  grid-column: 6;
  border-color: var(--yellow);
}

.blueSquare {
  height: 100px;
  width: 100px;
  background-color: var(--blue);
  grid-row: auto;
  grid-column: auto;
}

.yellowSquare {
  height: 100px;
  width: 100px;
  background-color: var(--yellow);
  grid-row: auto;
  grid-column: auto;
}

.pinkSquare {
  height: 100px;
  width: 100px;
  background-color: var(--pink);
  grid-row: auto;
  grid-column: auto;
}

.greenSquare {
  height: 100px;
  width: 100px;
  background-color: var(--green);
  grid-row: auto;
  grid-column: auto;
}

.blueCircle {
  height: 100px;
  width: 100px;
  background-color: var(--blue);
  grid-row: auto;
  grid-column: auto;
  border-radius: 50px;
}

.yellowCircle {
  height: 100px;
  width: 100px;
  background-color: var(--yellow);
  grid-row: auto;
  grid-column: auto;
  border-radius: 50px;
}

.pinkCircle {
  height: 100px;
  width: 100px;
  background-color: var(--pink);
  grid-row: auto;
  grid-column: auto;
  border-radius: 50px;
}

.greenCircle {
  height: 100px;
  width: 100px;
  background-color: var(--green);
  grid-row: auto;
  grid-column: auto;
  border-radius: 50px;
}

.blueRectangle {
  height: 50px;
  width: 100px;
  background-color: var(--blue);
  grid-row: auto;
  grid-column: auto;
}

.yellowRectangle {
  height: 50px;
  width: 100px;
  background-color: var(--yellow);
  grid-row: auto;
  grid-column: auto;
}

.pinkRectangle {
  height: 50px;
  width: 100px;
  background-color: var(--pink);
  grid-row: auto;
  grid-column: auto;
}

.greenRectangle {
  height: 50px;
  width: 100px;
  background-color: var(--green);
  grid-row: auto;
  grid-column: auto;
}

.blueOval {
  height: 50px;
  width: 100px;
  background-color: var(--blue);
  grid-row: auto;
  grid-column: auto;
  border-radius: 50%;
}

.yellowOval {
  height: 50px;
  width: 100px;
  background-color: var(--yellow);
  grid-row: auto;
  grid-column: auto;
  border-radius: 50%;
}

.pinkOval {
  height: 50px;
  width: 100px;
  background-color: var(--pink);
  grid-row: auto;
  grid-column: auto;
  border-radius: 50%;
}

.greenOval {
  height: 50px;
  width: 100px;
  background-color: var(--green);
  grid-row: auto;
  grid-column: auto;
  border-radius: 50%;
}

              
            
!

JS

              
                let blueArray = [];
let yellowArray = [];
let pinkArray = [];
let greenArray = [];

let squareArray = [];
let circleArray = [];
let rectangleArray = [];
let ovalArray = [];


function checkColorAndAddToArray(index, classOfElement) { //adds the element to the array corresponding to the colour of the element.
  classOfElement = document.getElementById(index).className;
  if (classOfElement == "blueSquare" || classOfElement == "blueCircle"
    || classOfElement == "blueOval" || classOfElement == "blueRectangle") {
      blueArray.push(index);
    }
  else if (classOfElement == "yellowSquare" || classOfElement == "yellowCircle"
    || classOfElement == "yellowOval" || classOfElement == "yellowRectangle") {
      yellowArray.push(index);
  }
  else if (classOfElement == "pinkSquare" || classOfElement == "pinkCircle"
    || classOfElement == "pinkOval" || classOfElement == "pinkRectangle") {
      pinkArray.push(index);
  }
  else if (classOfElement == "greenSquare" || classOfElement == "greenCircle"
    || classOfElement == "greenOval" || classOfElement == "greenRectangle") {
      greenArray.push(index);
  }
}

function iterateBlueArray() { //Moves all the blue divs to row one.
  blueArray.forEach(element => document.getElementById(element).style.gridRow = "1")
}

function iterateYellowArray() { //Moves all the yellow divs to row two.
  yellowArray.forEach(element => document.getElementById(element).style.gridRow = "2")
}

function iteratePinkArray() { //Moves all the pink divs to row three.
  pinkArray.forEach(element => document.getElementById(element).style.gridRow = "3")
}

function iterateGreenArray() { //Moves all the green divs to row four.
  greenArray.forEach(element => document.getElementById(element).style.gridRow = "4")
}

function sortByColor() { //Initiates the moving of divs when they are to be sorted by colour.
  iterateBlueArray();
  iterateYellowArray();
  iteratePinkArray();
  iterateGreenArray();
}

function checkShapeAndAddToArray(index, classOfElement) { //adds the element to the array corresponding to the shape of the element.
  if (classOfElement == "blueSquare" || classOfElement == "yellowSquare"
    || classOfElement == "pinkSquare" || classOfElement == "greenSquare") {
      squareArray.push(index);
  }
  else if (classOfElement == "blueCircle" || classOfElement == "yellowCircle"
    || classOfElement == "pinkCircle" || classOfElement == "greenCircle") {
      circleArray.push(index);
  }
  else if (classOfElement == "blueRectangle" || classOfElement == "yellowRectangle"
    || classOfElement == "pinkRectangle" || classOfElement == "greenRectangle") {
      rectangleArray.push(index);
  }
  else if (classOfElement == "blueOval" || classOfElement == "yellowOval"
    || classOfElement == "pinkOval" || classOfElement == "greenOval") {
      ovalArray.push(index);
  }
}

function iterateSquareArray() { //Moves all the square divs to row one.
  squareArray.forEach(element => document.getElementById(element).style.gridRow = "1")
}

function iterateCircleArray() { //Moves all the circular divs to row two.
  circleArray.forEach(element => document.getElementById(element).style.gridRow = "2")
}

function iterateRectangleArray() { //Moves all the rectangular divs to row three.
  rectangleArray.forEach(element => document.getElementById(element).style.gridRow = "3")
}

function iterateOvalArray() { //Moves all the oval divs to row four.
  ovalArray.forEach(element => document.getElementById(element).style.gridRow = "4")
}

function sortByShape() { //Initiates the moving of divs when they are to be sorted by shape.
  iterateSquareArray();
  iterateCircleArray();
  iterateRectangleArray();
  iterateOvalArray();
}

function colorSort() {
  for (let i = 1; i <= 40; i++) { //assign each div to array by color
    const classOfElement = document.getElementById(i).className
    checkColorAndAddToArray(i, classOfElement)
  }
  sortByColor()
}

function shapeSort() {
  for (let i = 1; i <= 40; i++) { //assign each div to array by shape
    const classOfElement = document.getElementById(i).className
    checkShapeAndAddToArray(i, classOfElement)
  }
  sortByShape()
}

document.getElementById("sortByColor").addEventListener("click", colorSort, false);
document.getElementById("sortByShape").addEventListener("click", shapeSort, false);

              
            
!
999px

Console