onclick only works once

G

Gregor Kofler

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
 
R

rf

Steve said:
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.
 
G

Gregor Kofler

Steve meinte:
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
 
D

Dr J R Stockton

In comp.lang.javascript message <03a27cb9-8dc5-4073-baca-502b0347ea99@v1
3g2000yqm.googlegroups.com>, Fri, 21 Nov 2008 01:33:11, Steve
It is obvious that I am self-learned, mainly from this forum and the
Oreilly Rhino book.

Please do not give ambiguous references.
 
G

Gregor Kofler

Dr J R Stockton meinte:
In comp.lang.javascript message <[email protected]>,


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
 
T

The Natural Philosopher

Gregor said:
Dr J R Stockton meinte:
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..
 
T

The Natural Philosopher

Gregor said:
David Mark meinte:

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..
 
D

David Mark

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

You suppose wrong (as usual.)
  Or


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]
 
G

Gregor Kofler

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>
 
T

Timo Reitz

The said:
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, (...)
 
T

The Natural Philosopher

Timo said:
The said:
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 ;-)
 
D

David Mark

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.)
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]

Read the FAQ. If you do so thoughtfully, a light should dawn; the entry
is sufficiently clear.
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.
 
T

Timo Reitz

The said:
Timo said:
The said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top