loops - Get last n quarters in JavaScript -
apparently, this kind of question asked before, it's c# answer can't translate javascript.
here's original code, fails first quarter:
var amount = 6; var = new date(); var thisyear = now.getfullyear(); var quarterstart = math.ceil((now.getmonth() + 1) / 3); //quarterstart return number between 1 , 4 (var = 0; < amount; i++) { //here's line needs changed var thisquarter = quarterstart - (i % 4); //if thisquarter last one, go 1 year if (thisquarter == 4 && > 0) { thisyear--; }; console.log('q' + thisquarter + ' ' + thisyear); }; //console should return following //'q1 2016' //'q4 2015' //'q3 2015' //'q2 2015' //'q1 2015' //'q4 2014' edit: setup loop can changed, long output shown.
i've made changes code, should work now.
var amount = 6; var = new date(); var thisyear = now.getfullyear(); var quarterstart = math.ceil((now.getmonth() + 1) / 3); (var = 0; < amount; i++) { console.log('q' + quarterstart + ' ' + thisyear); quarterstart--; if (quarterstart <= 0) { thisyear--; quarterstart = 4; }; };
Comments
Post a Comment