onclick only works once

Discussion in 'Javascript' started by Steve, Nov 20, 2008.

  1. Steve

    Steve Guest

    Steve, Nov 20, 2008
    #1
    1. Advertising

  2. Steve meinte:
    > This simple script works first time but if you click a second time on
    > the button nothing happens. I expected that the roll function would be
    > executed again but it doesn't. Could someone explain why.
    >
    > http://members.chello.at/stephen.joung/testsite/hotspots1.html
    >
    > javascript is at
    > http://members.chello.at/stephen.joung/testsite/hotspots1.js
    >
    > Thanks in advance


    You are constantly pushing values onto the rolls array, and feeding the
    output always with the same first few entries. A quick run with a
    debugger (e.g. Firebug) would have revealed the problem.

    A

    rolls = [];

    at the beginning of the roll function should suffice. (The rest of the
    code is ..er... could be better.)

    Gregor
    Gregor Kofler, Nov 20, 2008
    #2
    1. Advertising

  3. Steve

    rf Guest

    "Steve" <> wrote in message
    news:24fa0$4925cfd0$3eb29844$...
    > This simple script works first time but if you click a second time on the
    > button nothing happens. I expected that the roll function would be
    > executed again but it doesn't. Could someone explain why.


    It is being executed again. You are just putting exactly the same numbers
    back there.
    rf, Nov 21, 2008
    #3
  4. Steve meinte:
    > On Nov 20, 10:59 pm, Gregor Kofler <> wrote:


    >> (The rest of the code is ..er... could be better.)

    >
    > It is obvious that I am self-learned, mainly from this forum and the
    > Oreilly Rhino book. So I would be grateful if you could give a little
    > more detail of how the code could be improved, or how you would have
    > written the code, in order that I can improve my javascript abilities.


    Not tested:

    function roll() {
    var rows = 2, cols = 5, sum, v, c, r,
    table = getElementsByTagName("table")[0];
    for(c = cols; c--;) {
    sum = 0;
    for(r = rows; r--;) {
    v = Math.ceil(Math.random()*6);
    sum += v;
    // innerHTML is not standardized
    table.rows[r].cells[c].innerHTML = v;
    }
    table.rows[rows].cells[c].innerHTML = sum;
    }
    }

    Gregor
    Gregor Kofler, Nov 21, 2008
    #4
  5. In comp.lang.javascript message <03a27cb9-8dc5-4073-baca-502b0347ea99@v1
    3g2000yqm.googlegroups.com>, Fri, 21 Nov 2008 01:33:11, Steve
    <> posted:

    >It is obvious that I am self-learned, mainly from this forum and the
    >Oreilly Rhino book.


    Please do not give ambiguous references.

    --
    (c) John Stockton, nr London UK. replyYYWW merlyn demon co uk Turnpike 6.05.
    Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
    Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
    No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
    Dr J R Stockton, Nov 22, 2008
    #5
  6. In comp.lang.javascript message <UuwVk.8$>,
    Fri, 21 Nov 2008 11:57:56, Gregor Kofler <>
    posted:

    > v = Math.ceil(Math.random()*6);


    Code containing such a line is unlikely to be correct, but the fault
    will not be found with inadequate testing. Read the FAQ; or think.

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <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 J R Stockton, Nov 22, 2008
    #6
  7. Dr J R Stockton meinte:
    > In comp.lang.javascript message <UuwVk.8$>,
    > Fri, 21 Nov 2008 11:57:56, Gregor Kofler <>
    > posted:
    >
    >> v = Math.ceil(Math.random()*6);

    >
    > Code containing such a line is unlikely to be correct, but the fault
    > will not be found with inadequate testing. Read the FAQ; or think.
    >

    Interesting. In what respect is this code unlikely to be correct? Or
    rather, how do you infer that the rest of the code must be crap, if it
    contains such a line? Let's see: 2 static methods, a multiplication,
    result assigned to a local variable. Could you shed some light on the
    issues?

    Gregor
    Gregor Kofler, Nov 22, 2008
    #7
  8. Steve

    David Mark Guest

    On Nov 22, 2:28 pm, Gregor Kofler <> wrote:
    > Dr J R Stockton meinte:> In comp.lang.javascript message <UuwVk.8$>,
    > > Fri, 21 Nov 2008 11:57:56, Gregor Kofler <>
    > > posted:

    >
    > >>      v = Math.ceil(Math.random()*6);

    >
    > > Code containing such a line is unlikely to be correct, but the fault
    > > will not be found with inadequate testing.  Read the FAQ; or think.

    >
    > Interesting. In what respect is this code unlikely to be correct? Or


    If the Math.random call returns 0.

    [snip]
    David Mark, Nov 22, 2008
    #8
  9. David Mark meinte:
    > On Nov 22, 2:28 pm, Gregor Kofler <> wrote:
    >> Dr J R Stockton meinte:> In comp.lang.javascript message <UuwVk.8$>,
    >>> Fri, 21 Nov 2008 11:57:56, Gregor Kofler <>
    >>> posted:
    >>>> v = Math.ceil(Math.random()*6);
    >>> Code containing such a line is unlikely to be correct, but the fault
    >>> will not be found with inadequate testing. Read the FAQ; or think.

    >> Interesting. In what respect is this code unlikely to be correct? Or

    >
    > If the Math.random call returns 0.


    Ah, damn. Right. Yes, floor() + 1 is definitely the better solution.


    Gregor
    Gregor Kofler, Nov 22, 2008
    #9
  10. Gregor Kofler wrote:
    > Dr J R Stockton meinte:
    >> In comp.lang.javascript message <UuwVk.8$>,
    >> Fri, 21 Nov 2008 11:57:56, Gregor Kofler <>
    >> posted:
    >>
    >>> v = Math.ceil(Math.random()*6);

    >>
    >> Code containing such a line is unlikely to be correct, but the fault
    >> will not be found with inadequate testing. Read the FAQ; or think.
    >>

    > Interesting. In what respect is this code unlikely to be correct?


    Well its an odd thing, but its one way of rolling a pseudorandom die, I
    suppose..


    Or
    > rather, how do you infer that the rest of the code must be crap, if it
    > contains such a line? Let's see: 2 static methods, a multiplication,
    > result assigned to a local variable. Could you shed some light on the
    > issues?
    >


    I suspect he couldn't see the point of a random integer between one and
    six..


    > Gregor
    The Natural Philosopher, Nov 22, 2008
    #10
  11. Gregor Kofler wrote:
    > David Mark meinte:
    >> On Nov 22, 2:28 pm, Gregor Kofler <> wrote:
    >>> Dr J R Stockton meinte:> In comp.lang.javascript message
    >>> <UuwVk.8$>,
    >>>> Fri, 21 Nov 2008 11:57:56, Gregor Kofler <>
    >>>> posted:
    >>>>> v = Math.ceil(Math.random()*6);
    >>>> Code containing such a line is unlikely to be correct, but the fault
    >>>> will not be found with inadequate testing. Read the FAQ; or think.
    >>> Interesting. In what respect is this code unlikely to be correct? Or

    >>
    >> If the Math.random call returns 0.

    >
    > Ah, damn. Right. Yes, floor() + 1 is definitely the better solution.
    >
    >

    Wel the spec says that it returns a number between 0 and 1. To me
    'between' implies never 0 and never one..


    > Gregor
    The Natural Philosopher, Nov 22, 2008
    #11
  12. Steve

    David Mark Guest

    On Nov 22, 3:34 pm, The Natural Philosopher <> wrote:
    > Gregor Kofler wrote:
    > > Dr J R Stockton meinte:
    > >> In comp.lang.javascript message <UuwVk.8$>,
    > >> Fri, 21 Nov 2008 11:57:56, Gregor Kofler <>
    > >> posted:

    >
    > >>>      v = Math.ceil(Math.random()*6);

    >
    > >> Code containing such a line is unlikely to be correct, but the fault
    > >> will not be found with inadequate testing.  Read the FAQ; or think.

    >
    > > Interesting. In what respect is this code unlikely to be correct?

    >
    > Well its an odd thing, but its one way of rolling a pseudorandom die, I
    > suppose..


    You suppose wrong (as usual.)

    >
    >   Or
    >
    > > rather, how do you infer that the rest of the code must be crap, if it
    > > contains such a line? Let's see: 2 static methods, a multiplication,
    > > result assigned to a local variable. Could you shed some light on the
    > > issues?

    >
    > I suspect he couldn't see the point of a random integer between one and
    > six..
    >


    If you would just read, you wouldn't have to base your conclusions on
    suspicion.

    [snip quoted signature]
    David Mark, Nov 22, 2008
    #12
  13. The Natural Philosopher meinte:

    > Wel the spec says that it returns a number between 0 and 1. To me
    > 'between' implies never 0 and never one..


    It depends. For example, the 'between' operator in SQL means 'include
    both upper and lower bounds'. From what I know (or rather "remember"),
    many programming languages handle random number generation the way
    [lower bound, upper bound).

    Here [1]:

    "Returns a pseudo-random number in the range [0,1) — that is, between 0
    (inclusive) and 1 (exclusive). The random number generator is seeded
    from the current time, as in Java."

    Gregor

    [1]
    <https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Math/random>
    Gregor Kofler, Nov 22, 2008
    #13
  14. Steve

    David Mark Guest

    On Nov 22, 3:36 pm, The Natural Philosopher <> wrote:
    > Gregor Kofler wrote:
    > > David Mark meinte:
    > >> On Nov 22, 2:28 pm, Gregor Kofler <> wrote:
    > >>> Dr J R Stockton meinte:> In comp.lang.javascript message
    > >>> <UuwVk.8$>,
    > >>>> Fri, 21 Nov 2008 11:57:56, Gregor Kofler <>
    > >>>> posted:
    > >>>>>      v = Math.ceil(Math.random()*6);
    > >>>> Code containing such a line is unlikely to be correct, but the fault
    > >>>> will not be found with inadequate testing.  Read the FAQ; or think..
    > >>> Interesting. In what respect is this code unlikely to be correct? Or

    >
    > >> If the Math.random call returns 0.

    >
    > > Ah, damn. Right. Yes, floor() + 1 is definitely the better solution.

    >
    > Wel the spec says that it returns a number between 0 and 1. To me
    > 'between' implies never 0 and never one..


    The spec implies nothing. It explicitly states that it *can* be 0.

    https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Math/random

    For the love of Christ, stop posting nonsense here.
    David Mark, Nov 22, 2008
    #14
  15. Steve

    Timo Reitz Guest

    The Natural Philosopher wrote:
    > Wel the spec says that it returns a number between 0 and 1. To me
    > 'between' implies never 0 and never one..


    ECMA-262 says:
    > [Math.random()] Returns a number value with positive sign, greater than

    or equal to 0 but less than 1, (...)
    Timo Reitz, Nov 22, 2008
    #15
  16. Timo Reitz wrote:
    > The Natural Philosopher wrote:
    >> Wel the spec says that it returns a number between 0 and 1. To me
    >> 'between' implies never 0 and never one..

    >
    > ECMA-262 says:
    > > [Math.random()] Returns a number value with positive sign, greater

    > than or equal to 0 but less than 1, (...)

    right. so even te spces dont agree ;=)

    bet its different in IE7 from firefaux ;-)
    The Natural Philosopher, Nov 23, 2008
    #16
  17. Steve

    David Mark Guest

    On Nov 23, 4:33 am, The Natural Philosopher <> wrote:
    > Timo Reitz wrote:
    > > The Natural Philosopher wrote:
    > >> Wel the spec says that it returns a number between 0 and 1. To me
    > >> 'between' implies never 0 and never one..

    >
    > > ECMA-262 says:
    > >  > [Math.random()] Returns a number value with positive sign, greater
    > > than or equal to 0 but less than 1, (...)

    >
    > right. so even te spces dont agree ;=)


    Wrong.

    >
    > bet its different in IE7 from firefaux ;-)


    You lose (again.)
    David Mark, Nov 23, 2008
    #17
  18. In comp.lang.javascript message <
    et>, Sat, 22 Nov 2008 20:36:21, The Natural Philosopher <> posted:
    >Gregor Kofler wrote:
    >> David Mark meinte:
    >>> On Nov 22, 2:28 pm, Gregor Kofler <> wrote:
    >>>> Dr J R Stockton meinte:> In comp.lang.javascript message
    >>>><UuwVk.8$>,
    >>>>> Fri, 21 Nov 2008 11:57:56, Gregor Kofler <>
    >>>>> posted:
    >>>>>> v = Math.ceil(Math.random()*6);
    >>>>> Code containing such a line is unlikely to be correct, but the fault
    >>>>> will not be found with inadequate testing. Read the FAQ; or think.
    >>>> Interesting. In what respect is this code unlikely to be correct? Or


    Read the FAQ. If you do so thoughtfully, a light should dawn; the entry
    is sufficiently clear.

    >>> If the Math.random call returns 0.

    >> Ah, damn. Right. Yes, floor() + 1 is definitely the better solution.
    >>

    >Wel the spec says that it returns a number between 0 and 1. To me
    >'between' implies never 0 and never one..


    Remember that most computing specifications, and non-specification
    descriptions are originated without much thought by the semi-literate.
    When specifications are upgraded to national or international standards,
    the errors are usually fixed. As a demonstration, look for descriptions
    of methods of the date object. Look also at code purporting to give
    Week Number, when written in an area that does not use ISO 8601.

    --
    (c) John Stockton, nr London UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Check boilerplate spelling -- error is a public sign of incompetence.
    Never fully trust an article from a poster who gives no full real name.
    Dr J R Stockton, Nov 23, 2008
    #18
  19. Steve

    Timo Reitz Guest

    The Natural Philosopher schrieb:
    > Timo Reitz wrote:
    >> The Natural Philosopher wrote:
    >>> Wel the spec says that it returns a number between 0 and 1. To me
    >>> 'between' implies never 0 and never one..

    >>
    >> ECMA-262 says:
    >> > [Math.random()] Returns a number value with positive sign, greater

    >> than or equal to 0 but less than 1, (...)

    > right. so even te spces dont agree ;=)


    Which specs do you talk about?

    > bet its different in IE7 from firefaux ;-)


    I don't think so:

    > The pseudorandom number generated is from 0 (inclusive) to 1 (exclusive),
    > that is, the returned number can be zero, but it will always be less
    > than one.

    http://msdn.microsoft.com/en-us/library/41336409(VS.85).aspx

    > Returns a pseudo-random number in the range [0,1) — that is, between
    > 0 (inclusive) and 1 (exclusive).

    https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Math/random
    Timo Reitz, Nov 23, 2008
    #19
    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. Scott Townsend
    Replies:
    2
    Views:
    4,351
  2. Guy
    Replies:
    5
    Views:
    4,512
    Bob Rivers
    Dec 11, 2003
  3. Brandon
    Replies:
    4
    Views:
    9,104
    John Harrison
    Jun 23, 2004
  4. Replies:
    1
    Views:
    401
    Gabriel Genellina
    Nov 8, 2006
  5. Ze'ev
    Replies:
    1
    Views:
    399
    Ze'ev
    Jun 17, 2007
Loading...

Share This Page