-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (47 loc) · 1.74 KB
/
script.js
File metadata and controls
55 lines (47 loc) · 1.74 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
document.addEventListener('DOMContentLoaded', () => {
const minInput = document.getElementById('min');
const maxInput = document.getElementById('max');
const generateBtn = document.getElementById('generate');
const resultNumber = document.querySelector('.number');
// Function to generate random number
const generateRandomNumber = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};
// Function to validate input
const validateInput = () => {
const min = parseInt(minInput.value);
const max = parseInt(maxInput.value);
if (isNaN(min) || isNaN(max)) {
alert('Please enter valid numbers');
return false;
}
if (min >= max) {
alert('Maximum number must be greater than minimum number');
return false;
}
return true;
};
// Handle generate button click
generateBtn.addEventListener('click', () => {
if (validateInput()) {
const min = parseInt(minInput.value);
const max = parseInt(maxInput.value);
const randomNumber = generateRandomNumber(min, max);
// Animate the number change
resultNumber.style.animation = 'none';
resultNumber.offsetHeight; // Trigger reflow
resultNumber.style.animation = 'pulse 1s ease-in-out infinite';
resultNumber.textContent = randomNumber;
}
});
// Handle Enter key press
[minInput, maxInput].forEach(input => {
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
generateBtn.click();
}
});
});
});