title: Loop through whether a number exists, if not iterate by one until number does not exist
body: <p>I have the following code which loops through all of the <code>moduleId</code> in a course, for example the output of for one course is</p>
  
  <p><code>0000</code>
  <code>0001</code>
  <code>0002</code>
  <code>0003</code></p>
  
  <pre><code>var self = this;
  $('#addCourseBtn').on('click', function () {
      var templateId = self.courseTemplate.get('ID');
      var num = 0;
      var courseString = ("000" + num).substr(-4, 4);
      console.log(courseString);
  
  
      var i, s, courseModuleList = (self.courseTemplate.get('ModuleIndexList')), len = courseModuleList.length;
  
      for (i = 0; i &lt; len; ++i) {
          if (i in courseModuleList) {
              s = courseModuleList[i];
              if (self.modules.get(s) != undefined) {
                  var moduleId = (self.modules.get(s).get('Id'));
              }
          }
      }
  });
  </code></pre>
  
  <p>I want to be able to check whether a number exists beginning with <code>0000</code>, if that exists iterate by one to <code>0001</code> then if that exists iterate to <code>0002</code> etc. then if it doesn't exist <code>courseString = "0004"</code> Any help would be greatly appreciated.</p>