-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
45 lines (43 loc) · 1.53 KB
/
popup.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
$(document).ready(function () {
$("#number").keydown(function (e) {
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
// Allow: Ctrl+A
(e.keyCode == 65 && e.ctrlKey === true) ||
// Allow: Ctrl+C
(e.keyCode == 67 && e.ctrlKey === true) ||
// Allow: Ctrl+X
(e.keyCode == 88 && e.ctrlKey === true) ||
// Allow: home, end, left, right
(e.keyCode >= 35 && e.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
$("#number").on('input', function () {
var number = parseInt($("#number").val(), 10);
if (isPrime(number) == true) {
$("#result").text("is prime");
$("#result").addClass("green");
if ($("#result").hasClass("red"))
$("#result").removeClass("red");
} else {
$("#result").text("isn't prime");
$("#result").addClass("red");
if ($("#result").hasClass("green"))
$("#result").removeClass("green");
}
});
});
function isPrime(value) {
var limit = value / 2;
for (var i = 2; i <= limit; i++) {
if (value % i == 0)
return false;
}
return true;
}