newbie: question on rotating quotes

Discussion in 'Javascript' started by Sez1, Aug 10, 2004.

  1. Sez1

    Sez1 Guest

    Hello.

    I am trying to set up two rotating quotes on my web page.

    I only have a pool of 20 quotes to start with, which I've saved
    separately in .js files using document.write to include html formatting.

    The two quotes appearing on the page obviously should not be duplicated;
    but I'm not sure how to set it up.

    I am setting up the variables that govern the quotes in a separate JS
    file like this:

    var quote1 = Math.random()*20;
    quote1 = Math.round(quote1);

    var quote2 = Math.random()*5;
    quote2 = Math.round(quote2);

    Can I simply add an IF statement to this, like:

    IF (quote1==quote2 && quote1=20)
    {quote1 = quote2-1}
    ELSEIF (quote1==quote2 && quote1<20)
    {quote1 = quote2+1}


    THANKS FOR any pointers on this....
    Sez1, Aug 10, 2004
    #1
    1. Advertising

  2. Sez1

    Lee Guest

    Sez1 said:
    >
    >Hello.
    >
    >I am trying to set up two rotating quotes on my web page.
    >
    >I only have a pool of 20 quotes to start with, which I've saved
    >separately in .js files using document.write to include html formatting.
    >
    >The two quotes appearing on the page obviously should not be duplicated;
    >but I'm not sure how to set it up.
    >
    >I am setting up the variables that govern the quotes in a separate JS
    >file like this:
    >
    > var quote1 = Math.random()*20;


    That will actually select one of 21 values, 0-20, with zero and twenty being
    selected only half as often as any of the other values.

    var quote1 = Math.floor(Math.random()*20);

    will select twenty values 0-19, and will select them more evenly.


    > quote1 = Math.round(quote1);
    >
    > var quote2 = Math.random()*5;
    > quote2 = Math.round(quote2);
    >
    >Can I simply add an IF statement to this, like:
    >
    > IF (quote1==quote2 && quote1=20)
    > {quote1 = quote2-1}
    > ELSEIF (quote1==quote2 && quote1<20)
    > {quote1 = quote2+1}


    That would work, but it's actually simpler to do it better.
    Think of the quotes as being arranged around a wheel (like a roulette wheel),
    with each quote numbered from 0-19.

    After choosing the first quote, choose a second random number from 1-19 and
    count that many spaces around the circle to find the second quote.

    You don't need to test for equality, because you know that you're never counting
    all the way around the circle. Here's the code:

    var count=20;
    var quote1 = Math.floor(Math.random()*count);
    var quote2 = (quote1+Math.floor(Math.random()*(count-1)+1))%count;
    Lee, Aug 10, 2004
    #2
    1. Advertising

  3. JRS: In article <>, dated Tue,
    10 Aug 2004 03:16:43, seen in news:comp.lang.javascript, Sez1
    <> posted :
    >Hello.
    >
    >I am trying to set up two rotating quotes on my web page.
    >
    >I only have a pool of 20 quotes to start with, which I've saved
    >separately in .js files using document.write to include html formatting.
    >
    >The two quotes appearing on the page obviously should not be duplicated;
    >but I'm not sure how to set it up.
    >
    >I am setting up the variables that govern the quotes in a separate JS
    >file like this:
    >
    > var quote1 = Math.random()*20;
    > quote1 = Math.round(quote1);
    >
    > var quote2 = Math.random()*5;
    > quote2 = Math.round(quote2);
    >
    >Can I simply add an IF statement to this, like:
    >
    > IF (quote1==quote2 && quote1=20)
    > {quote1 = quote2-1}
    > ELSEIF (quote1==quote2 && quote1<20)
    > {quote1 = quote2+1}


    It will work much better if the third "=" character is duplicated.

    There is no need to repeat the equality test :
    if (q1==q2) { q1==20 ? q1=0 : q1++ }
    or if (q1==q2) { q1++ ; q1%=20 }

    It would be better to use Random, as in FAQ 4.22, which gives the
    numbers you want (with Round, you get one more possibility) ;

    q1 = Random(20)
    do q2 = Random(5) ; while (q2==q1)

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Aug 10, 2004
    #3
  4. JRS: In article <>, dated Tue,
    10 Aug 2004 03:16:43, seen in news:comp.lang.javascript, Sez1
    <> posted :

    >I am trying to set up two rotating quotes on my web page.
    >
    >I only have a pool of 20 quotes to start with, which I've saved
    >separately in .js files using document.write to include html formatting.
    >
    >The two quotes appearing on the page obviously should not be duplicated;
    >but I'm not sure how to set it up.


    Moreover, the following function will return an array containing in
    random order M elements, all different, drawn from 1..N. If N-M is not
    too small it will be reasonably efficient.

    function RndOrdrSubSet(M, N) { var aSet=[], bSet=[], j
    if (M>N) M = N
    while (M>0) { j = Math.floor(Math.random()*N + 1)
    if (!aSet[j]) bSet[--M] = aSet[j] = j }
    return bSet }

    See just *above* <URL:http://www.merlyn.demon.co.uk/js-randm.htm#NUD>.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Aug 11, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,824
    Hermit Dave
    Sep 22, 2004
  2. Chris
    Replies:
    1
    Views:
    13,589
    Oisin
    Mar 24, 2006
  3. Lawrence Tierney

    Multiline quotes - escaping quotes - et al

    Lawrence Tierney, Dec 24, 2003, in forum: Java
    Replies:
    3
    Views:
    4,474
    Andrew Thompson
    Dec 24, 2003
  4. jOhn
    Replies:
    1
    Views:
    207
    Phlip
    Jan 29, 2008
  5. Richard Sandoval
    Replies:
    5
    Views:
    184
    7stud --
    Apr 26, 2011
Loading...

Share This Page