mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-06-05 15:09:09 +09:00
Complete excercise 10
This commit is contained in:
@@ -1,4 +1,26 @@
|
||||
const fibonacci = function() {
|
||||
const fibonacci = function(number) {
|
||||
console.log(number)
|
||||
if (typeof number == "string") {
|
||||
number = parseInt(number)
|
||||
}
|
||||
const array = []
|
||||
for (i=0; i<number; i++) {
|
||||
if (i < 2) {
|
||||
let newNumber = 1
|
||||
array.push(newNumber)
|
||||
}
|
||||
else {
|
||||
let newNumber = array[i-2] + array [i-1]
|
||||
array.push(newNumber)
|
||||
}
|
||||
}
|
||||
console.log("Array: " + array)
|
||||
if (number < 0) {
|
||||
return "OOPS"
|
||||
}
|
||||
else {
|
||||
return number === 0 ? 0 : array[number-1]
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -4,34 +4,34 @@ describe('fibonacci', () => {
|
||||
test('4th fibonacci number is 3', () => {
|
||||
expect(fibonacci(4)).toBe(3);
|
||||
});
|
||||
test.skip('6th fibonacci number is 8', () => {
|
||||
test('6th fibonacci number is 8', () => {
|
||||
expect(fibonacci(6)).toBe(8);
|
||||
});
|
||||
test.skip('10th fibonacci number is 55', () => {
|
||||
test('10th fibonacci number is 55', () => {
|
||||
expect(fibonacci(10)).toBe(55);
|
||||
});
|
||||
test.skip('15th fibonacci number is 610', () => {
|
||||
test('15th fibonacci number is 610', () => {
|
||||
expect(fibonacci(15)).toBe(610);
|
||||
});
|
||||
test.skip('25th fibonacci number is 75025', () => {
|
||||
test('25th fibonacci number is 75025', () => {
|
||||
expect(fibonacci(25)).toBe(75025);
|
||||
});
|
||||
test.skip('0th fibonacci number is 0', () => {
|
||||
test('0th fibonacci number is 0', () => {
|
||||
expect(fibonacci(0)).toBe(0);
|
||||
});
|
||||
test.skip('doesn\'t accept negatives', () => {
|
||||
test('doesn\'t accept negatives', () => {
|
||||
expect(fibonacci(-25)).toBe("OOPS");
|
||||
});
|
||||
test.skip('DOES accept strings', () => {
|
||||
test('DOES accept strings', () => {
|
||||
expect(fibonacci("0")).toBe(0);
|
||||
});
|
||||
test.skip('DOES accept strings', () => {
|
||||
test('DOES accept strings', () => {
|
||||
expect(fibonacci("1")).toBe(1);
|
||||
});
|
||||
test.skip('DOES accept strings', () => {
|
||||
test('DOES accept strings', () => {
|
||||
expect(fibonacci("2")).toBe(1);
|
||||
});
|
||||
test.skip('DOES accept strings', () => {
|
||||
test('DOES accept strings', () => {
|
||||
expect(fibonacci("8")).toBe(21);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user