November 1st, 2011, 08:28 PM
Quote that changes weekly
I want to show a quote on a website for one week and then change to another quote a week later, say every Monday it changes? I have 52 quotes for the year. Im not sure how to write it up? I also wanted the first letter of the Quote to be a large letter to make it look funky.
Someone helped me with this below but this way just changes random when the page is refreshes. not really what I want.
Can you please help me and show me where about top paste it in Html. thanks.
var keyword = verses[Math.floor(Math.random()*verses.length)]
November 1st, 2011, 11:52 PM
It randomly changes because your index into the verses array is based on Math.random(); if you want to display a new quote for each week of the year you need to base it on the current date instead.
The modulo operator (%) can be used to restrict a value to a certain range.
For example, this code will take the current timestamp in milliseconds [now], divide it by 3600000.0 milliseconds (one hour), then divide it by the number of quotes you have [verses.length] and take the remainder (using the modulo operator).
var now = (new Date()).getTime();
var index = Math.round(now/3600000.0) % verses.length;
var keyword = verses[index];
Effectively, this produces a number (index) between 0 and 4 that increases to the next number once per hour and wraps back around to 0 after it exceeds 5. This number can be used as an index into your verses array.
For an approximate solution you could change 3600000.0 so that it represents 7 days in milliseconds instead of 1 hour in milliseconds. Then you would have code that cycles through a list of quotes, moving to the next one approximately once a week.
A more precise solution is possible of course, but requires more effort because you will need to compute the current week of the year.