forked from 0xvashishth/CalcHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (39 loc) · 1.2 KB
/
index.js
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
var txtNumber = document.getElementById('txtNumber');
var txtInterval = document.getElementById('txtInterval');
var btnGenerate = document.getElementById('btnGenerate');
var ans = document.getElementById('ans');
document.addEventListener('DOMContentLoaded', onFocus);
btnGenerate.addEventListener('click', onClick);
txtInterval.addEventListener('keydown', onKeyDown);
txtNumber.addEventListener('keydown', onKeyDown);
function onClick() {
let count = 0;
if (!validateNumber(txtNumber.value)) {
while (count <= txtInterval.value) {
ans.innerHTML += parseFloat(txtNumber.value) + ' X ' + count + ' = ' + (parseFloat(txtNumber.value) * count) + '<br>';
count++;
}
validateInterval(txtInterval.value);
}
return count;
}
function validateNumber(number) {
let ret = false;
if (number == '') ret = true;
else if (number != '' && ans != '') ans.innerHTML = '';
return ret;
}
function validateInterval(interval) {
let ret = false;
if (interval > 100) {
ans.innerHTML = '';
ret = true;
}
return ret;
}
function onKeyDown(e) {
if (e.key === 'e') e.preventDefault();
}
function onFocus() {
txtNumber.focus();
}