Skip to content

Instantly share code, notes, and snippets.

@amrza
Last active May 29, 2018 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save amrza/f38644c2794b76c7124bbba69daa72ad to your computer and use it in GitHub Desktop.
Save amrza/f38644c2794b76c7124bbba69daa72ad to your computer and use it in GitHub Desktop.
/**
* Print a table to show all days of a specific month.
*
* Example: Farvardin, 1397
* printMonthMatrix(1397, 1)
*
* J P Ch Se D Y S
* --------------------------------------
* 3 2 1
* 10 9 8 7 6 5 4
* 17 16 15 14 13 12 11
* 24 23 22 21 20 19 18
* 31 30 29 28 27 26 25
*
* @param {number} y Year
* @param {number} m Month
* @returns {boolean} true if everything goes fine.
*/
function printMonthMatrix(y, m) {
// This function can only process the 2d array returned by `monthMatrix()`
var matrix = monthMatrix(y, m);
// Prepeare header.
var line = "J P Ch Se D Y S\n"
+ "--------------------------------------\n";
// Walk in each row, format each element and append them to the `line`.
// after this, the `line` becomes a long string which holds our entire table.
for(var i = 0; i < matrix.length; i++) {
// select this row.
var m = matrix[i];
// each elements of this row represent a day.
for(var j = 0; j < m.length; j++) {
// hide the days from past/future month.
if (m[j] === 0) {
line += " "; // 6 space
continue;
}
// we need extra padding for days between 1 to 9
if (m[j] < 10) {
line += m[j] + " "; // 5 space
continue;
}
// default format for days between 10 to 31
line += m[j] + " "; // 4 space
}
line += "\n";
}
log(line);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment