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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
<link href="https://fonts.googleapis.com/css?family=Fira+Sans&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Fira+Code&display=swap" rel="stylesheet">
    <title>JavaScript Arrays</title>
</head>
  <body>
    <div class="content" style="overflow-x:auto;">
<table data-toggle="table">
        <tr>
          <th>Code</th>
          <th>Original Array</th>
          <th>Processed Array</th>
          <th>Returned Val</th>
          <th>Is Mutated</th>
          <th>Action</th>
          <th>Method</th>
        </tr>
    </table>
  </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    </script>
  </body>
</html>

            
          
!

CSS

            
              body {
  font-family: 'Fira Sans', sans-serif;
}

table {
  border-style: groove;
  border-color: #904baf;
  border-width: 1px;
  margin: 0px auto;
}

.content {
  padding: 20px;
  text-align: center;
}

th {
  background-color: #904baf;
  color: white;
}

tr:hover {background-color: #f5f5f5;}
tr:nth-child(even) {background-color: #d6d2d8;}

th {
  cursor: pointer;
  font-size: 13px;
}

th, td {
  border-bottom: 0px solid #ddd;
  padding: 15px;
  text-align: left;
}

td {
  font-family: 'Fira Code', monospace;
  font-size: 13px;
}

.tooltip {
  position: relative;
  display: inline-block;
  border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 120px;
  background-color: #555;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
  bottom: 125%;
  left: 50%;
  margin-left: -60px;
  opacity: 0;
  transition: opacity 0.3s;
}

.tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #555 transparent transparent transparent;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
  opacity: 1;
  font-size: 12px;
}

.asc:after { content: "\25B2"; }
.desc:after { content: "\25BC"; }
            
          
!

JS

            
              let table = document.getElementsByTagName('table')[0]
let mutated = ''

const testArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
let tempTestArray = [...testArray]

table.insertAdjacentHTML('beforebegin', `<h1>Original testArray: [${testArray}]</h1>`)

let tableBody = document.getElementsByTagName('tbody')[0]
let result = {
  code: '',
  returnedVal: '',
  action: '',
  method: ''
}

updateResult('testArray.push(\'j\')', testArray.push('j'), 'add', 'push()')
updateResult('testArray.unshift(\'z\')', testArray.unshift('z'), 'add', 'unshift()')
updateResult('testArray.concat(\'k\')', testArray.concat('k'), 'add', 'concat()')
updateResult('[\'y\', ...testArray, \'l\']', ['y', ...testArray, 'l'], 'add', '...')
updateResult('testArray.pop()', testArray.pop(), 'remove', 'pop()')
updateResult('testArray.shift()', testArray.shift(), 'remove', 'shift()')
updateResult('testArray.splice(0, 2)', testArray.splice(0, 2), 'remove', 'splice()')
updateResult('testArray.filter(a => a !== \'c\')', testArray.filter(a => a !== 'c'), 'remove', 'filter()')
updateResult('testArray.slice(1, 6)', testArray.slice(1, 6), 'remove', 'slice()')
updateResult('testArray.slice(2)', testArray.slice(2), 'remove', 'slice()')
updateResult('testArray.splice(2, 1, 30, 31)', testArray.splice(2, 1, 30, 31), 'update', 'splice()')
updateResult('testArray.map(x => x === \'d\' ? 29 : x)', testArray.map(x => x === 'd' ? 29 : x), 'update', 'map()')

// check if original array is mutated
function isMutated() {
  if (arraysEqual(tempTestArray, testArray)) {
    mutated = false
    return mutated
  } else {
    tempTestArray = [...testArray]
    mutated = true
    return mutated
  }
}

// check if arrays are equal
function arraysEqual(a, b) {
  return JSON.stringify(a) == JSON.stringify(b)
}

// update result object
function updateResult(code, returnedVal, action, method) {
  result.code = code
  result.returnedVal = returnedVal
  result.action = action
  result.method = method
  insertRecord(result)
}

// insert new records to table
function insertRecord(x) {
  tableBody.insertAdjacentHTML('beforeend', `
    <tr>
      <td>${x.code}</td>
      <td>[${tempTestArray}]</td>
      <td>${isMutated() ? 
        `<span style="color: #09c4bb">[${testArray}]</span>` 
        : `<span>[${testArray}]</span>`}</td>
      <td>${x.returnedVal instanceof Array ? `[${x.returnedVal}]` : x.returnedVal}</td>
      <td>${mutated}</td>
      <td>${x.action}</td>
      <td>${x.method === '...' ? 
        `<div class="tooltip">${x.method} <span class="tooltiptext">Spread Operator</span></div>` 
        : `<div>${x.method}</div>`}</td>
    </tr>
  `)
}

// sort table
$(document).ready(function() {
    $('th').each(function(col) {
        $(this).hover(
        function() { $(this).addClass('focus'); },
        function() { $(this).removeClass('focus'); }
    );
        $(this).click(function() {
            if ($(this).is('.asc')) {
                $(this).removeClass('asc');
                $(this).addClass('desc selected');
                sortOrder = -1;
            }
            else {
                $(this).addClass('asc selected');
                $(this).removeClass('desc');
                sortOrder = 1;
            }
            $(this).siblings().removeClass('asc selected');
            $(this).siblings().removeClass('desc selected');
            var arrData = $('table').find('tbody >tr:has(td)').get();
            arrData.sort(function(a, b) {
                var val1 = $(a).children('td').eq(col).text().toUpperCase();
                var val2 = $(b).children('td').eq(col).text().toUpperCase();
                if($.isNumeric(val1) && $.isNumeric(val2))
                  return sortOrder == 1 ? val1-val2 : val2-val1;
                else
                  return (val1 < val2) ? -sortOrder : (val1 > val2) ? sortOrder : 0;
            });
            $.each(arrData, function(index, row) {
                $('tbody').append(row);
            });
        });
    });
});
            
          
!
999px

Console