-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtask2.js
More file actions
110 lines (100 loc) · 2.12 KB
/
task2.js
File metadata and controls
110 lines (100 loc) · 2.12 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//Task 2: Write a function to Generate Fibonacci Numbers. The function should take the length of the series and should return an array of Fibonacci numbers starting from 0.the sequence goes like this : 0,1,1,2,3,5,8,13,21,34,55,89 ... Here every number is the sum of the previous two numbers.
// Ex 1:
// Input: n = 3
// Output: [0,1,1]
// Ex 2:
// Input: n = 5
// Output: [0,1,1, 2,3]
// Solutions
// Naveed
console.log("Fibonacci Series:");
function Fibonacci() {
const userEnteredNumber = document.getElementById('enteredNumber').value;
let num1 = 0, num2 = 1; nextNum = 0;
for(i = 0; i < userEnteredNumber; i++) {
console.log(num1);
nextNum = num1 + num2;
num1 = num2;
num2 = nextNum;
}
}
//Raj Bhat
function fibonacci(n){
if(n>0){
var x=0;
var y=1;
var fib=0;
for(var i=1;i<=n;i++){
console.log("fib number are");
console.log(x);
fib=x+y;
x=y;
y=fib;
}
}
else {
console.log("invalid number");
}
}
fibonacci(5);
//Sachin
function generateFibonacci(length){
let numbers = [0,1];
if(length<=0){
numbers=[];
}
else if(length==1) {
numbers = [0];
}
else if(length==2) {
numbers = [0,1];
}
else if(length>2)
{
for(i=2;i<length;i++){
numbers[i]=numbers[i-1]+numbers[i-2];
}
}
return numbers;
}
//kuldip Mochi
function genFibo(num){
let arr = [0,1];
//base case for num 0, 1 and 2
if(num<=0){
arr=[];
}
else if(num==1) {
arr = [0];
}
else if(num==2) {
arr = [0,1];
}
else if(num>2)
{
for(i=2;i<num;i++){
arr[i]=arr[i-1]+arr[i-2];
}
}
return arr;
}
let fibArr = genFibo(6);
console.log(fibArr)
//Anusha
function fib(n){
if(n>0){
let num1=0;
let num2=1;
let fib=0
for(let i=1;i<=n;i++){
console.log(num1)
fib=num1+num2
num1=num2
num2=fib
}
}
else{
console.log('not valid')
}
}
console.log(fib(8))