NameSilo

Javascript runthrough

Spaceship Spaceship
Watch
Heya guys!

For a project at uni, I'm making a website, I've been told by my 'client' that he wants a simple reaction test game, somthing to wow his friends i guess. I've found a free source code for a reaction test game, but i was wondering if someone could go through the code and write next to a couple of the important lines of script, telling me what it does.

I'm pretty interested in how it works, and it would be great if someone could do this for me!


Here's the code:


(head code)

<!-- Begin
var startTime=new Date();
var endTime=new Date();
var startPressed=false;
var bgChangeStarted=false;
var maxWait=20;
var timerID;
function startTest() {
document.bgColor=document.response.bgColorChange.options[document.response.bgColorChange.selectedIndex].text;
bgChangeStarted=true;
startTime=new Date();
}
function remark(responseTime) {
var responseString="";
if (responseTime < 0.10)
responseString="Well done!";
if (responseTime >= 0.10 && responseTime < 0.20)
responseString="Nice!";
if (responseTime >=0.20 && responseTime < 0.30)
responseString="Could be better...";
if (responseTime >=0.30 && responseTime < 0.60)
responseString="Keep practicing!";
if (responseTime >=0.60 && responseTime < 1)
responseString="Have you been drinking?";
if (responseTime >=1)
responseString="Did you fall asleep?";
return responseString;
}
function stopTest() {
if(bgChangeStarted) {
endTime=new Date();
var responseTime=(endTime.getTime()-startTime.getTime())/1000;
document.bgColor="white";
alert("Your response time is: " + responseTime + " seconds " + "\n" + remark(responseTime));
startPressed=false;
bgChangeStarted=false;
}
else
{
if (!startPressed) {
alert("press start first to start test");
}
else
{
clearTimeout(timerID);
startPressed=false;
alert("cheater! you pressed too early!");
}
}
}
var randMULTIPLIER=0x015a4e35;
var randINCREMENT=1;
var today=new Date();
var randSeed=today.getSeconds();
function randNumber() {
randSeed = (randMULTIPLIER * randSeed + randINCREMENT) % (1 << 31);
return((randSeed >> 15) & 0x7fff) / 32767;
}
function start() {
if(startPressed) {
alert("Already started. Press stop to stop");
return;
}
else
{
startPressed=true;
timerID=setTimeout('startTest()', 20000*randNumber());
}
}
// End -->
</SCRIPT>


(body code)

<CENTER>
<FORM name="response">
Change background color to:
<SELECT name="bgColorChange">
<option selected>deeppink
<option>aliceblue
<option>crimson
<option>darkkhaki
<option>cadetblue
<option>darkorchid
<option>coral
<option>chocolate
<option>mediumslateblue
<option>tomato
<option>darkslategray
<option>limegreen
<option>cornflowerblue
<option>darkolivegreen
</SELECT>
<input type="button" value="start" onClick="start()">
<input type="button" value="stop" onClick="stopTest()">
</FORM>
</CENTER>



Cheers guys!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
When you press start, it waits a random amount of time before changing the backgrounds color. It gets the time when it changed and when you press stop and just subtracts them. Then it just uses a bunch of if statements to give you your "score."

Just follow the code from onClick="start()". From there look at the function start and just try to read through what it does.

Nothing really complicated about this script..
 
0
•••
Sorry, I don't know if you mis-understood ... I know NOTHING of javascript. I'm like the special olympic winner of not knowing anything programming-wise. Please please PLEASE help me! ;)

What I meant was, could you go through the important statements and do stuff like this

document.bgColor=document.response.bgColorChange.o ptions[document.response.bgColorChange.selectedIndex].text;
bgChangeStarted=true; <----- This statement does somthing lovely with the background!

Just somthing that tells me what the important commands do and where they are?

I'd much appreciate it, and there might be cake involved!
 
0
•••
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back