View Single Post
Old 06-08-2012, 06:51 PM   PM User | #1
IMayNeed
New to the CF scene

 
Join Date: Jun 2012
Posts: 7
Thanks: 5
Thanked 0 Times in 0 Posts
IMayNeed is an unknown quantity at this point
Mistake in Calendar and Trying to Find a Way to Not to Choose the Same Random Number

Hello to the Coding Forums!

This is my first post!

I am trying to (re)write a calendar which starts on Monday, but days is one day off. For example, September 1st 2012 shows Sunday instead of Saturday and June 8th 2012 shows Saturday instead of Friday. I couldn't find the mistake myself.

Code:
 var monthName = new Array("JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER");

function calendar() {
var calDate = new Date("September 01, 2012");
document.write("<table id='calendar_table'>");
writeCalTitle(calDate);
writeDayTitle();
writeCalDays(calDate);
document.write("</table>");
}

function writeCalTitle(calendarDay) {

var thisMonth = calendarDay.getMonth();
var thisYear = calendarDay.getFullYear();

document.write("<tr>");
document.write("<th colspan='7'>");
document.write("<span id='month'>"+monthName[thisMonth]+" </span><span id='year'>"+thisYear+"</span>");
document.write("</th>");
document.write("</tr>");
}

function writeDayTitle() {
var dayName = new Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");

document.write("<tr>");
for (var i=0;i<=4;i++){
document.write("<th class='calendar_weekdays'>"+dayName[i]+"</th>");
}

for (var i=5;i<=6;i++){
document.write("<th class='calendar_weekends'>"+dayName[i]+"</th>");
}
document.write("</tr>");
}

function daysInMonth (calendarDay) {
var dayCount = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
var thisYear = calendarDay.getFullYear();
var thisMonth = calendarDay.getMonth();
if(thisYear%4==0){
if((thisYear%100!=0)||(thisYear%400==0)){
dayCount[1]=29;
}
}
return dayCount[thisMonth];
}

function writeCalDays(calendarDay){
var currentDay = calendarDay.getDate();
var totalDays = daysInMonth(calendarDay);
calendarDay.setDate(1);
var weekDay=calendarDay.getDay();
document.write("<tr>");
for (var i=0;i<weekDay;i++){
document.write("<td></td>");
}
var dayCount=1;
while(dayCount<=totalDays){
if (weekDay==0){
document.write("<tr>");
}
if(dayCount==currentDay){
document.write("<td class='calendar_dates' id='calendar_today'>"+dayCount+"</td>");
} else {

document.write("<td class='calendar_dates'>"+dayCount+"</td>");
}
if (weekDay==6){
document.write("</tr>");
}
dayCount++;
calendarDay.setDate(dayCount);
weekDay=calendarDay.getDay();
}
document.write("</tr>");
}
My second question is I am trying to use 11 values from an array of 11 items, but I do want items to be changed at every load, but never be the same at a time (var1!=var2!=!var3...). I am new to javascript and couldn't find the right function to do that. I am just posting the related codes.

Code:
var colors = new Array("aqua","blue","fuchsia","lime","magenta","navy","purple","red","white","yellow","black");
var color1 = randomInteger(10);
var color2 = randomInteger(10);
var color3 = randomInteger(10);
var color4 = randomInteger(10);
var color5 = randomInteger(9);
var color6 = randomInteger(10);
var color7 = randomInteger(10);
var color8 = randomInteger(10);
var color9 = randomInteger(10);
var color10 = randomInteger(10);
var color11 = randomInteger(10);

function randomInteger(size) {
return Math.floor((size+1)*Math.random());
}

function changeColor() {

document.write("h1 {color:"+colors[color1]+"}");

document.write("h2 {color:"+colors[color2]+"}");

document.write("#month {color:"+colors[color3]+"}");

document.write("#year {color:"+colors[color4]+"}");

document.write(".calendar_dates {color:"+colors[color5]+"}");

document.write(".calendar_weekdays {color:"+colors[color6]+"}");

document.write(".calendar_weekends {color:"+colors[color7]+"}");

document.write("#calendar_today {color:"+colors[color8]+"}");

document.write("#clock {color:"+colors[color9]+"}");

document.write("#clock p {color:"+colors[color10]+"}");

document.write("#clock input {color:"+colors[color11]+"}");
}
Now, instead of creating 11 variables, is there any other way that I can use and how can I make it in a way that the colors will be random and never be the same at a time.

color1!=color2!=color3!=color4....

My main question is do I have to create 11 variables or not, the second part is less important.

P.S.: color 5 is randomInteger(9) on purpose; I didn't want it to be black, but if there is an answer to my question but color5 has to be randomInteger(10) instead of randomInteger(9); it is absolutely ok.

Thank you.

Last edited by IMayNeed; 06-09-2012 at 12:27 AM..
IMayNeed is offline   Reply With Quote