javascript - How to compare months in a DRY way? -


i'm building wordpress based application , client have option choose data entered on last year in quarters. love make code smarter i'm not sure how it. data looks in js:

idsh: array[4]     0: "565"     1: "565"     2: "567"     3: "569"     length: 4 datesh: array[4]     0: "12/22/2014 - 00:00:00, 12:00 am"     1: "01/21/2015 - 00:00:00, 12:00 am"     2: "01/28/2015 - 00:00:00, 12:00 am"     3: "02/04/2015 - 00:00:00, 12:00 am"     length: 4 ids: array[5]     0: "1009"     1: "979"     2: "1009"     3: "1011"     4: "1013"     length: 5 dates: array[5]     0: "01/05/2015 - 00:00:00, 12:00 am"     1: "12/22/2014 - 00:00:00, 12:00 am"     2: "02/13/2015 - 00:00:00, 12:00 am"     3: "02/20/2015 - 00:00:00, 12:00 am"     4: "02/27/2015 - 00:00:00, 12:00 am"     length: 5 

    var getquarters = (function() {        'use strict';        var dates = [          "01/05/2015 - 00:00:00, 12:00 am",          "12/22/2014 - 00:00:00, 12:00 am",          "02/13/2015 - 00:00:00, 12:00 am",          "02/20/2015 - 00:00:00, 12:00 am",          "02/27/2015 - 00:00:00, 12:00 am"        ];          function loops(items, fn, onloopcomplete) {          var i;          try {            if (items && items.length) {              = items.length;            } else {              throw new error(items + ' required have length');            }              if (i > -1) {              {                if (items[i] !== undefined) {                  fn(i);                  /* console.log(i + ' current iteration'); */                }              }              while (--i >= 0);            }            if (typeof onloopcomplete === 'function') {              onloopcomplete(items.length);            }          } catch (e) {            throw new error(e);          }        }            function show3() {          loops(dates, function(i) {            var months = dates[i].slice(0, 2),              thismonth = new date().getmonth();              if (months == thismonth ||              months == thismonth - 1 ||              months == thismonth - 2) {              console.log(months);            } else {              console.log(dates[i] + ' not meet criteria.');            }          });        }          function show6() {          loops(dates, function(i) {            var months = dates[i].slice(0, 2),              thismonth = new date().getmonth();              if (months == thismonth ||              months == thismonth - 1 ||              months == thismonth - 2 ||              months == thismonth - 3 ||              months == thismonth - 4 ||              months == thismonth - 5) {              console.log(months);            } else {              console.log(dates[i] + ' not meet criteria.');            }          });        }            return function(quarters) {          switch (quarters) {            case 2:              show6();              break;            case 3:              show9();              break;            case 4:              show12();              break;            default:              show3();              break;          }        };      }());        getquarters();

i loop through dates available see if match criteria, (like last 3 months) my code repetitive right now. how make smarter , dry?

function showlastmonths(numberofmonths){   loops(dates, function(i) {      var months = dates[i].slice(0, 2),      thismonth = new date().getmonth();       monthdifferential = thismonth - months      if (monthdifferential > 0 && monthdifferential <= numberofmonths ) {         console.log(months);         console.log(ids[i]);      } else {         console.log(dates[i] + ' not meet criteria.');      }     }); } 

this easiest refactoring can see there, extract parameter , delete show3 show6 etc functions , call instead.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -