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

              
                
<h1>Array average of max 10 values</h1>
<p><code>arr</code> value</p>
<pre id="data">[]</pre>

<p>
  <label for="data-output"><code>arr.average</code></label>
<input value="0" readonly id="data-output" />
</p>

<button id="button">Add random number</button>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  padding: 2rem;
}

label {
  margin-bottom: 0.25rem;
  display: block;
}

pre {
  padding: 1rem;
  background-color: #eee;
  display: block;
}

h1 {
  margin-top: 0;
}
              
            
!

JS

              
                /* UTILS */

const getAverageScore = (score) =>
  score.length ? score.reduce((acc, point) => acc + point) / score.length : 0;

/* PROXY */

const handleSet = (obj, prop, value) => {
  const isNumber = !isNaN(parseInt(prop));

  if (isNumber) {
    const index = parseInt(prop);
    console.log(index);

    if (index >= 10) {
      throw new RangeError("Array limit reached");
    }

    Reflect.set(obj, prop, value);
  }

  Reflect.set(obj, prop, value);
  return true;
};

const handleGet = (obj, prop) => {
  if (prop === "average") {
    return getAverageScore(obj);
  }

  if (prop in obj) {
    return Reflect.get(obj, prop);
  }
};

const createArrayProxy = (arr) => {
  const proxy = new Proxy(arr, {
    set: handleSet,
    get: handleGet
  });

  return proxy;
};

/* INIT */

let arr = createArrayProxy([]);

/* ACTIONS */

const containerData = document.getElementById("data");
const output = document.getElementById("data-output");
const button = document.getElementById("button");

button.addEventListener("click", addRandomNumber);

function addRandomNumber() {
  const value = Math.floor(Math.random() * 11);

  try {
    arr.push(value);
    logValues();
  } catch (e) {
    console.error(e);
  }
}

/* LOG VALUES */

function logValues() {
  containerData.innerHTML = "";
  containerData.innerHTML = JSON.stringify(arr);

  output.value = arr.average;
}

              
            
!
999px

Console