Compactify & rewrite "typing speed check" (cut out functions) to learn. Explain eac

h of 32 lines: why the dot-like hierarchy of variables takes place?

<script type="text/javascript">
    var timing = {s: null, e: null, t: null}; // start, end, textbox
    		function typoInit()       {
    			typoSetup();
    			var t = timing.t; }
    		function typoStart()                        {
    			timing.s = timing.e = new Date();
    			timing.t.onkeypress = typoContinue; }
    		function typoContinue(e)       {
    			if (!e) e = event;
    			timing.e = new Date(); }
    		function typoSetup()              {
    			timing.t = document.getElementById('typoBox');
    			var t = timing.t;
    			t.onkeypress = typoStart; }
    		function typoCalculate()                            {
    var res = document.getElementById('typoResult');
    if (!res) return;`;`. mnbv
    var deltams, chars, cpms, snippets;
    deltams = timing.e.getTime() - timing.s.getTime();
    chars = timing.t.value.length;
    cpms = chars / deltams;
    snippets = [ "", chars, " characters (in ", deltams, " ms): ", cpms, " cpms" ];
    res.innerHTML=snippets.join(""); typoSetup(); 			     } </script>
<!--2-->
    
    <form name="foo" onsubmit="return false;">
    	<textarea id="typoBox"></textarea><br />
    	<input type="button" id="typoCalcBtn" value="Ok" />
    <span id="typoResult"></span> </form>
<!--3-->

    <script type="text/javascript">
    	document.getElementById('typoCalcBtn').onclick = typoCalculate;	typoInit();
    </script>

One man kindly explained me some structure outline. Okay, to be true the program was cut down from 90 lines and it still works.

> Object timing contains variables: s, e в in format: Date – this is the
> time of the typing text beginning and ending time, & t, presenting
> itself as a DOM element – as a textbox.
>
> Function typoStart zeroes out timing.s value and timing.e and assigns
> typoContinue function to the onkeypress event for “our textbox”.
>
> Function typoContinue receives a pressed keystroke, assigns current
> time to timing.e.
>
> Function typoSetup defines, that timing.t – is “our textbox”. Launches
> typoStart in case of pressing a button.
>
> Function typoInit launches & typoSetup does nothing special.
>
> And finally the main function is – typoCalculate:
>
> 1. Defines, where to output the result (res variable)
>
> 2. deltams = start time minus ending time if the time and end of the
> text typing ≠ zero (i.e. somebody typed a text). deltams simplifies
> the program showcase by just showing .getTime()
>
> 3. сhars = the quantity of typed symbols
>
> 4. snippets – is an information array, which we output as being the
> results
>
> 5. Basically we output the result and launch typoSetup again (we’re
> waiting new keypresses).

QUESTIONS:

  1. Main question: What is <span id="typoResult"></span> and why it is used?

  2. res.innerHTML=snippets.join("") - why specifically this? Do we always output like this (document.write …)? And why we display the output as an array (I know there’re loops L: mov cx,3 loop L).

  3. OOP and DOM are extremely hard. Is there a more simply way of programming my “touching speed”? I mean the hierarchy of dot-parameters and variables seem complicated.

  4. if (!res) return - I just don’t understand it

  5. document.getElementById('typoCalcBtn').onclick =
    Can it be without a function? And ById (which get assigns which variable?).
    An explanation to 5: a function (functions) is called and it complicates perception in learning.
    And functions are called twice separately from lower <script>.
    My program starts with ById - so the tracing (&there’s no debugger!!!) begins… from this starting point and takes subroutines… more and more. Yikes!

  6. Why scripts have two segments (why not one)? - puzzling span should be before/or in the middle of them?

  7. <form name="foo" onsubmit="return false;"> . "return false;" The meaning in the form’s contest of the “return false”,;

  8. function typoContinue(e){if (!e) e = event; function typoContinue(e) - It is the only function with e inside the brackets. Receives? - How? And Exclamation mark.

The full version http://lanother.tk/ and this is http://lanother.tk/rc.php.

Hi,

Welcome to the forums :slight_smile:

I looked at your questions, but I’ve no idea where to start.
If you want any chance of an answer, I would suggest asking just one concise question which has an answer.
As it stands, it looks rather like an exam and you are expecting other people to do all the work for you.

First of all I’m russian-speaking. So it’s slowly going now…

Not at all. I have zero knowledge of JS. So I decided to rewrite the code thrice.
(Every time when programming the functions and subroutines (assembler) were so far that I just wrote simple progs interrupt calls etc…

So I beg you at least to release the code totally from functions (to me to understand - like not a fish inside another one).

<script type="text/javascript">
var timing = {st: null, en: null, tx: null}; // start, end, textbox
		function initpr()       {
			timing.tx = document.getElementById('prin');
			var tx = timing.tx;
			tx.onkeypress = function ()                        {
			timing.st = timing.en = new Date();
	timing.tx.onkeypress = function (en) { if (!en) en = event; timing.en = new Date(); }}}

		function clc()                            {
r = document.getElementById('resultpr');

deltams = timing.en.getTime() - timing.st.getTime();
chars = timing.tx.value.length;
cpms = chars / deltams;
snippets = [ chars, " chars (in ", deltams, " ms): ", cpms, " cpms" ];
r.innerHTML=snippets.join(""); initpr(); 			     } </script>

<form name="foo" onsubmit="return false;">
	<textarea id="prin"></textarea><br />
	<input id="btnclc" />
<span id="resultpr"></span> </form>
<script type="text/javascript">
	document.getElementById('btnclc').onclick = clc;	initpr();
</script>

It’s code. It works even now.
When I asked on IRC they helped to write semi-without a func: timing.tx.onkeypress = function (en) (as you could see it is inside another func).

  1. Lots of books on JS I remember (and they frighten me of simplicity) - they just alert(smthHAHA) and go windows and nothing more. And it is smth like

smth.onclick=alert() . And I want to have a single <script> (so that’s why the question arises why they are apart?)

I want

<script>
alert( my program ); //no functions
</script>

<form>
</form>
end.

initpr(); - TWO TIMES (my mind cycles - and couldn’t grasp - alas!)

As no novice was interested in process and to start together I won’t tell how. Just to say the problem is finally solved (only kind ppl helped after other have written good just inspiring (no code,info)):
<input id=“b"onblur=“l()”></input><script>t=0;document.getElementById(‘b’).onkeypress=function(){t==0?s=new Date():e=new Date();t=1}
function l(){d=e.getTime()-s.getTime();c=b.value.length;b.value+=” c"+c+“ms”+d+" cpms"+c/d}</script>