Compare commits

...

13 Commits

Author SHA1 Message Date
Austin b8b1ae4eda
Merge pull request #396 from jamienorthman/main
10_fibonacci: handling of string zero & update of tests
2023-12-12 21:04:42 +00:00
Alex Younger a27f66263a
Merge branch 'main' into main 2023-11-15 20:39:53 -05:00
Eric Olkowski a3992aa0de
Merge pull request #409 from Luislev/main
10_fibonacci: Add alternative solution in fibonacci-solution.js
2023-11-11 07:17:33 -05:00
Luis Leiva 123e00d933
Add alternative solution in fibonacci-solution.js 2023-11-07 21:27:27 -05:00
Luis Leiva 908c4ed26e
Update fibonacci-solution.js 2023-11-05 23:12:41 -05:00
Jamienorthman 80ca665767 added explicit conversion to number for fibonacci solution 2023-10-29 12:36:58 +01:00
Miko 4a03e410bf
More consistent spelling in 10_fibonacci
Co-authored-by: Alex Younger <61510135+fortypercenttitanium@users.noreply.github.com>
2023-10-29 11:52:01 +01:00
Miko 38f0da9643
More consistent spelling in 10_fibonacci
Co-authored-by: Alex Younger <61510135+fortypercenttitanium@users.noreply.github.com>
2023-10-29 11:51:42 +01:00
Miko e5c0f77d21
Fixed usage of test (skip all but first) in exercise 2023-09-18 23:11:41 +02:00
Miko 3b84151724
Fixed usage of test (no skip) in solution 2023-09-18 23:11:09 +02:00
Miko 59a2f1ce47
Added test case for string 0 2023-09-18 23:06:51 +02:00
Miko 5ac1931f72
Added test cases for 0 and string 0 2023-09-18 23:06:29 +02:00
Miko f2c0d0955b
Added handling of string case for 0 2023-09-18 23:01:33 +02:00
3 changed files with 37 additions and 11 deletions

View File

@ -16,9 +16,15 @@ describe('fibonacci', () => {
test.skip('25th fibonacci number is 75025', () => {
expect(fibonacci(25)).toBe(75025);
});
test.skip('0th fibonacci number is 0', () => {
expect(fibonacci(0)).toBe(0);
});
test.skip('doesn\'t accept negatives', () => {
expect(fibonacci(-25)).toBe("OOPS");
});
test.skip('DOES accept strings', () => {
expect(fibonacci("0")).toBe(0);
});
test.skip('DOES accept strings', () => {
expect(fibonacci("1")).toBe(1);
});

View File

@ -1,10 +1,19 @@
const fibonacci = function(count) {
const fibonacci = function(countArg) {
// checks argument's type and makes sure we use
// a number throughout rest of function.
let count
if (typeof countArg !== 'number') {
count = parseInt(countArg)
} else {
count = countArg
}
if (count < 0) return "OOPS";
if (count === 0) return 0;
if (count == 0) return 0;
let firstPrev = 1;
let secondPrev = 0;
for (let i = 2; i <= count; i++) {
let current = firstPrev + secondPrev;
secondPrev = firstPrev;
@ -12,6 +21,14 @@ const fibonacci = function(count) {
}
return firstPrev;
};
// Another way to do it is by using an iterative approach with an array containing two values, 0 and 1.
// const fib = [0, 1];
// for (let i = 2; i <= count; i++) {
// fib[i] = fib[i - 1] + fib[i - 2];
// }
// return fib[count];
module.exports = fibonacci;

View File

@ -1,4 +1,4 @@
const fibonacci = require('./fibonacci-solution');
const fibonacci = require('./fibonacci-solution')
describe('fibonacci', () => {
test('4th fibonacci number is 3', () => {
@ -16,19 +16,22 @@ describe('fibonacci', () => {
test('25th fibonacci number is 75025', () => {
expect(fibonacci(25)).toBe(75025);
});
test('0th fibonacci number is o', () => {
test('0th fibonacci number is 0', () => {
expect(fibonacci(0)).toBe(0);
});
test("doesn't accept negatives", () => {
expect(fibonacci(-25)).toBe('OOPS');
test('doesn\'t accept negatives', () => {
expect(fibonacci(-25)).toBe("OOPS");
});
test('DOES accept strings', () => {
expect(fibonacci('1')).toBe(1);
expect(fibonacci("0")).toBe(0);
});
test('DOES accept strings', () => {
expect(fibonacci('2')).toBe(1);
expect(fibonacci("1")).toBe(1);
});
test('DOES accept strings', () => {
expect(fibonacci('8')).toBe(21);
expect(fibonacci("2")).toBe(1);
});
});
test('DOES accept strings', () => {
expect(fibonacci("8")).toBe(21);
});
});