Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > onclick only works once

Reply
Thread Tools

onclick only works once

 
 
Steve
Guest
Posts: n/a
 
      11-20-2008
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.jou...hotspots1.html

javascript is at
http://members.chello.at/stephen.jou...e/hotspots1.js

Thanks in advance
 
Reply With Quote
 
 
 
 
Gregor Kofler
Guest
Posts: n/a
 
      11-20-2008
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.jou...hotspots1.html
>
> javascript is at
> http://members.chello.at/stephen.jou...e/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
 
Reply With Quote
 
 
 
 
rf
Guest
Posts: n/a
 
      11-21-2008

"Steve" <(E-Mail Removed)> wrote in message
news:24fa0$4925cfd0$3eb29844$(E-Mail Removed). ..
> 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.


 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      11-21-2008
Steve meinte:
> On Nov 20, 10:59 pm, Gregor Kofler <(E-Mail Removed)> 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
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      11-22-2008
In comp.lang.javascript message <03a27cb9-8dc5-4073-baca-502b0347ea99@v1
3g2000yqm.googlegroups.com>, Fri, 21 Nov 2008 01:33:11, Steve
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      11-22-2008
In comp.lang.javascript message <UuwVk.8$(E-Mail Removed)>,
Fri, 21 Nov 2008 11:57:56, Gregor Kofler <(E-Mail Removed)>
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.
 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      11-22-2008
Dr J R Stockton meinte:
> In comp.lang.javascript message <UuwVk.8$(E-Mail Removed)>,
> Fri, 21 Nov 2008 11:57:56, Gregor Kofler <(E-Mail Removed)>
> 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
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-22-2008
On Nov 22, 2:28*pm, Gregor Kofler <(E-Mail Removed)> wrote:
> Dr J R Stockton meinte:> In comp.lang.javascript message <UuwVk.8$(E-Mail Removed)>,
> > Fri, 21 Nov 2008 11:57:56, Gregor Kofler <(E-Mail Removed)>
> > 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]
 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      11-22-2008
David Mark meinte:
> On Nov 22, 2:28 pm, Gregor Kofler <(E-Mail Removed)> wrote:
>> Dr J R Stockton meinte:> In comp.lang.javascript message <UuwVk.8$(E-Mail Removed)>,
>>> Fri, 21 Nov 2008 11:57:56, Gregor Kofler <(E-Mail Removed)>
>>> 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
 
Reply With Quote
 
The Natural Philosopher
Guest
Posts: n/a
 
      11-22-2008
Gregor Kofler wrote:
> Dr J R Stockton meinte:
>> In comp.lang.javascript message <UuwVk.8$(E-Mail Removed)>,
>> Fri, 21 Nov 2008 11:57:56, Gregor Kofler <(E-Mail Removed)>
>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PythonWin crash - works only once after installation Ze'ev Python 1 06-17-2007 09:01 AM
Python c-api and reusing python-objects: works only once karye2004@gmail.com Python 1 11-08-2006 09:42 PM
SMTPClinet - mailMessage.DeliveryNotificationOptions only works once per session? Scott Townsend ASP .Net 2 10-18-2006 02:01 PM
istringstream: str() only works once? Brandon C++ 4 06-23-2004 07:22 PM
Writing Excel file from java works only once. Guy Java 5 12-11-2003 12:55 PM



Advertisments