/    Sign up×
Community /Pin to ProfileBookmark

I am new to programming. i see a lot of random quote. can any one help with the quote coming in serially instead?
Thank you

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 06.2019 — Track the last and/or next quote ID in a cookie, perhaps? Then a little logic on the server side to read the cookie, pick the next quote (or wrap back to the first one), and update the ID in the cookie?
Copy linkTweet thisAlerts:
@JMRKERAug 06.2019 — Can also use the local storage method as well. May be (?) simpler (to me) to implement.
Copy linkTweet thisAlerts:
@VITSUSAAug 07.2019 — @aceklaes9#1607245 What you want exactly? Which type of help you have required?
Copy linkTweet thisAlerts:
@JMRKERAug 07.2019 — Another thought: Depending upon how many "quotes" you have they could be numbered by the day of the week, month, month-interval, year. For example with 366 quotes there would be no repeats for at least a year (even leap years). Just calculate the day-of-the-year and pull from a quote array. .
Copy linkTweet thisAlerts:
@NogDogAug 07.2019 — @JMRKER#1607311

Depends on whether you want a different quote each day, or a different quote on each page load. :)
Copy linkTweet thisAlerts:
@aceklaes9authorAug 08.2019 — Sorry my question wasn't clear enough. I am just trying to do a simple quote app that picks a quote from an array of quotes and displays each one base on date.
Copy linkTweet thisAlerts:
@JMRKERAug 08.2019 — @aceklaes9#1607336

Do you have an example of what you have created thus far?

Where are you getting the daily quotes from?

Will be glad to help, but site is for learning, not just free code.
Copy linkTweet thisAlerts:
@aceklaes9authorAug 11.2019 — @JMRKER#1607357 Yes

var quotes = {

'20190808':{quote:'one',author:'fred'},

'20190809':{quote:'two',author:'mary'},

'20190810':{quote:'three',author:'sue'}

};

function showQuote(quotes) {

var d = new Date();

var key = '' + d.getFullYear() +

('0'+(d.getMonth()+1)).slice(-2) +

('0'+d.getDate()).slice(-2);

return quotes[key]

}

// On 2019-08-09 shows quote: two, author: mary

console.log(showQuote(quotes));
Copy linkTweet thisAlerts:
@JMRKERAug 11.2019 — See if you can modify this to your needs.

I saw no need for the specific dates in your example and chose just a routine text file

that can be used in past and future years as well as the current.

Note: I did not put in all 365+ quotes that I have, just enough for a month's example.
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/&gt;

&lt;title&gt; Abbreviated Quote-Of-The-Day &lt;/title&gt;
&lt;style&gt;
#quoteOfDay { position: absolute; top: 120px; left: 150px; font-size: 1.5em;
padding: 5px; width: 300px; background-color: yellow;
border: 3px solid blue;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;div id="quoteOfDay" class="hide"&gt;&lt;/div&gt;

&lt;!-- script src="quotes.js"&gt;&lt;/script --&gt;
&lt;script&gt;
// Quotes -- abbreviated file contents from much larger quote list
var quotes =
<span><code>&quot;Anything in life worth having is worth working for. - Andrew Carnegie&quot;,

&quot;Success often comes to those who dare to act. It seldom goes to the timid who
are ever afraid of the consequences. - Jawaharlal Nehru&quot;,

&quot;I just love when people say I can't do something because all my life people said I wasn't going to make it. - Ted Turner&quot;,

&quot;Success is never ending, failure is never final. - Dr. Robert Schuller&quot;,

&quot;Great thoughts speak only to the thoughtful mind, but great actions speak to all mankind. - Emily P. Bissell&quot;,

&quot;It takes a strong fish to swim against the current. Even a dead one can float with it. - John Crowe&quot;,

&quot;Obstacles are those frightful things you can see when you take your eyes off your goal. - Henry Ford&quot;,

&quot;You will never find time for anything. You must make it. - Charles Buxton&quot;,

&quot;Remove failure as an option. - Joan Lunden&quot;,

&quot;There is no one giant step that does it.its a lot of little steps-.Peter A Cohen&quot;,

&quot;Shoot for the moon. Even if you miss, you will land among the stars. - Les Brown&quot;,

&quot;Some of us have great runways already built for us. If you have one take off,
but if you don't have one realise its your responsibility to grab a shovel
and build one for yourself and for those who will follow after you - Amelia Earhart &quot;,

&quot;Work like you don't need the money. Love like you ve never been hurt. Dance like nobody is watching. - Mark Twain&quot;,

&quot;What the mind of man can conceive and believe, it can achieve. - Napoleon Hill&quot;,

&quot;There are no secrets to success. It is the result of preparation, hard work, and
learning from failures - Colin Powell&quot;,

&quot;What lies behind us and what lies before us are tiny matters compared to what
lies within us. - Ralph Waldo Emerson&quot;,

&quot;Great works are performed not by strength but by perseverance.- Samuel Johnson&quot;,

&quot;The difference between a successful person and others is not a lack of strength,
not a lack of knowledge, but rather a lack of will. - Vince Lombardi&quot;,

