title: Creating and overwriting variables dynamic per user input
body: <p>In the following code, the user is able to create variables utilizing the <code>window</code> object via an input type text element. I've written a function that console logs the name of the variable followed by the value of <code>0</code> in which the variable is initialized. This only occurs when the following key string literal, <code>"-nr "</code> precedes the desired name for the created variable.</p>
  
  <p>The goal of this exercise is to increment any created variable value by <code>1</code> when the variable name is reentered into the input element. My attempt at doing so is by first writing the first function, <code>varCreate</code> to declare and initialize variables to <code>0</code>, <code>push</code> them into an array, and console log the variable name followed by its value. The next function which I have a problem with (<code>varPlus</code>) is meant to add <code>1</code> to the value of each value when a particular name is entered into the input element however, it adds a few more than <code>1</code> even when I utilize a <code>for</code> loop to evaluate if the string literal value of the input element value property is equivalent to each element of the array <code>varArray</code>.</p>
  
  <pre class="lang-js prettyprint-override"><code>const _in = document.getElementById('in');
  
  var varArray = [];
  
  function varCreate(e) {
  
      let _key = e.key;
      if(_key === "Enter") {
  
          if(_in.value.substring(0, 4) == "-nr ") {
  
              window[_in.value.substring(4).replace(/\s/g, "_")] = 0;
              varArray.push(_in.value.substring(4).replace(/\s/g, "_"));
              console.log("var: " + varArray[varArray.length - 1] + "\nvalue: " + window[varArray[varArray.length - 1]]);
  
              _in.value = "";
  
          }
  
      }
  
  }
  
  function varPlus(e1) {
  
      let _key1 = e1.key;
      if(_key1 === "Enter") {
  
          checker = new RegExp(_in.value.replace(/\s/g, "_"), "gi");
          for(var i = 0; i &lt; varArray.length; i++) {
  
              if(checker.test(varArray[i])) {
  
                  window[varArray[i]] += 1;
                  console.log("var: " + varArray[i] + "\nvalue: " + window[varArray[i]]);
  
              }
  
          }
  
          delete window["checker"];
  
      }
  
  }
  
  _in.addEventListener('keydown', varCreate);
  _in.addEventListener('keydown', varPlus);
  </code></pre>
  
  <pre class="lang-html prettyprint-override"><code>&lt;input id='in' type='text' /&gt;
  </code></pre>
  
  <p>The end result when attempting to utilize <code>varPlus</code> is that it'll console log all variable names and values which somehow increment in value when it should only be console logging only the variable name which I'm trying to access via user input followed by its value. I would greatly appreciate it if anyone can shed some light on how I'm encountering these errors. </p>