-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeTypeScript.js
More file actions
124 lines (106 loc) · 4.78 KB
/
Copy pathcodeTypeScript.js
File metadata and controls
124 lines (106 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const items = [
"public class Example {\npublic static void main(String[] args) {\nSystem.out.println(\"Hey there\");\n}\n}",
"int number = 1;\n\nwhile (true) {\nSystem.out.println(number);\nif (number >= 5) {\nbreak;\n}\nnumber = number + 1;\n}",
"import java.util.*;\npublic class ListExample{\npublic static void main(String args[]){\nList<String> list=new ArrayList<String>();\nlist.add(\"Mango\")",
"num = 8\nnum_sqrt = num ** 0.5\nprint('The square root of %0.3f is %0.3f'%(num ,num_sqrt))",
"a = 5\nb = 6\nc = 7\n\ns = (a + b + c) / 2\n\narea = (s*(s-a)*(s-b)*(s-c)) ** 0.5",
"my_string = 'deepak'\nmy_tuple = (1,2,3,4)\nprint('name in a list: ', list(mystring))\nprint('numbers in a list: ', list(mytuple))",
"var names = new List<string>() { \"John\", \"Tom\", \"Peter\" };\nforeach (string name in names)\n{\nConsole.WriteLine(name);\n}",
"int i = 1;\n\nswitch (i)\n{\ncase 1:\nConsole.WriteLine(\"One\");\nbreak;\ncase 2:\nConsole.WriteLine(\"Two\");\nbreak;\n}",
"int[] intArray = new int[5] { 8, 10, 2, 6, 3 };\nArray.Sort(intArray);\nforeach (int i in intArray) Console.Write(i + \" \");",
"#include <iostream>\nusing namespace std;\nint main() {\nint a;\nint b;\ncin>>a>>b;\ncout<<a+b;\nreturn 0;\n}",
"#include <iostream>\nusing namespace std;\nint main() {\nint a;\ncin>>a;\nif(a%2 == 0)\ncout<<\"even\";\nelse cout<<\"odd\";\nreturn 0;\n}",
"#include <iostream>\nusing namespace std;\nint main() {\nstring str;\ncin>>str;\nint count = 0;\nfor(int i = 0;str[i];i++) count++;\ncout<<count;",
];
var i = -1;
const quoteDisplayElement = document.getElementById('quoteDisplay')
const quoteInputElement = document.getElementById('quoteInput')
const timerElement = document.getElementById('timer')
const userTyped = new Event("waitType")
const stopCount = new Event("stopCounting")
let mistakes = 0
let totalChars = 0
renderNewQuote()
document.addEventListener('input', () => {
quoteInputElement.focus()
const arrayQuote = quoteDisplayElement.querySelectorAll('span')
const arrayValue = quoteInputElement.value.split('')
document.getElementById("timer").style.color='#FFFFFF';
if (!started) {
document.dispatchEvent(userTyped)
}
let correct = true
arrayQuote.forEach((characterSpan, index) => {
const character = arrayValue[index]
if (character == null) {
characterSpan.classList.remove('correct')
characterSpan.classList.remove('incorrect')
correct = false
} else if (character === characterSpan.innerText) {
characterSpan.classList.add('correct')
characterSpan.classList.remove('incorrect')
} else {
mistakes++
characterSpan.classList.remove('correct')
characterSpan.classList.add('incorrect')
}
})
if (correct) {
let wpm = Math.round(((totalChars-mistakes) / 5 ) / parseInt(timer.innerText) * 60)
wpm = wpm < 0 || !wpm || wpm === Infinity ? 0: wpm;
let acc = Math.round(((totalChars-mistakes) / totalChars) * 100)
acc = acc < 0 || !acc || acc === Infinity ? 0: acc;
document.getElementById("wpm").innerHTML = "WPM: " + wpm + "\t"
document.getElementById("accuracy").innerHTML = "ACC: " + acc
renderNewQuote()
document.getElementById("wpm").style.color = 'white'
document.getElementById("accuracy").style.color = 'white'
document.dispatchEvent(stopCount)
}
})
function getRandomQuote() {
i++;
if (i == items.length) i = 0;
return items[i];
}
async function renderNewQuote() {
document.getElementById("wpm").style.color = "#222222"
document.getElementById("accuracy").style.color = "#222222"
quoteInputElement.focus()
timerElement.innerText = 0
document.getElementById("timer").style.color="#222222"
document.dispatchEvent(stopCount)
started = false
startTimer()
const quote = getRandomQuote()
totalChars = quote.length
mistakes = 0
quoteDisplayElement.innerHTML = ''
quote.split('').forEach(character => { // for looping through each character
const characterSpan = document.createElement('span')
characterSpan.innerText = character
quoteDisplayElement.appendChild(characterSpan)
})
quoteInputElement.value = null
}
let startTime
function startTimer() {
timerElement.innerText = 0
started = false
document.addEventListener("waitType", e => {
started = true
startTime = new Date()
setInterval(() => {
timer.innerText = getTimerTime()
}, 1000)
})
document.addEventListener("stopCounting", e => {
return
})
}
function getTimerTime() {
return Math.floor((new Date() - startTime) / 1000)
}
function goBack() {
window.location.href="pageMain.html";
}