Timing response from prompt box ...

M

M100C

My son is learning his multiplication facts, and I've built a nifty
little browser applet that runs with javascript. 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?
 
T

Thomas 'PointedEars' Lahn

M100C said:
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
 
J

Jorge

My son is learning his multiplication facts, and I've built a nifty
little browser applet that runs with javascript.  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?

Yes.

The date object is not a live, running clock. Instead, it represents a
given, fixed date and time. IOW, you need to create one before the
prompt and another after.

var time= +new Date();
response = prompt(fact);
time= (+new Date()- time) / 1e3;
 
T

Thomas 'PointedEars' Lahn

Jorge said:
var time= +new Date();
response = prompt(fact);
time= (+new Date()- time) / 1e3;

The unary pluses are superfluous here, the minus already converts to number.


PointedEars
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top