forked from khushi-purwar/WebDev-ProjectKart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacci.js
57 lines (45 loc) · 1.58 KB
/
Fibonacci.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
46
47
48
49
50
51
52
53
54
55
56
57
const txtInput = document.querySelector(".inputs input"),
checkBtn = document.querySelector(".inputs button"),
infoTxt = document.querySelector(".info-txt");
let filterInput;
// javascript program to check if x is a perfect square
// A utility function that returns true if x is perfect square
function isPerfectSquare( x)
{
let s = parseInt(Math.sqrt(x));
return (s * s == x);
}
// Returns true if n is a Fibonacci Number, else false
function isFibonacci( n)
{
// n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or both
// is a perfect square
return isPerfectSquare(5 * n * n + 4) ||
isPerfectSquare(5 * n * n - 4);
}
// A utility function to test above functions
function isString(value) {
return typeof value === 'string' || value instanceof String;
}
checkBtn.addEventListener("click", () => {
let n = filterInput;
console.log(typeof n);
console.log(isFibonacci(n));
infoTxt.style.display = "block";
// if(isString(n)===true){
// return infoTxt.innerHTML = `Enter a valid input`
// }
// else{
if(isFibonacci(n)===true) {
return infoTxt.innerHTML = `Yes, <span>'${txtInput.value}'</span> is Fibanocci number`;
}
infoTxt.innerHTML = `NO, <span>'${txtInput.value}'</span> is not a Fibanocci number`;
});
txtInput.addEventListener("keyup", () => {
filterInput = txtInput.value.toLowerCase().replace(/[^A-Z0-9]/ig, "");
if(filterInput) {
return checkBtn.classList.add("active");
}
infoTxt.style.display = "none";
checkBtn.classList.remove("active");
});