-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_prime.html
More file actions
62 lines (58 loc) · 2.09 KB
/
check_prime.html
File metadata and controls
62 lines (58 loc) · 2.09 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
<!DOCTYPE html>
<html>
<head>
<title>Проверка простого числа</title>
<meta charset="UTF-8">
<style>
.normal {
border: 1px solid black;
}
.green {
border: 2px solid green;
}
.blue {
border: 2px solid blue;
}
.red {
border: 2px solid red;
}
</style>
</head>
<body>
<input type="text" id="chisloVvoda" class="" normal="">
<button id="knopkaproverki" class="normal">Проверить</button>
<script>
const poleVvoda = document.getElementById('chisloVvoda');
const knopka = document.getElementById('knopkaproverki');
function proveritProstoe(chislo) {
for (let i = 2; i <= Math.sqrt(chislo); i++) {
if (chislo % i === 0) {
return false; // Число не является простым
}
}
return chislo > 1; // Число является простым, если больше 1
}
function proveritCeloeChislo(stroka) {
const regulyarka = /^-?\d+$/;
return regulyarka.test(stroka);
}
knopka.addEventListener("click", function () {
const vvedenayaStroka = poleVvoda.value;
if (proveritCeloeChislo(vvedenayaStroka)) {
const chislo = parseInt(poleVvoda.value)
if (proveritProstoe(chislo)) {
poleVvoda.classList.add("green")
} else {
poleVvoda.classList.add("blue")
}
} else {
poleVvoda.classList.add("red")
}
});
poleVvoda.addEventListener('input', function () {
poleVvoda.className = "";
poleVvoda.classList.add('normal');
});
</script>
</body>
</html>