Commit | Line | Data |
---|---|---|
3d0fb55f | 1 | $ = document.getElementById.bind(document); |
2 | function printStats(lines, numLines, numChars, parasWithExtraSpace, parasLines) { | |
3 | $("results").innerHTML=""; | |
4 | for(var i=0; i<numLines; i++) { | |
5 | $("results").innerHTML+=(i<9?"0"+(i+1):(i+1))+" "+lines[i]+"<br />"; | |
6 | if(parasLines.indexOf(i+1)>=0 && $("chk2").checked) $("results").innerHTML+="<br />"; | |
7 | } | |
8 | ||
9 | $("statLines").innerHTML="Lines: "+numLines+"/47"; | |
10 | $("statLines").style.color=numLines>47?"red":"initial"; | |
11 | $("statChars").innerHTML="Characters: "+numChars+"/4000"; | |
12 | $("statChars").style.color=numChars>4000?"red":"initial"; | |
13 | ||
14 | if(parasWithExtraSpace.length>0) { | |
15 | $("statWarn").innerHTML="Warning: Extra space(s) found at end of paragraph(s) "+parasWithExtraSpace.join(", "); | |
16 | $("statWarn").style.visibility="visible"; | |
17 | $("statWarn").style.color="red"; | |
18 | } else { | |
19 | $("statWarn").innerHTML="Warning: Extra space(s) found at end of paragraph(s) "; | |
20 | $("statWarn").style.visibility="hidden"; | |
21 | } | |
22 | } | |
23 | ||
24 | function analyse() { | |
25 | var text=$("txt1").value; | |
26 | var parasWithExtraSpace=[]; | |
27 | var parasLines=[]; | |
28 | var paras=text.split('\n'); | |
29 | var lines=[]; | |
30 | var line="" | |
31 | if(text=="") { | |
32 | printStats(lines, 0, 0, parasWithExtraSpace, parasLines); | |
33 | return; | |
34 | } | |
85defcca AIL |
35 | |
36 | var maxlinelen = 94; | |
37 | if($("teacher").checked) maxlinelen = 80; | |
38 | ||
3d0fb55f | 39 | for(var i=0; i<paras.length; i++) { |
40 | if(paras[i][paras[i].length-1]==' ') parasWithExtraSpace.push(i+1); | |
41 | do { | |
85defcca AIL |
42 | if(paras[i][0]==' ') { |
43 | paras[i] = paras[i].substring(1); //remove preceding space | |
44 | } | |
45 | ||
46 | /* extract line of maxlinelen chars or fewer */ | |
47 | if(paras[i].length > maxlinelen) { | |
48 | line = paras[i].substring(0, maxlinelen); | |
49 | } else { | |
50 | line = paras[i].substring(0, paras[i].length); | |
51 | } | |
52 | ||
53 | var lastidx = line.lastIndexOf(' '); | |
54 | if(lastidx >= 0) { //if there is a space in line | |
55 | if(line.length < maxlinelen) { | |
56 | lastidx = line.length; //if it can contain whole line | |
57 | } | |
58 | } else { | |
59 | lastidx = line.length; //cutoff line if line has maxlinelen | |
60 | } // chars without spaces | |
61 | ||
62 | if(paras[i][maxlinelen] == ' ') { //if extracted right before a space | |
63 | lastidx = maxlinelen; | |
64 | } | |
65 | paras[i] = line.substring(lastidx+1)+paras[i].substring(maxlinelen); //remove extracted string (up to lastidx) from paras | |
3d0fb55f | 66 | line=line.substring(0, lastidx); |
67 | lines.push(line); | |
68 | } while(paras[i]!=""); | |
69 | parasLines.push(lines.length); //lines where paragraphs end | |
70 | } | |
71 | printStats(lines, lines.length, text.length+paras.length-1, parasWithExtraSpace, parasLines); | |
72 | } | |
73 | ||
74 | function autocheck() { | |
75 | if($("chk1").checked) { | |
76 | $("txt1").addEventListener("input", analyse); | |
77 | $("btn1").style.visibility='hidden'; | |
78 | analyse(); | |
79 | } else { | |
80 | $("txt1").removeEventListener("input", analyse); | |
81 | $("btn1").style.visibility='visible'; | |
82 | } | |
83 | } |