123

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

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>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Multiplication Table</title>
    <meta name="" content="...">
   <link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:200,400,700" rel="stylesheet">
    <link rel="stylesheet" href="style.css">
</head>  
<body class="page">
    
      <header class='header'>
          <h1>Multiplicaiton Table</h1>
      </header>
      <div class='score'></div>
      <main class='main'></main>
      <aside class="sidebar"></aside>
      <footer class='footer'>
          <p>footer</p>
      </footer>    
    <script src="script.js"></script>
</body>
</html>

            
          
!
            
              
  body {
      text-align: center;
      font-family: 'Source Code Pro', monospace;
      font-weight: 400;    
      margin: 0;
      }

  /**************  GRID  **************/ 
  .header { grid-area: header; }
  .score { grid-area: score; }
  .main { grid-area: main; }
  .sidebar { grid-area: sidebar; }
  .footer { grid-area: footer; }

  .page {
    display: grid; 
    grid-template-columns: 250px 1fr 100px;
    grid-template-rows: 100px 1fr 50px;
    grid-template-areas: "header header score"
                         "sidebar main main"
                         "footer footer footer";
   
    height: 100vh;
    max-width: 100vw;
  }

  .main {
    /*display: grid;
    grid-template-rows: 1fr;
    grid-template-columns: 1fr;
    max-width: 300px*/
    justify-self: center;
  }

  .score {
     justify-self: center;
     align-self: center;
  }


  /*#main=container {
    display: grid;
      grid-template-columns: 100px 1fr 100px;
  }

  #table {
    display: grid;
    grid-template-columns: 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px;
  /*  grid-template-rows: 50px;
  /*  grid-gap: 4px;
  }*/

  /**************  FLEX  **************/

  .equationParagraph {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
  }

  .score {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .sidebar {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  /**************  CONTENT  **************/

  header {
      text-align: center; 
      border: dashed 1px black;    
  }

  footer {
      border: dashed 1px black;
  }
  footer p {
      margin: 0;
      padding: 5px;
  }

  .score {
    height: 80px;
    width: 80px;
    border: solid 2px black;
    font-size: 40px;
  }

  .sidebar {
    border: dashed 1px black;
  }

  .equationParagraph {
    margin-top: 10px;
    font-size: 40px;
  }

  .equationElements {
    margin: 10px;
  }

   .equationParagraph input {
    margin: 10px;
    width: 40px;
    height: 40px;
    border: solid 1px black;
    text-align: center;
    font-family: 'Source Code Pro', monospace;
  }
            
          
!
            
              
const naturalNumbers = {
  getRandomOfRange: function(min,max) { 
    return Math.floor(Math.random() * (max + 1 - min) ) + min 
  },
  getAll: function() {
    const array = [];
    for (let i = 1; i < 10 ; i++) { array[i-1] = i };
    return array;
  },
  shuffle: function(array) {   
   for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
  },
  getAllShuffled: function() {
    return this.shuffle( this.getAll() )
  },
  allPaired: function() {
    const arrayOfPairs = []; 
    const arrayOfEquations = [];
    for (let i = 1; i < 10; i++ ) {       
      for (let j = 1; j < 10; j++ ) {
          let pair = [];
          pair.push(i)
          pair.push(j)
          arrayOfPairs.push(pair)
        }
      }
    for (key in arrayOfPairs) {
      let obj = {};
      obj.valueX = arrayOfPairs[key][0]
      obj.valueY = arrayOfPairs[key][1]
      obj.valueZ =  obj.valueX * obj.valueY
      arrayOfEquations.push( obj );
    }
    return arrayOfEquations;
  },
  allPairsShuffled: function() {
    const newArray = this.shuffle( this.allPaired() )
    for (let i = 0; i < newArray.length; i++) {
      newArray[i].index = i;
    };
    return newArray;
  },
 
};

const ShuffledEquations = function() {
  const allShuffled = naturalNumbers.allPairsShuffled();
  let globalIndex = 0; 
  let tempIndex = 0;

  this.getAll = function() {return allShuffled };
  this.getOne = function(n) {return allShuffled [n] };

  this.setGlobalCurrent = function(n) { globalIndex = n }
  this.setNextAsGlobalCurrent = function() { globalIndex ++; }
  this.getGlobalCurrent = function() {return allShuffled [ globalIndex ] };

  this.setTempCurrent = function(n) { tempIndex = n }
  this.setNextAsTempCurrent = function() { tempIndex ++ }
  this.getTempCurrent = function() {return allShuffled [ tempIndex ] };
};

const Counter = function() {
  let globalCounter = 0;
  let localCounter = 0;
  this.setGlobal = function(n) { globalCounter = n; return globalCounter };
  this.getGlobal = function(n) { return globalCounter };
  this.incrementGlobal = function(n) { globalCounter ++ ;  return globalCounter };

  this.setLocal = function(n) { localCounter = n ;  return localCounter };
  this.getLocal = function(n) { return localCounter };
  this.incrementLocal = function(n) { localCounter ++ ; return localCounter };
}

////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// *** M O D E L *** ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////

const model = {
  init: function() {},
  equations: new ShuffledEquations(),

  userData: {
    currentSession: {
      correctAnswers: [],
      incorrectAnswers: [],
      getScore: function() {
        return this.correctAnswers.length * 2;
      }
    },
  },

};
////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// *** C O N T R O L L E R *** ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////

const controller = {
  init: function() {
    model.init();
    view.init();
  },
  getScore: function() {return model.userData.currentSession.getScore() },
  setScore: function() {

  },
  
  number: function() {
    return {
      x: model.equations.getGlobalCurrent().valueX,
      y: model.equations.getGlobalCurrent().valueY,
      z: model.equations.getGlobalCurrent().valueZ,
      checkAnswer: function(inputValue) {
        const answer = parseInt(inputValue);
        const correctAnswer = this[controller.correctAnswer.is];
        // console.log(correctAnswer)
        if (answer === correctAnswer) {
          model.userData.currentSession.correctAnswers.push(this);
        } else 
          model.userData.currentSession.incorrectAnswers.push(this);
      },
      
     };
  },

  equationType: function() {
    let number = this.number;
    const inputField = this.createEl("input", "equationElements");
    const givenNumber = function(n) { createEl ("div", "equationElements", n ) }
    const xSign = function(n) { createEl ("div", "equationElements", "x" ) }
    const equationArray = [
    this.createEl ("div", "equationElements", this.number().x ),
    this.createEl ("div", "equationElements", "x"),
    this.createEl ("div", "equationElements", this.number().y ),
    this.createEl ("div", "equationElements", "=" ),
    this.createEl ("input", "input"),
    ]
    
    return {
      leftHand: function() {
        return {
          blankFirst: function() {
            equationArray[0] = inputField;
            equationArray[4] = givenNumber( number().z );
            controller.correctAnswer = Object.create( { is : "x" } );
            return equationArray;
          },
          blankSecond: function() {
            equationArray[2] = inputField;
            equationArray[4] = givenNumber( number().z );
            controller.correctAnswer = Object.create( { is : "y" });
            return equationArray;
          },
          blankThird: function() {
            controller.correctAnswer = Object.create( { is : "z" });
            return equationArray;
          },
        }
      },
      rightHand: function(n) { // z = y * x
        equationArray[0] = givenNumber( number().z );
        equationArray[1] = createEl("div", "equationElements", "=" );
        equationArray[3] = createEl("div", "equationElements", "x" );
        equationArray[4] = givenNumber( number().x );
        if (n === "blankFirst") {
            equationArray[0] = inputField;
            (function() { controller.correctAnswer = Object.create( { is : "z" } )   })()
        };
        if (n === "blankSecond") { 
            equationArray[2] = inputField;            
            (function() { controller.correctAnswer = Object.create( { is : "y" } )   })()
        } else
            equationArray[4] = inputField;
            (function() { controller.correctAnswer = Object.create( { is : "x" } )   })()
        return equationArray;
      },
    }
  },
  wrapInParagraph: function(equationType) {
      const equationParagraph = controller.createEl("DIV", "equationParagraph");
      for (let i = 0; i < equationType.length; i ++) {           
        equationParagraph.appendChild( equationType[i] );
      };
      return equationParagraph;
    },

  
  equationsToShow: function(t) {
    const listOfEquations = []
    for (let i = 0; i < t; i++) {
      listOfEquations[i] = this.wrapInParagraph( this.equationType().leftHand().blankThird() );
      model.equations.setNextAsGlobalCurrent();
    }
    model.equations.setGlobalCurrent(0);
    return listOfEquations;
  },

  createEl: function(tag, className, textContent) {
    const newElement = document.createElement(tag);
    newElement.className = className;
    newElement.textContent = textContent;
    return newElement;
  },
}

////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// *** V I E W *** //////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////

const view = {
  main: document.querySelector('.main'),
  init: function () {
    this.render();
  },
  render: function() {
    this.equations.render();
    this.score.render();   
  },
  score: {
    scoreParent: document.querySelector('.score'),
    render: function() {
      let score = controller.getScore()
      this.clear();
      this.scoreParent.appendChild( controller.createEl("DIV", "scoreDiv", score) )
    },
    clear: function() {
      if (this.scoreParent.children.length > 0) {
        this.scoreParent.removeChild( this.scoreParent.firstElementChild );
      };
    },    
  },

  equations: {
    inputFields: document.getElementsByTagName('INPUT'),
    list: controller.equationsToShow(22),
    counter: new Counter(),
    maxAllowed: 7,
    render: function() {
      console.log( this.counter.getGlobal() )
      let batch = view.equations.counter.getGlobal() * this.maxAllowed;
      if (this.counter.getLocal() === this.maxAllowed - 1) {
        this.counter.incrementGlobal();
      };
      if (this.counter.getLocal() === this.maxAllowed) {
        this.clearAll();
        this.counter.setLocal(0);
      };
      if (this.counter.getLocal() >= 0 && this.counter.getLocal() < this.maxAllowed ) {
        this.clearAll();
        this.counter.incrementLocal();
        for (let i = batch ; i < this.counter.getLocal() + batch ; i ++ ) {
            view.main.appendChild( this.list[ i ] );          
        };
      };
      this.inputFieldEvents( this.counter.getLocal() );      
    },
    clearAll: function() {
      const allElements = view.main.children;     
      if (allElements.length > 0) {
        for (let i = allElements.length-1; i >= 0; i--) {
          view.main.removeChild( allElements[i] )
        };        
      };
    },
    update: function() {
      model.equations.setNextAsGlobalCurrent()
      view.render();
    },
    
    inputFieldEvents: function(i) {
      let inputFields = this.inputFields;
      inputFields [ this.counter.getLocal() - 1 ].focus();
      inputFields[ i-1 ].addEventListener("keypress", function onEnter (e) {
        let key = e.keyCode;
        if (key === 13) {
          controller.number().checkAnswer(inputFields[ i-1 ].value);
          view.equations.update();
          if ( inputFields.length > 1) {
            inputFields[ i-1 ].removeEventListener("keypress", onEnter);
          };
        };       
      });      
    },
  },
};

controller.init();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console