&quot;The man who follows the crowd will usually get no further than the crowd. The
man who walks alone is likely to find himself in places no one has ever been.-Alan Ashley-Pitt&quot;,

&quot;It takes 20 years to build a reputation and five minutes to ruin it. If you think
about that, you will do things differently. - Warren Buffett&quot;,

&quot;Champions aren't made in the gyms. Champions are made from something they
have deep inside them - a desire, a dream, a vision. - Muhammad Ali&quot;,

&quot;Dwell not on the past. Use it to illustrate a point, then leave it behind. Nothing
really matters except what you do now in this instant of time. From this moment
onwards you can be an entirely different person, filled with love and understanding, &lt;br/&gt;
ready with an outstretched hand, uplifted and positive in every thought and deed.- Eileen Caddy&quot;,

&quot;The critical ingredient is getting off your butt and doing something. Its as simple
as that. A lot of people have ideas, but there are few who decide to do something about them
now. Not tomorrow. Not next week. But today. The true entrepreneur is a doer.- Nolan Bushnell&quot;,

&quot;Whatever you vividly imagine, ardently desire, sincerely believe, and
enthusiastically act upon must inevitably come to pass - Paul J. Meyer&quot;,

&quot;Keep away from small people who try to belittle your ambitions. Small people
always do that, but the really great make you feel that you too can become great.- Mark Twain&quot;,

&quot;For anything worth having one must pay the price and the price is always work,patience, love,
self-sacrifice. No paper currency, no promises to pay, but the gold of real service.- John Burroughs&quot;,

&quot;To be successful, you must decide exactly what you want to accomplish, then resolve to pay the price to get it. - Bunker Hunt&quot;,

&quot;You just can't beat the person who never gives up. - Babe Ruth&quot;,

&quot;When you get right down to the root of the meaning of the word succeed, you
find it simply means to follow through. - F.W. Nichol&quot;,

&quot;Successful people are always looking for opportunities to help others.
Unsuccessful people are always asking, Whats in it for me? - Brian Tracy&quot;,

&quot;If you don't set goals, you can't regret not reaching them. - Yogi Berra&quot;,

&quot;Success is achieved by those who try and keep trying with a positive mental attitude. - W. Clement Stone&quot;,

&quot;I couldn't wait for success, so I went ahead without it. - Jonathan Winters&quot;
</code></span>.split('nn');
&lt;/script&gt;

&lt;script&gt;
// From: http://javascriptexample.net/b/?topic=get-day-of-year-script-bug-with-daylight-savings-time-and-2nd-bugat-midnight/
Date.prototype.isLeapYear= function(){
return new Date(this.getFullYear(), 1, 29).getDate()==29;
}
// Get Day of Year
Date.prototype.getDOY = function() {
var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var yn = this.getFullYear();
var mn = this.getMonth();
var dn = this.getDate();
var dayOfYear = dayCount[mn] + dn;
if(mn &gt; 1 &amp;&amp; this.isLeapYear()) dayOfYear++;
return dayOfYear;
}

function message(msgNumber) {
let d = new Date(),
DOY = d.getDOY();

// msgNumber = DOY; // for file with 365+ quotes
msgNumber = (DOY % 31); // abbreviated file with 30+ quotes
document.getElementById('quoteOfDay').innerHTML = quotes[msgNumber];
}
message(0);

&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@aceklaes9authorAug 12.2019 — @JMRKER#1607471 Thank You.
Copy linkTweet thisAlerts:
@JMRKERAug 12.2019 — @aceklaes9#1607475

You're most welcome.

Glad I could help.

Good Luck! :)
Copy linkTweet thisAlerts:
@NogDogAug 12.2019 — Just because, here's a PHP approach using the modulus % operator. Not sure if the same could be done in JavaScript? Advantage: it always cycles through the entire array before going back to the beginning. Disadvantage: the date will roll over at midnight UTC, not your local time -- plus probably off by few seconds due to leap seconds that have been added since the start of "UNIX time". :)

<i>
</i>php &gt;
php &gt; $quotes = ['quote 0', 'quote 1', 'quote 2', 'quote 3'];
php &gt; $day = round(time() / (60 * 60 * 24));
php &gt; echo $quotes[$day % count($quotes)];
quote 1
php &gt;
Copy linkTweet thisAlerts:
@JMRKERSep 08.2019 — @NogDog#1607494

Your code can be converted to JS from PHP:
<i>
</i>&lt;script&gt;
const $quotes = ['quote 0', 'quote 1', 'quote 2', 'quote 3'];
let t = new Date(); // current date or specific choice: new Date(2019,8,8);
// let $day = Math.round(t.getTime() / (1000 * 60 * 60 * 24)); // uses time function
// let $day = t.getDate(); // uses date function
let $day = t.getDay(); // uses day function
console.log($day+'n'+$quotes[$day % $quotes.length]+'n'+$quotes.length);
&lt;/script&gt;

Note: You can use $day converted to integer using several Date() methods.


All work with variable length '$quotes' array.
×

Success!

Help @aceklaes9 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...