Trying to write a code in javascript for a calendar given the number of days in
ID: 3591781 • Letter: T
Question
Trying to write a code in javascript for a calendar given the number of days in a month and the day on which the month starts. The start day is given by the number 0-6, 0 denoting Sunday, 1 denoting Monday, and so on. I'm attempting to modify this code:
public class Lab3p5 {
public static void main(String [] args) {
System.out.printf(" %4s %4s %4s %4s %4s %4s %4s", "Sun","Mon","Tue","Wed","Thr","Fri","Sat"+" ");
System.out.println("+------+------+------+------+------+------+------+");
printCalender(31, 6);
}
public static void printCalender(int days, int sunday) {
for(int i = 1; i <= 8 - sunday; i++){
System.out.print("| ");
}
for(int i = 1; i < sunday; i++){
System.out.print("| " + i + " ");
}
for(int i = sunday; i <= days; i++){
if((i - sunday) % 7 == 0){
System.out.println("|");
}
if(i >= 10) System.out.print("| " + i + " ");
else System.out.print("| " + i + " ");
}
System.out.println("|");
}
}
...which similarly prints a calendar given the first Sunday of the month, but am strugglng to get any of my code to line up as a calendar when there are less than three weeks (i.e. if I'm doing a 9 day month starting on 2 - Tuesday, it should look like:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|____|____|__1__|__2__|__3__|__4__|__5__|
|_6__|__7_|__8__|__9__|_____|_____|_____|
Explanation / Answer
I can't understand the current code, tried debugging, modifying the logic, but not able to get it.
But i have made a program, which is fairly simple and easy to understand the working.
i have commented in-code for understanding the working.
public class Calendar
{
private static final String DAYS = "Su Mo Tu We Th Fr Sa";
private static final String NEW_LINE = " ";
private static final String NULL_STRING = " ";
private static final String TRIPLE_EMPTY_STRING = " ";
public static void main(final String[] args)
{
final String cString = getMyCalender(4, 6);
System.out.println(cString);
}
private static String getMyCalender(final int startDay, final int endDay)
{
// here we Create a StringBuilder object
final StringBuilder calendar = new StringBuilder();
// now we just print the weekdays, so we Append weekdays
calendar.append(DAYS).append(NEW_LINE);
// This will help us to keep track of days
int day = 1;
for (int i = 1; i <= 5; i++) // Week loop
{
for (int j = 1; j <= 7; j++) // Weekday loop
{
// If we are on the last week of the month,
// and we've reached the endDay that we specified,
// simply return the assembled string
if (i == 5 && j == endDay + 1)
return calendar.toString();
// These are the empty spaces for the beginning of
// the first week
if (i == 1 && j < startDay)
{
// Just append empty space, then CONTINUE
// to next iteration (j++)
calendar.append(TRIPLE_EMPTY_STRING);
continue;
}
// Check if the day is a single or double digit
if (day / 10 >= 1)
calendar.append(day++).append(NULL_STRING);
else
// If this is the first week, then it means that
// we have single-digit days. Apply strings on each
// side of the day for proper spacing of digits
calendar.append(NULL_STRING).append(day++).append(NULL_STRING);
}
calendar.append(NEW_LINE);
}
return calendar.toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.