Pen Settings



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


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


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.


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.


                <h2>Magic Square</h2>
(<i>See: <a href="">Hacker Rank</a> - Medium Level - 73.25% Pass Rate</i>)
<i>The problem cannot be solved within the time complexity constraints without knowing the set of magic square constants.</i>
<p>Make sure to see: <a href="">this article</a> which provides a great mathematics background for this problem. After reading <a href="">several other blog posts about people taking a while to solve this (me too!)</a>. I decided to cave and do some research beforehand!</p>
<div id="here"></div>




                const print = str => {
  const el = document.getElementById("here");
  const txt = document.createTextNode(str);
  const br = document.createElement("br");

const formingMagicSquare = (s, expected) => {
    let c = [0,0,0,0,0,0,0,0]

    // magic square 15 - 8 at 3 by 3
    const MAGIC_SQUARES = [

    for (let i = 0; i < s.length; i++) {
        for (let j = 0; j < 3; j++) {
        const a = s[i][j]
        if (a !== MAGIC_SQUARES[0][i][j]) c[0] += Math.abs(MAGIC_SQUARES[0][i][j] - a)
        if (a !== MAGIC_SQUARES[1][i][j]) c[1] += Math.abs(MAGIC_SQUARES[1][i][j] - a)
        if (a !== MAGIC_SQUARES[2][i][j]) c[2] += Math.abs(MAGIC_SQUARES[2][i][j] - a)
        if (a !== MAGIC_SQUARES[3][i][j]) c[3] += Math.abs(MAGIC_SQUARES[3][i][j] - a)
        if (a !== MAGIC_SQUARES[4][i][j]) c[4] += Math.abs(MAGIC_SQUARES[4][i][j] - a)
        if (a !== MAGIC_SQUARES[5][i][j]) c[5] += Math.abs(MAGIC_SQUARES[5][i][j] - a)
        if (a !== MAGIC_SQUARES[6][i][j]) c[6] += Math.abs(MAGIC_SQUARES[6][i][j] - a)
        if (a !== MAGIC_SQUARES[7][i][j]) c[7] += Math.abs(MAGIC_SQUARES[7][i][j] - a)

    let ops = c[0]

    for (let x = 0; x < c.length; x++) {
        if (ops > c[x]) ops = c[x]
    print(`TEST CASE: ${s}`)
    print(`EXPECTED: ${expected} ACTUAL: ${ops}`)

formingMagicSquare([[1,2,2],[2,2,3],[4,3,2]], 26)
formingMagicSquare([[4,9,2],[3,5,7],[8,1,5]], 1)
formingMagicSquare([[4,8,2],[4,5,7],[6,1,6]], 4)