Reputation: 31
I am having an issue trying to complete my code. I am trying to get the code to print the arrays in several different rows. It is suppose to look like this:
Ok, here is the updated code. I have been working on it and it is getting better! However, my issue now is trying to figure out what I need to do in order to create blank spaces where the "0" and "1" are currently in the output of the code. I think I just need to insert: calDaysOfWeek.unshift(); or possibly: calDaysOfWeek.shift(); I just cannot figure out where to put it. If I can figure that out, I should be able to make the days in my code less abbreviated (Such as M to Mon or T to Tues).
/*Write a program called calendar.js that displays a calendar month
for May 2012 as the month and year. You must use a loop. The format
of the month should be as shown below: Hint: You can't use console
log and print on the same line. Try storing the entire row as one
concatenated string and then displaying it. You must use loops for
this one. Do not just print the calendar as a series of console.log
statements!*/
calDaysOfWeek = [];
calDaysOfWeek.push("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat");
var s = "MAY 2012\nS M T W T F S\n";
var numDaysOfWeek = calDaysOfWeek.length;
var firstDay = 2;
var numDaysOfMonth = 31;
var numWeeks = numDaysOfMonth/7;
for(var i = 0; i < numWeeks; i++) {
for(var j = 0; j < numDaysOfWeek; j++) {
s += i*numDaysOfWeek + j + ' ';
}
s += "\n";
}
if (firstDay != calDaysOfWeek[2]) {
calDaysOfWeek.push();
}
else if (calDaysOfWeek[30] != 31) {
calDaysOfWeek.pop();
}
else
console.log(s);
Upvotes: 3
Views: 1062
Reputation: 1168
You have an error here:
for(var i = 0; len = calDaysOfWeek.length; i < len; i++)
It should be:
len = calDaysOfWeek.length;
for(var i = 0; i < len; i++) {
}
Now, you need to use this loop to fill your string rather than using arrays. So:
var s = "";
len = calDaysOfWeek.length;
for(var i = 0; i < len; i++) {
}
But your loop is only going from 0 to 6 since you have 7 days of week. So you need another loop for the weeks. Something that you cannot compute easily is the first day of the month which is a Tuesday, so it's indice in calDaysOfWeek is 2 (Sun=0, Mon=1, Tue=2). You also need the number of days in the month, here 31.
var s = '';
var numDaysOfWeek = calDaysOfWeek.length;
var firstDay = 2;
var numDaysOfMonth = 31;
var numWeeks = ??? // compute this
for (var i=0; i<numWeeks; i++) {
for (var j=0; j<numDaysOfWeek; j++) {
s = ??? // you need to do this
}
}
Once the result is correct you can display it by using html code that you will print.
Edit: changed the loops
Edit2: played with it on fiddle
calDaysOfWeek = [];
calDaysOfWeek.push("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat");
var s = "MAY 2012\n";
var numDaysOfWeek = calDaysOfWeek.length;
var firstDay = 2;
var numDaysOfMonth = 31;
var numWeeks = 5; // compute this
for (var i=0; i<numWeeks; i++) {
for (var j=0; j<numDaysOfWeek; j++) {
var n = i * numDaysOfWeek + j + 1 - firstDay ;
if (n < 1 || n > numDaysOfMonth) {
s += ' '; // three spaces
}
else {
if (n < 10)
{
s += ' '; // pad with one space
}
s += n + ' ' ;
}
}
s += "\n";
}
alert(s);
Upvotes: 2