-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy patharmstrong.js
More file actions
70 lines (63 loc) · 1.82 KB
/
armstrong.js
File metadata and controls
70 lines (63 loc) · 1.82 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
63
64
65
66
67
68
69
70
const textinput = document.querySelector("#text");
// console.log(textinput);
const res = document.getElementById("demo");
const invalid = document.getElementById("invalid");
const checkbtn = document.getElementById("btn");
function textfxn() {
// let text = document.getElementById("text").value;
// res.innerHTML = "";
invalid.innerHTML = "";
const text = textinput.value;
const hasOnlyNumbers = /^\d+$/.test(text);
console.log(text, hasOnlyNumbers);
if (text && hasOnlyNumbers) {
checkbtn.style.opacity = "1";
checkbtn.classList.add("buttonjs");
document.getElementById("btn").addEventListener("click", check);
checkbtn.disabled = false;
// document.getElementById("btn").addEventListener("enter",check );
} else {
if (text) {
invalid.innerHTML = "Invalid Input: Please use numbers only.";
}
// alert("invalid input!");
checkbtn.style.opacity = "0.25";
checkbtn.classList.remove("buttonjs");
checkbtn.disabled = true;
// window.location.reload();
}
}
function check() {
if (checkbtn.disabled) {
return;
}
res.innerHTML="";
const numStr = textinput.value;
const numberOfDigits = numStr.length;
let sum = 0;
let temp = parseInt(numStr, 10);
// An empty input shouldn't be checked
if (numStr === "") {
return;
}
while (temp > 0) {
const remainder = temp % 10;
sum += remainder ** numberOfDigits;
temp = parseInt(temp / 10, 10);
}
if (sum == numStr) {
res.innerHTML = `${numStr} is an Armstrong number ✨`;
} else {
res.innerHTML = `${numStr} is not an Armstrong number.`;
}
}
// });value;
// textinput.addEventListener("keyup", textfxn);
checkbtn.addEventListener("click", check);
textinput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
event.preventDefault();
check();
}
});
textfxn();