M100C wrote:
> My son is learning his multiplication facts, and I've built a nifty
> little browser applet that runs with javascript.
Such programs are usually _not_ called applets. That term is more or less
reserved for client-side Java (!= JavaScript) programs running in browsers.
> I pass a randomly constructed math fact (e.g. "6 X 7 =") to a prompt box,
> and await his input. However, I'd like to capture how long it takes him
> to answer the prompt. I've tried something like this:
>
> var date = new Date()
> var time = date.getTime()
> response = prompt (fact)
> time = date.getTime() - time
>
> and
>
> var s_date = new Date()
> var e_date = new Date()
> var s_time = s_date.getTime()
> response = prompt (fact)
> var e_time = e_date.getTime()
> var time = (e_time - s_time) / 1000
>
> but, when I watch these variables, they are assigned the same time.
> Not sure what I am doing wrong. Any suggestions?
The Date instance referred to by `date' always stores the same time value,
regardless when you call its getTime() method.
In the first block, lose the unnecessary `time' initialization, declare
`response', replace `prompt' with `window.prompt', replace the second call
with `new Date()', and subtract `date'. (Implicit type conversion to
number allows this operation to succeed, see ES3/5, section 9.3. You might
want to choose more fitting identifiers in the process.)
Lose the second block, and delimit your assignments with a trailing
semicolon as recommended.
HTH
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
|