-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
39 lines (31 loc) · 978 Bytes
/
app.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
let inputMin = document.querySelector('#input-min');
let inputMax = document.querySelector('#input-max');
let btnGenerate = document.querySelector('#btn-generate');
let result = document.querySelector('.tag-result');
btnGenerate.addEventListener('click', () => {
let min = +inputMin.value;
let max = +inputMax.value;
let isValid = checkInput(min, max);
if(!isValid){
showError();
return;
}
let random = generateRandom(min, max);
showRandom(random);
});
function checkInput(min, max){
let isValidNumber = true;
if(isNaN(min) || isNaN(max) || (min > max) || (min < 0) || (max < 0) || !Number.isInteger(min) || !Number.isInteger(max)){
isValidNumber = false;
}
return isValidNumber;
}
function generateRandom(min, max){
return Math.floor(Math.random() * (max - min + 1) + min);
}
function showRandom(num){
result.textContent = num;
}
function showError(){
result.textContent = 'ERROR';
}