Following are the test cases
Test cases | Result | Status |
---|---|---|
generateFibSeries(5) | {0,1,1,2,3} | Ok |
generateFibSeries(7) | {0,1,1,2,3,5,8} | Ok |
generateFibSeries(10) | {0,1,1,2,3,5,8,13,21,34} | Ok |
Java solution
public class Program11 {
public static void main(String[] args) {
Program11.generateFibSeries(5);
Program11.generateFibSeries(7);
Program11.generateFibSeries(10);
}
public static int[] generateFibSeries(int limit){
int f = 0;
int g = 1;
int array[] = new int[limit];
for (int i = 0; i < limit; i++) {
array[i] = f;
f = f + g;
g = f - g;
}
return array;
}
}
Javascript solution
const generateFibSeries = (limit) => {
let first = 0;
let second = 1;
let array = [];
for (let index = 0; index < limit; index++) {
array.push(first);
first = first + second;
second = first - second;
}
return array;
}
console.log(generateFibSeries(5)); //[0,1,1,2,3]
console.log(generateFibSeries(7)); //[0,1,1,2,3,5,8]
console.log(generateFibSeries(10)); //[0,1,1,2,3,5,8,13,21,34]