fibonacci counter in typescript code example
Example 1: how to generate a fibonacci sequence in javascript
let fibonacci = [0,1];
function listFibonacci(num) {
for (let i = 1; i < num; i++) {
fibonacci.push(fibonacci[i] + fibonacci[i - 1]);
}
console.log(fibonacci);
}
listFibonacci(10);
Example 2: js to confirm fibonnaci
function isFibonacci(n) {
var fib,
a = (5 * Math.pow(n, 2) + 4),
b = (5 * Math.pow(n, 2) - 4)
var result = Math.sqrt(a) % 1 == 0,
res = Math.sqrt(b) % 1 == 0;
if (result || res == true)
{
fib = Math.round(n * 1.618);
console.log("The next Fibonacci number is " + fib);
} else {
console.log(`The given number ${n} is not a fibonacci number`);
}
}
$('#fib').on("keyup change", function() {
isFibonacci(+this.value)
})
Example 3: fibonacci counter in typescript
'use strict';
let fibonacci: number[] = [0, 1];
function listFibonacci(num: number) {
for (let i: number = 2; i < num; i++) {
fibonacci[i] = fibonacci[i - 2] + fibonacci[i - 1];
}
return fibonacci;
}
console.log(listFibonacci(10));