If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

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.

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.

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.

As has often been stated in this forum, document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded. Use DOM methods instead.

I am new to java..

Be aware that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

"Let us be thankful for the fools. But for them the rest of us could not succeed. " - Mark Twain, US humorist, novelist, short story author, & wit (1835 - 1910)

Last edited by Philip M; 06-08-2012 at 07:50 PM.

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

In that case using document.write() is fine for this purpose to help students quickly get output to the browser while learning the basics of javascript.

As soon as you start learning more about the Document Object Model (DOM), you really should then use more appropriate DOM methods to output to the browser.

document.write outputs to the current page only while it is still loading. If called after the the page has loaded, it will output to a new page. document.write is really only useful for writing to child pages.

</offTopic>

Using innerHTML is far more straightforward and less error prone for beginners to use than document.write is - there's no reason for using the more complicated document.write rather than innerHTML except in those advanced userscripts where document.write is the only way that works.

There are far fewer situations where innerHTML will break the web page functionality compared to the ways that document.write can break the functionality.

Of course the way that always works is to use the proper DOM commands that make up about 60% of JavaScript but learning those is a bit more complex and since innerHTML will work most of the time it is the obvious choice for beginners to use first.

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.

To find the day of the week for any date, use Zeller's Congruence ( LINK ).

Also you are misusing parseInt in that code where you should be using Number() (which is really only needed around the (month+1) part if you are passing the month astext instead of as a number - you don't need to convert to base 10 anywhere in that function and so it doesn't need parseInt at all.

Also you are misusing parseInt in that code where you should be using Number() (which is really only needed around the (month+1) part if you are passing the month astext instead of as a number - you don't need to convert to base 10 anywhere in that function and so it doesn't need parseInt at all.

That code snippet is just a standard algorithm I use for my own stuff here. I have the same code in C, Intel ASM, Mototola ASM, GWBASIC and Javascript.

I do need parseInt because the algorithm depends on integer math and indeed fails if floating point numbers get involved.

If, for example, part of the algorithm does "1987 / 4", the result (496.75) would propagate through and produce an incorrect day of week. So I have to force JS to act as though all numbers are (INT).

For fun, make an array of day names, then run the JS code and plug in well known dates (Dec 7, 1941 - Sep 11, 2001, - etc...) and see what happens with and without "parseInt".

Users who have thanked Krupski for this post:

Thank you for the help everybody. I am copying this whole page and getting home. Today, I just got out of 2 finals so, I will rest probably, but Weekend, I will finish this calendar and the rest of the site (one transaction, one credit card, one comment forms).

I have to ask this too (after checking out books mostly -don't have internet at home-); I have a frame (row) at the top and when I make the window smaller, the text disappears. Is there any way to avoid that or with the right words, what are the ways to avoid that? I checked out the books I had, but either I am not getting it or I am having a hard time to get it or something else!

The difference between using the function intended to convert to integer Math.floor(x) and one that does so as a side effect but which is actually intended for a different purpose parseInt(x,10) is the same difference as there is between driving a Ford to work or driving a Cesna to work - one is intended for that purpose and the other is intended to be used for something different.

Just watch out that you don't get your wings knocked off next time you drive your Cesna to work.

In particular parseInt should NEVER be used where dates are concerned because of the danger that it will treat numbers as base 8 instead of base 10 and so will fail to work on the 8th and ninth of each month as well as failing completely for August and September. I have lost count of the number of forum questions I have answered where this problem occurred because someone misused parseInt.

Anyway that code you posted with 15 lines in the function is about 13 lines longer than necessary as I showed in my following post where including the actual call to the function my solution used only 3 lines of code to do the same thing.